nilportugues/jsend
Composer 安装命令:
composer require nilportugues/jsend
包简介
JSend API transformer outputting valid API responses.
README 文档
README
[
]
(https://insight.sensiolabs.com/projects/2ee8556c-d480-4f6f-a645-0ee18c271867)
[
]
(https://packagist.org/packages/nilportugues/jsend)
[
]
(https://packagist.org/packages/nilportugues/jsend)
Installation
Use Composer to install the package:
$ composer require nilportugues/jsend
Usage
Given a PHP Object, and a series of mappings, the JSend 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:
$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 JSON response using the correct formatting:
use NilPortugues\Api\JSend\JSendTransformer; use NilPortugues\Api\JSend\Http\Message\Response; use NilPortugues\Serializer\DeepCopySerializer; $transformer = new JSendTransformer($mapper); //Output transformation $serializer = new DeepCopySerializer($transformer); $serializer->setSelfUrl('http://example.com/posts/9'); $serializer->setNextUrl('http://example.com/posts/10'); $serializer->addMeta('author',[['name' => 'Nil Portugués Calderó', 'email' => 'contact@nilportugues.com']]); $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
{
"status": "success",
"data": {
"post_id": 9,
"title": "Hello World",
"content": "Your first post",
"author": {
"user_id": 1,
"name": "Post Author"
},
"comments": [
{
"comment_id": 1000,
"dates": {
"created_at": "2015-07-18T12:13:00+00:00",
"accepted_at": "2015-07-19T00:00:00+00:00"
},
"comment": "Have no fear, sers, your king is safe.",
"user": {
"user_id": 2,
"name": "Barristan Selmy"
}
}
]
},
"links": {
"self": {
"href": "http://example.com/post/9"
},
"next": {
"href": "http://example.com/post/10"
},
"comments": {
"href": "http://example.com/post/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, PSR-2, PSR-4 and PSR-7.
If you notice compliance oversights, please send a patch via Pull Request.
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Support
Get in touch with me using one of the following means:
- Emailing me at contact@nilportugues.com
- Opening an Issue
- Using Gitter:
Authors
License
The code base is licensed under the MIT license.
nilportugues/jsend 适用场景与选型建议
nilportugues/jsend 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 468 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「response」 「serializer」 「transformer」 「jsend」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nilportugues/jsend 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nilportugues/jsend 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nilportugues/jsend 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
HTTP request logger middleware for Laravel
A PSR-7 compatible library for making CRUD API endpoints
Class to generate a standard structure for api json responses
统计信息
- 总下载量: 468
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-14