PHPackages                             nilportugues/xml - 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. [API Development](/categories/api)
4. /
5. nilportugues/xml

ActiveLibrary[API Development](/categories/api)

nilportugues/xml
================

XML transformer outputting valid API responses.

1.0.0(10y ago)222.2kMITPHPPHP &gt;=5.5.0

Since Jun 16Pushed 9y agoCompare

[ Source](https://github.com/nilportugues/php-xml)[ Packagist](https://packagist.org/packages/nilportugues/xml)[ Docs](http://nilportugues.com)[ RSS](/packages/nilportugues-xml/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

XML Transformer
===============

[](#xml-transformer)

[![Build Status](https://camo.githubusercontent.com/de82cd61b2d3cc6d71c98f9a9d2b40409c479407916c8a1ee537b97f4573fbf9/68747470733a2f2f7472617669732d63692e6f72672f6e696c706f727475677565732f7068702d786d6c2e7376673f)](https://travis-ci.org/nilportugues/php-xml)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c00abb9a0f04aa32f4d9277ed73c9b0c8b73afa17e030540d57826fea0de7057/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f7068702d786d6c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/php-xml/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/0f17adfb305d4cd6019b20db870df611158fe9482236a196fb37637e5613167a/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f64643836303465652d316565322d346230652d396366652d6238613634323330386432302f6d696e692e706e673f)](https://insight.sensiolabs.com/projects/dd8604ee-1ee2-4b0e-9cfe-b8a642308d20)[![Latest Stable Version](https://camo.githubusercontent.com/5f49085530b62d5686185e411e02bde014446b31662ae48dbc73f6c06c7621b7/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f786d6c2f762f737461626c653f)](https://packagist.org/packages/nilportugues/xml)[![Total Downloads](https://camo.githubusercontent.com/b923bbeac3ae6bd3447de7bcfabcf62ac707fd2ba82d319bcc64abd526cc106a/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f786d6c2f646f776e6c6f6164733f)](https://packagist.org/packages/nilportugues/xml)[![License](https://camo.githubusercontent.com/c5990ff8e7d828c81d76991fbdf853aedb5529bb63a10e9329d95d8466b36f08/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f786d6c2f6c6963656e73653f)](https://packagist.org/packages/nilportugues/xml)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

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

[](#installation)

Use [Composer](https://getcomposer.org) to install the package:

```
$ composer require nilportugues/xml
```

Usage
-----

[](#usage)

Given a PHP Object, and a series of mappings, the **XML Transformer** will represent the given data as a XML object.

For instance, given the following piece of code, defining a Blog Post and some comments:

```
$post = new Post(
  new PostId(9),
  'Hello World',
  'Your first post',
  new User(
      new UserId(1),
      'Post Author'
  ),
  [
      new Comment(
          new CommentId(1000),
          'Have no fear, sers, your king is safe.',
          new User(new UserId(2), 'Barristan Selmy'),
          [
              'created_at' => (new DateTime('2015/07/18 12:13:00'))->format('c'),
              'accepted_at' => (new DateTime('2015/07/19 00:00:00'))->format('c'),
          ]
      ),
  ]
);
```

And a Mapping array for all the involved classes:

```
use NilPortugues\Api\Mapping\Mapper;

$mappings = [
    [
        'class' => Post::class,
        'alias' => 'Message',
        'aliased_properties' => [
            'author' => 'author',
            'title' => 'headline',
            'content' => 'body',
        ],
        'hide_properties' => [

        ],
        'id_properties' => [
            'postId',
        ],
        'urls' => [
            'self' => 'http://example.com/posts/{postId}',
            'comments' => 'http://example.com/posts/{postId}/comments'
        ],
    ],
    [
        'class' => PostId::class,
        'alias' => '',
        'aliased_properties' => [],
        'hide_properties' => [],
        'id_properties' => [
            'postId',
        ],
        'urls' => [
            'self' => 'http://example.com/posts/{postId}',
        ],
    ],
    [
        'class' => User::class,
        'alias' => '',
        'aliased_properties' => [],
        'hide_properties' => [],
        'id_properties' => [
            'userId',
        ],
        'urls' => [
            'self' => 'http://example.com/users/{userId}',
            'friends' => 'http://example.com/users/{userId}/friends',
            'comments' => 'http://example.com/users/{userId}/comments',
        ],
    ],
    [
        'class' => UserId::class,
        'alias' => '',
        'aliased_properties' => [],
        'hide_properties' => [],
        'id_properties' => [
            'userId',
        ],
        'urls' => [
            'self' => 'http://example.com/users/{userId}',
            'friends' => 'http://example.com/users/{userId}/friends',
            'comments' => 'http://example.com/users/{userId}/comments',
        ],
    ],
    [
        'class' => Comment::class,
        'alias' => '',
        'aliased_properties' => [],
        'hide_properties' => [],
        'id_properties' => [
            'commentId',
        ],
        'urls' => [
            'self' => 'http://example.com/comments/{commentId}',
        ],
    ],
    [
        'class' => CommentId::class,
        'alias' => '',
        'aliased_properties' => [],
        'hide_properties' => [],
        'id_properties' => [
            'commentId',
        ],
        'urls' => [
            'self' => 'http://example.com/comments/{commentId}',
        ],
    ],
];

$mapper = new Mapper($mappings);
```

Calling the transformer will output a **valid XML response** using the correct formatting:

```
use NilPortugues\Api\Xml\XmlSerializer;
use NilPortugues\Api\Xml\Http\Message\Response;

$serializer = new XmlSerializer($mapper);
$output = $serializer->serialize($post);

//PSR7 Response with headers and content.
$response = new Response($output);

header(
    sprintf(
        'HTTP/%s %s %s',
        $response->getProtocolVersion(),
        $response->getStatusCode(),
        $response->getReasonPhrase()
    )
);
foreach($response->getHeaders() as $header => $values) {
    header(sprintf("%s:%s\n", $header, implode(', ', $values)));
}

echo $response->getBody();
```

**Output:**

```
HTTP/1.1 200 OK
Cache-Control: private, max-age=0, must-revalidate
Content-type: text/xml; charset=utf-8

```

```

  9]]>
  Hello World]]>
  Your first post]]>

    1]]>
    Post Author]]>

      1000]]>

        2015-07-18T12:13:00+02:00]]>
        2015-07-19T00:00:00+02:00]]>

      Have no fear, sers, your king is safe.]]>

        2]]>
        Barristan Selmy]]>

```

Quality
-------

[](#quality)

To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit.

This library attempts to comply with [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/), [PSR-4](http://www.php-fig.org/psr/psr-4/) and [PSR-7](http://www.php-fig.org/psr/psr-7/).

If you notice compliance oversights, please send a patch via [Pull Request](https://github.com/nilportugues/php-xml/pulls).

Contribute
----------

[](#contribute)

Contributions to the package are always welcome!

- Report any bugs or issues you find on the [issue tracker](https://github.com/nilportugues/php-xml/issues/new).
- You can grab the source code at the package's [Git repository](https://github.com/nilportugues/php-xml).

Support
-------

[](#support)

Get in touch with me using one of the following means:

- Emailing me at
- Opening an [Issue](https://github.com/nilportugues/php-xml/issues/new)

Authors
-------

[](#authors)

- [Nil Portugués Calderó](http://nilportugues.com)
- [The Community Contributors](https://github.com/nilportugues/php-xml/graphs/contributors)

License
-------

[](#license)

The code base is licensed under the [MIT license](LICENSE).

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

3666d ago

### Community

Maintainers

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

---

Top Contributors

[![nilportugues](https://avatars.githubusercontent.com/u/550948?v=4)](https://github.com/nilportugues "nilportugues (11 commits)")

---

Tags

composermarshallerphpphp7serializertransformerxmlxml-transformationresponseapixmlpsr7serializertransformer

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/nilportugues-xml/health.svg)

```
[![Health](https://phpackages.com/badges/nilportugues-xml/health.svg)](https://phpackages.com/packages/nilportugues-xml)
```

###  Alternatives

[nilportugues/json-api

Serializer transformers outputting valid API responses in JSON, JSON API and HAL+JSON API formats.

70106.7k3](/packages/nilportugues-json-api)[nilportugues/laravel5-json-api

Laravel 5 JSON API Transformer Package

31032.5k1](/packages/nilportugues-laravel5-json-api)[nilportugues/jsonapi-bundle

Symfony 2 &amp; 3 JSON API Transformer Package

11446.4k](/packages/nilportugues-jsonapi-bundle)[flugger/laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal and the elegancy of Laravel.

8911.6M5](/packages/flugger-laravel-responder)[nilportugues/hal

HAL+JSON &amp; HAL+XML API transformer outputting valid API responses.

307.7k2](/packages/nilportugues-hal)[nilportugues/api-problems

PSR7 Response implementation for the Problem Details for HTTP APIs

1749.5k2](/packages/nilportugues-api-problems)

PHPackages © 2026

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