PHPackages                             nikserg/yii2-xml-model-behavior - 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. nikserg/yii2-xml-model-behavior

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

nikserg/yii2-xml-model-behavior
===============================

Behavior to serialize model as XML

1.0.0(7y ago)047PHP

Since Feb 25Pushed 6y agoCompare

[ Source](https://github.com/nikserg/yii2-xml-model-behavior)[ Packagist](https://packagist.org/packages/nikserg/yii2-xml-model-behavior)[ RSS](/packages/nikserg-yii2-xml-model-behavior/feed)WikiDiscussions master Synced yesterday

READMEChangelog (4)Dependencies (1)Versions (6)Used By (0)

yii2-xml-model-behavior
=======================

[](#yii2-xml-model-behavior)

Behavior and trait to serialize model as XML

Install
-------

[](#install)

`composer require nikserg/yii2-xml-model-behavior`

Usage
-----

[](#usage)

Core functionality is `$model->asXml()`, which returns XML string, representating object. To successfully use this, the `$model`'s class should satisfy conditions:

1. Extends `yii\base\Model`
2. Uses behavior `nikserg\yii2\XmlModelBehavior\XmlModelBehavior`
3. Uses trait `nikserg\yii2\XmlModelBehavior\XmlArrayableTrait`
4. Overrides `fields()` function to use underscore functionality (see in Explaining Example section).

Simple example better than long explaination
--------------------------------------------

[](#simple-example-better-than-long-explaination)

```
use Document;
use nikserg\yii2\XmlModelBehavior\XmlArrayableTrait;
use nikserg\yii2\XmlModelBehavior\XmlModelBehavior;
use yii\base\Model;
class File extends Model
{
    use XmlArrayableTrait;

    public $idFile = null;
    public $applicationVersion = null;
    public $formVersion = null;

    /**
     *
     * @property Document $document
     */
    public $document = null;

    public function behaviors()
    {
        $return = parent::behaviors();
        $return[] = [
            'class'       => XmlModelBehavior::class,
            'rootElement' => 'File',
        ];
        return $return;
    }

    public function fields()
    {
        return [
            '_idFile'   => 'idFile',
            '_applicationVersion' => 'applicationVersion',
            '_formVersion' => 'formVersion',
            'document'  => 'document',
        ];
    }
}

class Document extends Model
{
    use XmlArrayableTrait;
    public $documentId = null;

    public function fields()
    {
        return [
            '_id'   => 'documentId',
        ];
    }
}

```

Result of calling `$file->asXml()`:

```

```

Explaining example
------------------

[](#explaining-example)

As you can see, behavior adds only in root model. Trait should be added to root AND child models. Parameter `rootElement` can set the name of root XML element. In `fields()` function, those attributes, which names starts with underscore `_`, would be transformed into XML attributes of element. Other attributes would be children elements.

Behind the scene
----------------

[](#behind-the-scene)

`nikserg\yii2\XmlModelBehavior\XmlModelBehavior` provides `$model->asXml()`function. In fact, it's just a wrapper around `Spatie\ArrayToXml\ArrayToXml\ArrayToXml::convert` function, which is not very interesting. Interesting part starts with nesting models. To support unserscore functionality, you must override `toArray()` function, which is impossible in behavior. This is where trait comes in game.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

5

Last Release

2633d ago

Major Versions

0.0.4 → 1.0.02019-02-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/b03ce989f89fc2cc42dc6847d51f517a6e40f3dddde319060169a933e4bc0c77?d=identicon)[nikserg](/maintainers/nikserg)

---

Top Contributors

[![uc-itcom](https://avatars.githubusercontent.com/u/21470696?v=4)](https://github.com/uc-itcom "uc-itcom (2 commits)")

### Embed Badge

![Health badge](/badges/nikserg-yii2-xml-model-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/nikserg-yii2-xml-model-behavior/health.svg)](https://phpackages.com/packages/nikserg-yii2-xml-model-behavior)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
