{"id":15732802,"url":"https://github.com/nilportugues/php-json","last_synced_at":"2025-03-13T05:31:14.699Z","repository":{"id":36350936,"uuid":"40655700","full_name":"nilportugues/php-json","owner":"nilportugues","description":"JSON transformer outputting valid (PSR-7) API Responses.","archived":false,"fork":false,"pushed_at":"2016-06-16T21:23:41.000Z","size":35,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T18:57:03.183Z","etag":null,"topics":["api","json","json-api","json-transformation","jsonapi","marshaller","microservice","microservices","php","php7","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:42:14.000Z","updated_at":"2024-11-05T11:45:16.000Z","dependencies_parsed_at":"2022-09-06T06:01:32.894Z","dependency_job_id":null,"html_url":"https://github.com/nilportugues/php-json","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fphp-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nilportugues","download_url":"https://codeload.github.com/nilportugues/php-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243351188,"owners_count":20276894,"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","json","json-api","json-transformation","jsonapi","marshaller","microservice","microservices","php","php7","serializer","transformer"],"created_at":"2024-10-04T00:22:46.507Z","updated_at":"2025-03-13T05:31:14.293Z","avatar_url":"https://github.com/nilportugues.png","language":"PHP","funding_links":["https://paypal.me/nilportugues"],"categories":[],"sub_categories":[],"readme":"# JSON Transformer\n\n[![Build Status](https://travis-ci.org/nilportugues/php-json.svg)]\n(https://travis-ci.org/nilportugues/php-json) \n[![Coverage Status](https://coveralls.io/repos/nilportugues/json-transformer/badge.svg?branch=master\u0026service=github)]\n(https://coveralls.io/github/nilportugues/json-transformer?branch=master) \n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nilportugues/json-transformer/badges/quality-score.png?b=master)]\n(https://scrutinizer-ci.com/g/nilportugues/json-transformer/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/76f021fa-6559-4faf-a010-5dfb95cd70e2/mini.png)]\n(https://insight.sensiolabs.com/projects/76f021fa-6559-4faf-a010-5dfb95cd70e2) [![Latest Stable Version](https://poser.pugx.org/nilportugues/json/v/stable)]\n(https://packagist.org/packages/nilportugues/json) \n[![Total Downloads](https://poser.pugx.org/nilportugues/json/downloads)]\n(https://packagist.org/packages/nilportugues/json) \n[![License](https://poser.pugx.org/nilportugues/json/license)]\n(https://packagist.org/packages/nilportugues/json) \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```json\n$ composer require nilportugues/json\n```\n\n\n## Usage\nGiven a PHP Object, and a series of mappings, the **JSON Transformer** will represent the given data as a JSON 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 JSON response** using the correct formatting:\n\n```php\nuse NilPortugues\\Api\\Json\\JsonSerializer;\nuse NilPortugues\\Api\\Json\\Http\\Message\\Response;\n\n$serializer = new JsonSerializer($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: application/json; charset=utf-8\n```\n\n```json\n{\n  \"post_id\": 9,\n  \"headline\": \"Hello World\",\n  \"body\": \"Your first post\",\n  \"author\": {\n    \"user_id\": 1,\n    \"name\": \"Post Author\"\n  },\n  \"comments\": [\n    {\n      \"comment_id\": 1000,\n      \"dates\": {\n        \"created_at\": \"2015-07-18T12:13:00+02:00\",\n        \"accepted_at\": \"2015-07-19T00:00:00+02:00\"\n      },\n      \"comment\": \"Have no fear, sers, your king is safe.\",\n      \"user\": {\n        \"user_id\": 2,\n        \"name\": \"Barristan Selmy\"\n      }\n    }\n  ],\n  \"links\": {\n    \"self\": {\n      \"href\": \"http://example.com/posts/9\"\n    },\n    \"comments\": {\n      \"href\": \"http://example.com/posts/9/comments\"\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/json-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/json-transformer/issues/new).\n* You can grab the source code at the package's [Git repository](https://github.com/nilportugues/json-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/json-transformer/issues/new)\n - Using Gitter: [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/nilportugues/json-transformer?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)\n\n\n\n## Authors\n\n* [Nil Portugués Calderó](http://nilportugues.com)\n* [The Community Contributors](https://github.com/nilportugues/json-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-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnilportugues%2Fphp-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilportugues%2Fphp-json/lists"}