{"id":15685672,"url":"https://github.com/nilportugues/php-api-transformer","last_synced_at":"2025-06-20T06:06:09.978Z","repository":{"id":36350971,"uuid":"40655735","full_name":"nilportugues/php-api-transformer","owner":"nilportugues","description":"Base library providing the core functionality for API transformation.","archived":false,"fork":false,"pushed_at":"2017-04-07T14:54:35.000Z","size":86,"stargazers_count":9,"open_issues_count":3,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-08T22:09:00.522Z","etag":null,"topics":["api","api-transformer","hal","jsend","json","marshaller","microservice","microservices","php","php7","serialization","serializer","transformer"],"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":"2015-08-13T11:43:07.000Z","updated_at":"2025-02-16T04:19:43.000Z","dependencies_parsed_at":"2022-09-06T03:50:50.812Z","dependency_job_id":null,"html_url":"https://github.com/nilportugues/php-api-transformer","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"purl":"pkg:github/nilportugues/php-api-transformer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-transformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-transformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-transformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-transformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nilportugues","download_url":"https://codeload.github.com/nilportugues/php-api-transformer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-api-transformer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260891143,"owners_count":23077909,"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":["api","api-transformer","hal","jsend","json","marshaller","microservice","microservices","php","php7","serialization","serializer","transformer"],"created_at":"2024-10-03T17:28:42.531Z","updated_at":"2025-06-20T06:06:04.964Z","avatar_url":"https://github.com/nilportugues.png","language":"PHP","funding_links":["https://paypal.me/nilportugues"],"categories":[],"sub_categories":[],"readme":"# API Transformer\n\n[![Build Status](https://travis-ci.org/nilportugues/php-api-transformer.svg)](https://travis-ci.org/nilportugues/php-api-transformer) \n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nilportugues/api-transformer/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nilportugues/api-transformer/?branch=master)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/b4e5056d-c552-407e-ae21-2da685e07c06/mini.png)](https://insight.sensiolabs.com/projects/b4e5056d-c552-407e-ae21-2da685e07c06)\n[![Latest Stable Version](https://poser.pugx.org/nilportugues/api-transformer/v/stable)](https://packagist.org/packages/nilportugues/api-transformer)\n[![Total Downloads](https://poser.pugx.org/nilportugues/api-transformer/downloads)](https://packagist.org/packages/nilportugues/api-transformer)\n[![License](https://poser.pugx.org/nilportugues/api-transformer/license)](https://packagist.org/packages/nilportugues/api-transformer)\n[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://paypal.me/nilportugues)\n\n## Purpose\nThis library provides the core functionality for API transformation and is a base library for many other packages. \n\nBy itself it's not usable at all. Check the projects using it below.\n\n## Used by\n\nCurrently the following transformers make use of this library as foundation:\n\n- [nilportugues/json](https://github.com/nilportugues/json-transformer)\n- [nilportugues/jsend](https://github.com/nilportugues/jsend-transformer)\n- [nilportugues/hal](https://github.com/nilportugues/hal-transformer)\n- [nilportugues/json-api](https://github.com/nilportugues/jsonapi-transformer)\n\n\n\n## Installation\n\nUse [Composer](https://getcomposer.org) to install the package:\n\n```json\n$ composer require nilportugues/api-transformer\n```\n\n\n## How it works\n\nLoading must be done using the `Mapper` class, as it expects an array containing a defined structure, or a class name implementing `ApiMapping`. \n\nThere are 2 styles for flexibility when integrating the library into PHP frameworks. \n\nWhile I discourage having 2 styles for mapping it is well possible to have them side by side in the very same configuration file. The `Mapper` class that loads all Mappings does internal transformation for both, so client does not have to worry. \n\n### Usage\n\n```php\nuse NilPortugues\\Api\\Mapping\\Mapper;\nuse NilPortugues\\AcmeProject\\Infrastructure\\Api\\Mappings\\PostApiMapping;\n\n$arrayConfig = include 'mappings.php';\n$classConfig = [\n\tPostApiMapping::class,\n];\n\n$mappings = array_merge($classConfig, $arrayConfig);\n\n//Now $mapper can be passed to a Transformer.\n$mapper = new Mapper($mappings);\n```\n\n\u003cbr\u003e\n\n## Creating the Mapping files\n\n### Implementing ApiMapping (Prefered method)\n\nTo create a Mapping you may implement the following interfaces:\n\n- `ApiMapping`: transform your data into plain JSON for API consumtion or the JSend API format.\n- `JsonApiMapping` to transform your data into the JSONAPI 1.0 standard.\n- `HalMapping` to transform your data to HAL+JSON and HAL+XML API standards.\n\n\nAs expected you may implement many interfaces to support multiple API formats.\n\n\n```php\n\u003c?php\n\nnamespace NilPortugues\\AcmeProject\\Infrastructure\\Api\\Mappings;\n\nuse NilPortugues\\AcmeProject\\Blog\\Domain\\Post;\nuse NilPortugues\\Api\\Mappings\\HalMapping;\nuse NilPortugues\\Api\\Mappings\\JsonApiMapping;\n\nclass PostApiMapping implements JsonApiMapping, HalMapping\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function getClass() : string\n    {\n        return Post::class;\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getAlias() : string\n    {\n        return 'Posting'; //If none is used 'Post' will be used instead.\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getAliasedProperties() : array\n    {\n        return [\n            'title' =\u003e 'headline',\n            'content' =\u003e 'body',\n        ];\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getHideProperties() : array\n    {\n        return [\n           'comments',\n       ];\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getIdProperties() : array\n    {\n        return [\n            'postId',\n        ];\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getUrls() : array\n    {\n        return [\n          // Mandatory\n          'self' =\u003e 'http://example.com/posts/{postId}',\n          // Optional\n          'comments' =\u003e 'http://example.com/posts/{postId}/comments',\n        ];\n    }\n\n    /**\n     * Returns an array of curies.\n     *\n     * @return array\n     */\n    public function getCuries() : array\n    {\n        return [\n            'name' =\u003e 'example',\n            'href' =\u003e 'http://example.com/docs/rels/{rel}',\n        ];\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getRelationships() : array\n    {\n        return [\n            'author' =\u003e [\n                'related' =\u003e 'http://example.com/posts/{postId}/author',\n                'self' =\u003e 'http://example.com/posts/{postId}/relationships/author',\n            ],\n        ];\n    }\n}\n\n```\n\n### Mapping using an array\n\n```php\n// mappings.php\n\nreturn  [\n  [\n      'class' =\u003e Post::class,\n      'alias' =\u003e 'Posting', //If none is used 'Post' will be used instead.\n      'aliased_properties' =\u003e [\n          'title' =\u003e 'headline',\n          'content' =\u003e 'body',\n      ],\n      'hide_properties' =\u003e [\n\t\t  'comments',\n      ],\n      'id_properties' =\u003e [\n          'postId',\n      ],\n      'urls' =\u003e [\n          // Mandatory\n          'self' =\u003e 'http://example.com/posts/{postId}',\n          // Optional\n          'comments' =\u003e 'http://example.com/posts/{postId}/comments',\n      ],\n      // (Optional) Used by HAL+JSON / HAL+XML\n      'curies' =\u003e [\n          'name' =\u003e 'example',\n          'href' =\u003e 'http://example.com/docs/rels/{rel}',\n      ],\n      // (Optional) Used by JSONAPI\n      'relationships' =\u003e [\n          'author' =\u003e [\n            'related' =\u003e 'http://example.com/posts/{postId}/author',\n            'self' =\u003e 'http://example.com/posts/{postId}/relationships/author',\n          ],\n      ]\n  ],\n];\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/api-transformer/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/api-transformer/issues/new).\n* You can grab the source code at the package's [Git repository](https://github.com/nilportugues/api-transformer).\n\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/api-transformer/issues/new)\n\n\n\n## Authors\n\n* [Nil Portugués Calderó](http://nilportugues.com)\n* [The Community Contributors](https://github.com/nilportugues/api-transformer/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-api-transformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnilportugues%2Fphp-api-transformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilportugues%2Fphp-api-transformer/lists"}