Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nilportugues/php-json
JSON transformer outputting valid (PSR-7) API Responses.
https://github.com/nilportugues/php-json
api json json-api json-transformation jsonapi marshaller microservice microservices php php7 serializer transformer
Last synced: 19 days ago
JSON representation
JSON transformer outputting valid (PSR-7) API Responses.
- Host: GitHub
- URL: https://github.com/nilportugues/php-json
- Owner: nilportugues
- License: mit
- Created: 2015-08-13T11:42:14.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-16T21:23:41.000Z (over 8 years ago)
- Last Synced: 2024-10-05T00:22:38.847Z (about 1 month ago)
- Topics: api, json, json-api, json-transformation, jsonapi, marshaller, microservice, microservices, php, php7, serializer, transformer
- Language: PHP
- Homepage: http://nilportugues.com
- Size: 34.2 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON Transformer
[![Build Status](https://travis-ci.org/nilportugues/php-json.svg)]
(https://travis-ci.org/nilportugues/php-json)
[![Coverage Status](https://coveralls.io/repos/nilportugues/json-transformer/badge.svg?branch=master&service=github)]
(https://coveralls.io/github/nilportugues/json-transformer?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nilportugues/json-transformer/badges/quality-score.png?b=master)]
(https://scrutinizer-ci.com/g/nilportugues/json-transformer/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/76f021fa-6559-4faf-a010-5dfb95cd70e2/mini.png)]
(https://insight.sensiolabs.com/projects/76f021fa-6559-4faf-a010-5dfb95cd70e2) [![Latest Stable Version](https://poser.pugx.org/nilportugues/json/v/stable)]
(https://packagist.org/packages/nilportugues/json)
[![Total Downloads](https://poser.pugx.org/nilportugues/json/downloads)]
(https://packagist.org/packages/nilportugues/json)
[![License](https://poser.pugx.org/nilportugues/json/license)]
(https://packagist.org/packages/nilportugues/json)
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://paypal.me/nilportugues)## Installation
Use [Composer](https://getcomposer.org) to install the package:
```json
$ composer require nilportugues/json
```## Usage
Given a PHP Object, and a series of mappings, the **JSON Transformer** will represent the given data as a JSON object.For instance, given the following piece of code, defining a Blog Post and some comments:
```php
$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:
```php
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 JSON response** using the correct formatting:
```php
use NilPortugues\Api\Json\JsonSerializer;
use NilPortugues\Api\Json\Http\Message\Response;$serializer = new JsonSerializer($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: application/json; charset=utf-8
``````json
{
"post_id": 9,
"headline": "Hello World",
"body": "Your first post",
"author": {
"user_id": 1,
"name": "Post Author"
},
"comments": [
{
"comment_id": 1000,
"dates": {
"created_at": "2015-07-18T12:13:00+02:00",
"accepted_at": "2015-07-19T00:00:00+02:00"
},
"comment": "Have no fear, sers, your king is safe.",
"user": {
"user_id": 2,
"name": "Barristan Selmy"
}
}
],
"links": {
"self": {
"href": "http://example.com/posts/9"
},
"comments": {
"href": "http://example.com/posts/9/comments"
}
}
}
```## 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/json-transformer/pulls).
## Contribute
Contributions to the package are always welcome!
* Report any bugs or issues you find on the [issue tracker](https://github.com/nilportugues/json-transformer/issues/new).
* You can grab the source code at the package's [Git repository](https://github.com/nilportugues/json-transformer).## Support
Get in touch with me using one of the following means:
- Emailing me at
- Opening an [Issue](https://github.com/nilportugues/json-transformer/issues/new)
- Using Gitter: [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/nilportugues/json-transformer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)## Authors
* [Nil Portugués Calderó](http://nilportugues.com)
* [The Community Contributors](https://github.com/nilportugues/json-transformer/graphs/contributors)## License
The code base is licensed under the [MIT license](LICENSE).