{"id":15658934,"url":"https://github.com/nilportugues/php-xml","last_synced_at":"2025-05-05T03:44:28.861Z","repository":{"id":57027223,"uuid":"61248374","full_name":"nilportugues/php-xml","owner":"nilportugues","description":"XML transformer outputting valid (PSR-7) API Responses.","archived":false,"fork":false,"pushed_at":"2017-05-04T20:20:21.000Z","size":19,"stargazers_count":22,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-05T03:44:03.873Z","etag":null,"topics":["composer","marshaller","php","php7","serializer","transformer","xml","xml-transformation"],"latest_commit_sha":null,"homepage":"http://nilportugues.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nilportugues.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-15T23:47:25.000Z","updated_at":"2022-02-12T06:46:36.000Z","dependencies_parsed_at":"2022-08-23T16:20:38.773Z","dependency_job_id":null,"html_url":"https://github.com/nilportugues/php-xml","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-xml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-xml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-xml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-xml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nilportugues","download_url":"https://codeload.github.com/nilportugues/php-xml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252436240,"owners_count":21747467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","marshaller","php","php7","serializer","transformer","xml","xml-transformation"],"created_at":"2024-10-03T13:14:27.798Z","updated_at":"2025-05-05T03:44:28.841Z","avatar_url":"https://github.com/nilportugues.png","language":"PHP","funding_links":["https://paypal.me/nilportugues"],"categories":[],"sub_categories":[],"readme":"# XML Transformer\n\n[![Build Status](https://travis-ci.org/nilportugues/php-xml.svg?)](https://travis-ci.org/nilportugues/php-xml) \n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nilportugues/php-xml/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nilportugues/php-xml/?branch=master)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/dd8604ee-1ee2-4b0e-9cfe-b8a642308d20/mini.png?)](https://insight.sensiolabs.com/projects/dd8604ee-1ee2-4b0e-9cfe-b8a642308d20)\n[![Latest Stable Version](https://poser.pugx.org/nilportugues/xml/v/stable?)](https://packagist.org/packages/nilportugues/xml)\n[![Total Downloads](https://poser.pugx.org/nilportugues/xml/downloads?)](https://packagist.org/packages/nilportugues/xml) \n[![License](https://poser.pugx.org/nilportugues/xml/license?)](https://packagist.org/packages/nilportugues/xml) \n[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://paypal.me/nilportugues)\n\n## Installation\n\nUse [Composer](https://getcomposer.org) to install the package:\n\n```xml\n$ composer require nilportugues/xml\n```\n\n\n## Usage\nGiven a PHP Object, and a series of mappings, the **XML Transformer** will represent the given data as a XML object.\n\nFor instance, given the following piece of code, defining a Blog Post and some comments:\n\n```php\n$post = new Post(\n  new PostId(9),\n  'Hello World',\n  'Your first post',\n  new User(\n      new UserId(1),\n      'Post Author'\n  ),\n  [\n      new Comment(\n          new CommentId(1000),\n          'Have no fear, sers, your king is safe.',\n          new User(new UserId(2), 'Barristan Selmy'),\n          [\n              'created_at' =\u003e (new DateTime('2015/07/18 12:13:00'))-\u003eformat('c'),\n              'accepted_at' =\u003e (new DateTime('2015/07/19 00:00:00'))-\u003eformat('c'),\n          ]\n      ),\n  ]\n);\n```\n\nAnd a Mapping array for all the involved classes:\n\n```php\nuse NilPortugues\\Api\\Mapping\\Mapper;\n\n$mappings = [\n    [\n        'class' =\u003e Post::class,\n        'alias' =\u003e 'Message',\n        'aliased_properties' =\u003e [\n            'author' =\u003e 'author',\n            'title' =\u003e 'headline',\n            'content' =\u003e 'body',\n        ],\n        'hide_properties' =\u003e [\n\n        ],\n        'id_properties' =\u003e [\n            'postId',\n        ],\n        'urls' =\u003e [\n            'self' =\u003e 'http://example.com/posts/{postId}',\n            'comments' =\u003e 'http://example.com/posts/{postId}/comments'\n        ],\n    ],\n    [\n        'class' =\u003e PostId::class,\n        'alias' =\u003e '',\n        'aliased_properties' =\u003e [],\n        'hide_properties' =\u003e [],\n        'id_properties' =\u003e [\n            'postId',\n        ],\n        'urls' =\u003e [\n            'self' =\u003e 'http://example.com/posts/{postId}',\n        ],\n    ],\n    [\n        'class' =\u003e User::class,\n        'alias' =\u003e '',\n        'aliased_properties' =\u003e [],\n        'hide_properties' =\u003e [],\n        'id_properties' =\u003e [\n            'userId',\n        ],\n        'urls' =\u003e [\n            'self' =\u003e 'http://example.com/users/{userId}',\n            'friends' =\u003e 'http://example.com/users/{userId}/friends',\n            'comments' =\u003e 'http://example.com/users/{userId}/comments',\n        ],\n    ],\n    [\n        'class' =\u003e UserId::class,\n        'alias' =\u003e '',\n        'aliased_properties' =\u003e [],\n        'hide_properties' =\u003e [],\n        'id_properties' =\u003e [\n            'userId',\n        ],\n        'urls' =\u003e [\n            'self' =\u003e 'http://example.com/users/{userId}',\n            'friends' =\u003e 'http://example.com/users/{userId}/friends',\n            'comments' =\u003e 'http://example.com/users/{userId}/comments',\n        ],\n    ],\n    [\n        'class' =\u003e Comment::class,\n        'alias' =\u003e '',\n        'aliased_properties' =\u003e [],\n        'hide_properties' =\u003e [],\n        'id_properties' =\u003e [\n            'commentId',\n        ],\n        'urls' =\u003e [\n            'self' =\u003e 'http://example.com/comments/{commentId}',\n        ],\n    ],\n    [\n        'class' =\u003e CommentId::class,\n        'alias' =\u003e '',\n        'aliased_properties' =\u003e [],\n        'hide_properties' =\u003e [],\n        'id_properties' =\u003e [\n            'commentId',\n        ],\n        'urls' =\u003e [\n            'self' =\u003e 'http://example.com/comments/{commentId}',\n        ],\n    ],\n];\n\n$mapper = new Mapper($mappings);\n```\n\nCalling the transformer will output a **valid XML response** using the correct formatting:\n\n```php\nuse NilPortugues\\Api\\Xml\\XmlSerializer;\nuse NilPortugues\\Api\\Xml\\Http\\Message\\Response;\n\n$serializer = new XmlSerializer($mapper);\n$output = $serializer-\u003eserialize($post);\n\n//PSR7 Response with headers and content.\n$response = new Response($output);\n\nheader(\n    sprintf(\n        'HTTP/%s %s %s',\n        $response-\u003egetProtocolVersion(),\n        $response-\u003egetStatusCode(),\n        $response-\u003egetReasonPhrase()\n    )\n);\nforeach($response-\u003egetHeaders() as $header =\u003e $values) {\n    header(sprintf(\"%s:%s\\n\", $header, implode(', ', $values)));\n}\n\necho $response-\u003egetBody();\n```\n\n**Output:**\n\n\n```\nHTTP/1.1 200 OK\nCache-Control: private, max-age=0, must-revalidate\nContent-type: text/xml; charset=utf-8\n```\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cdata\u003e\n  \u003cpostId\u003e\u003c![CDATA[9]]\u003e\u003c/postId\u003e\n  \u003cheadline\u003e\u003c![CDATA[Hello World]]\u003e\u003c/headline\u003e\n  \u003cbody\u003e\u003c![CDATA[Your first post]]\u003e\u003c/body\u003e\n  \u003cauthor\u003e\n    \u003cuserId\u003e\u003c![CDATA[1]]\u003e\u003c/userId\u003e\n    \u003cname\u003e\u003c![CDATA[Post Author]]\u003e\u003c/name\u003e\n  \u003c/author\u003e\n  \u003ccomments\u003e\n    \u003cresource\u003e\n      \u003ccommentId\u003e\u003c![CDATA[1000]]\u003e\u003c/commentId\u003e\n      \u003cdates\u003e\n        \u003ccreated_at\u003e\u003c![CDATA[2015-07-18T12:13:00+02:00]]\u003e\u003c/created_at\u003e\n        \u003caccepted_at\u003e\u003c![CDATA[2015-07-19T00:00:00+02:00]]\u003e\u003c/accepted_at\u003e\n      \u003c/dates\u003e\n      \u003ccomment\u003e\u003c![CDATA[Have no fear, sers, your king is safe.]]\u003e\u003c/comment\u003e\n      \u003cuser\u003e\n        \u003cuserId\u003e\u003c![CDATA[2]]\u003e\u003c/userId\u003e\n        \u003cname\u003e\u003c![CDATA[Barristan Selmy]]\u003e\u003c/name\u003e\n      \u003c/user\u003e\n    \u003c/resource\u003e\n  \u003c/comments\u003e\n  \u003clinks\u003e\n    \u003clink rel=\"self\" href=\"http://example.com/posts/9\"/\u003e\n    \u003clink rel=\"comments\" href=\"http://example.com/posts/9/comments\"/\u003e\n  \u003c/links\u003e\n\u003c/data\u003e\n```\n\n## Quality\n\nTo run the PHPUnit tests at the command line, go to the tests directory and issue phpunit.\n\nThis 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/).\n\nIf you notice compliance oversights, please send a patch via [Pull Request](https://github.com/nilportugues/php-xml/pulls).\n\n\n\n## Contribute\n\nContributions to the package are always welcome!\n\n* Report any bugs or issues you find on the [issue tracker](https://github.com/nilportugues/php-xml/issues/new).\n* You can grab the source code at the package's [Git repository](https://github.com/nilportugues/php-xml).\n\n\n## Support\n\nGet in touch with me using one of the following means:\n\n - Emailing me at \u003ccontact@nilportugues.com\u003e\n - Opening an [Issue](https://github.com/nilportugues/php-xml/issues/new)\n\n## Authors\n\n* [Nil Portugués Calderó](http://nilportugues.com)\n* [The Community Contributors](https://github.com/nilportugues/php-xml/graphs/contributors)\n\n\n## License\nThe code base is licensed under the [MIT license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilportugues%2Fphp-xml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnilportugues%2Fphp-xml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilportugues%2Fphp-xml/lists"}