Download the PHP package zamaldev/xml-model without Composer
On this page you can find all versions of the php package zamaldev/xml-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zamaldev/xml-model
More information about zamaldev/xml-model
Files in zamaldev/xml-model
Package xml-model
Short Description Fast parse xml info model
License MIT
Homepage https://github.com/zamaldev/xml-model
Informations about the package xml-model
XML to Model
Simple and fast package to convert XML into plain PHP objects.
Why?
Working with third-party APIs often means dealing with raw arrays, stdClass objects or some kind of xml readers.
This package allows you to:
- Convert XML into typed PHP objects
- Use IDE autocompletion and static analysis
- Keep your code clean and maintainable
- Generate model directly from response
- Do it with performance in mind
Note: this package will not be suitable for cases when you need to work with almost raw xml, like parsing metadata fields, reading comments, etc. Take a look at the examples and tests before using.
Requirements
- PHP 8+
Installation
Basic Usage
Key mapping
You can use Attributes\Map property attribute, so map XML keys to model properties.
Arrays
To parse XML into array of models or scalars, Attributes\AsArray property attribute can be used. By default, array will be parsed into array of string, but you can specify type with additional properties.
DNF types
While working with inconsistent APIs, sometime you may need to support few types per one property. This is possible using Attributes\Caster interface. However, only you know the resolution logic, so you should write your own implementation for your specific case. If you need to cast value to any model, you have the $xmlModel parameter, this is current XmlModel instance, so you could use parse method.
Sanitizer
Don't blind trust any third party API you work with. It is important to validate or at least simple sanitize the input. For this purpose, you can use Attributes\Sanitizer interface. Package already provides Attributes\SimpleSanitizer, but you can write your own for your needs.
Note attributes order. As you can see from example, it goes from top to bottom, so sanitize chain will goes like this:
" vAlUe " -(trim)-> "vAlUe" -(strtolower)-> "value" -(ucfirst)-> "Value"
Generator
Working with many third party requests and huge responses, it is useful to just autogenerate everything. For this purposes XmlGenerator class exists.
Here is an example on how to use it:
This script will generate those files:
Some rules on how generator works:
- New class names generated at
[root model][key] - If key has prohibited symbols for class properties, they are simply omitted, but
Attributes\Mapis added - When there same class name is used for different objects (even with same structure), they resolved with incremental index
[root model][key][index] - If array has different types inside,
mixedtype will be used, so the xml text values will be used "as is"
All versions of xml-model with dependencies
ext-simplexml Version *