PHPackages                             zamaldev/xml-model - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. zamaldev/xml-model

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

zamaldev/xml-model
==================

Fast parse xml info model

v1.0.0(1mo ago)00MITPHPPHP &gt;=8.0

Since Apr 11Pushed 1mo agoCompare

[ Source](https://github.com/zamaldev/xml-model)[ Packagist](https://packagist.org/packages/zamaldev/xml-model)[ Docs](https://github.com/zamaldev/xml-model)[ RSS](/packages/zamaldev-xml-model/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

XML to Model
============

[](#xml-to-model)

Simple and fast package to convert XML into plain PHP objects.

Why?
----

[](#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
------------

[](#requirements)

- PHP 8+

Installation
------------

[](#installation)

```
composer require zamaldev/xml-model
```

Basic Usage
-----------

[](#basic-usage)

```
use Zamaldev\XmlModel\XmlModel;

require 'vendor/autoload.php';

class Model {
    public string $key;

    public int $key2;
}

$parser = new XmlModel();

$data = key2; // 2
```

Key mapping
-----------

[](#key-mapping)

You can use `Attributes\Map` property attribute, so map XML keys to model properties.

```
use Zamaldev\XmlModel\Attributes\Map;
use Zamaldev\XmlModel\XmlModel;

require 'vendor/autoload.php';

class Model {
    #[Map(name: 'key')]
    public string $string;

    #[Map(name: 'key2')]
    public int $int;
}

$parser = new XmlModel();

$data = int; // 2
```

Arrays
------

[](#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.

```
use Zamaldev\XmlModel\Attributes\AsArray;
use Zamaldev\XmlModel\Attributes\Map;
use Zamaldev\XmlModel\XmlModel;

require 'vendor/autoload.php';

class Model
{
    #[Map(name: '@level')]
    public ?string $level = null;

    /**
     * @var array
     */
    #[AsArray(itemType: Model::class)]
    public array $item = [];
}

$parser = new XmlModel();

$data = level; // "1"
echo $model->item[0]->item[0]->level; // "2"
echo $model->item[0]->item[0]->item[0]->level; // "3"
```

DNF types
---------

[](#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.

```
use Zamaldev\XmlModel\Attributes\Caster;
use Zamaldev\XmlModel\XmlModel;
use Zamaldev\XmlModel\XmlModelInterface;

require 'vendor/autoload.php';

#[Attribute(Attribute::TARGET_PROPERTY)]
class CustomCaster implements Caster
{
    public function cast(XmlModelInterface $xmlModel, mixed $value): mixed
    {
        /** @var SimpleXMLElement $value */

        // Here will be your logic.
        if (count($value) > 1) {
            return (array) $value;
        }
        if (count($value->children()) > 0) {
            return $xmlModel->parse($value, Model::class);
        }

        return (string) $value;
    }
}

class Model {
    #[CustomCaster]
    public self|string|array $result;
}

$parser = new XmlModel();

$data = result[1]; // "2"
echo $model->result[2]; // "3"

$data =
