tobscure/json-api
最新稳定版本:v0.4.1
Composer 安装命令:
composer require tobscure/json-api
包简介
JSON-API responses in PHP
README 文档
README
JSON-API responses in PHP.
Works with version 1.0 of the spec.
⚠️ The project is no longer maintained. Check out tobyzerner/json-api-server instead.
Install
via Composer:
composer require tobscure/json-api
Usage
use Tobscure\JsonApi\Document; use Tobscure\JsonApi\Collection; // Create a new collection of posts, and specify relationships to be included. $collection = (new Collection($posts, new PostSerializer)) ->with(['author', 'comments']); // Create a new JSON-API document with that collection as the data. $document = new Document($collection); // Add metadata and links. $document->addMeta('total', count($posts)); $document->addLink('self', 'http://example.com/api/posts'); // Output the document as JSON. echo json_encode($document);
Elements
The JSON-API spec describes resource objects as objects containing information about a single resource, and collection objects as objects containing information about many resources. In this package:
Tobscure\JsonApi\Resourcerepresents a resource objectTobscure\JsonApi\Collectionrepresents a collection object
Both Resources and Collections are termed as Elements. In conceptually the same way that the JSON-API spec describes, a Resource may have relationships with any number of other Elements (Resource for has-one relationships, Collection for has-many). Similarly, a Collection may contain many Resources.
A JSON-API Document may contain one primary Element. The primary Element will be recursively parsed for relationships with other Elements; these Elements will be added to the Document as included resources.
Sparse Fieldsets
You can specify which fields (attributes and relationships) are to be included on an Element using the fields method. You must provide a multidimensional array organized by resource type:
$collection->fields(['posts' => ['title', 'date']]);
Serializers
A Serializer is responsible for building attributes and relationships for a certain resource type. Serializers must implement Tobscure\JsonApi\SerializerInterface. An AbstractSerializer is provided with some basic functionality. At a minimum, a serializer must specify its type and provide a method to transform attributes:
use Tobscure\JsonApi\AbstractSerializer; class PostSerializer extends AbstractSerializer { protected $type = 'posts'; public function getAttributes($post, array $fields = null) { return [ 'title' => $post->title, 'body' => $post->body, 'date' => $post->date ]; } }
By default, a Resource object's id attribute will be set as the id property on the model. A serializer can provide a method to override this:
public function getId($post) { return $post->someOtherKey; }
Relationships
The AbstractSerializer allows you to define a public method for each relationship that exists for a resource. A relationship method should return a Tobscure\JsonApi\Relationship instance.
public function comments($post) { $element = new Collection($post->comments, new CommentSerializer); return new Relationship($element); }
By default, the AbstractSerializer will convert relationship names from kebab-case and snake_case into a camelCase method name and call that on the serializer. If you wish to customize this behaviour, you may override the getRelationship method:
public function getRelationship($model, $name) { // resolve Relationship called $name for $model }
Meta & Links
The Document, Resource, and Relationship classes allow you to add meta information:
$document = new Document; $document->addMeta('key', 'value'); $document->setMeta(['key' => 'value']);
They also allow you to add links in a similar way:
$resource = new Resource($data, $serializer); $resource->addLink('self', 'url'); $resource->setLinks(['key' => 'value']);
You can also easily add pagination links:
$document->addPaginationLinks( 'url', // The base URL for the links [], // The query params provided in the request 40, // The current offset 20, // The current limit 100 // The total number of results );
Serializers can provide links and/or meta data as well:
use Tobscure\JsonApi\AbstractSerializer; class PostSerializer extends AbstractSerializer { // ... public function getLinks($post) { return ['self' => '/posts/' . $post->id]; } public function getMeta($post) { return ['some' => 'metadata for ' . $post->id]; } }
Note: Links and metadata of the resource overrule ones with the same key from the serializer!
Parameters
The Tobscure\JsonApi\Parameters class allows you to easily parse and validate query parameters in accordance with the specification.
use Tobscure\JsonApi\Parameters; $parameters = new Parameters($_GET);
getInclude
Get the relationships requested for inclusion. Provide an array of available relationship paths; if anything else is present, an InvalidParameterException will be thrown.
// GET /api?include=author,comments $include = $parameters->getInclude(['author', 'comments', 'comments.author']); // ['author', 'comments']
getFields
Get the fields requested for inclusion, keyed by resource type.
// GET /api?fields[articles]=title,body $fields = $parameters->getFields(); // ['articles' => ['title', 'body']]
getSort
Get the requested sort criteria. Provide an array of available fields that can be sorted by; if anything else is present, an InvalidParameterException will be thrown.
// GET /api?sort=-created,title $sort = $parameters->getSort(['title', 'created']); // ['created' => 'desc', 'title' => 'asc']
getLimit and getOffset
Get the offset number and the number of resources to display using a page- or offset-based strategy. getLimit accepts an optional maximum. If the calculated offset is below zero, an InvalidParameterException will be thrown.
// GET /api?page[number]=5&page[size]=20 $limit = $parameters->getLimit(100); // 20 $offset = $parameters->getOffset($limit); // 80 // GET /api?page[offset]=20&page[limit]=200 $limit = $parameters->getLimit(100); // 100 $offset = $parameters->getOffset(); // 20
Error Handling
You can transform caught exceptions into JSON-API error documents using the Tobscure\JsonApi\ErrorHandler class. You must register the appropriate Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface instances.
try { // API handling code } catch (Exception $e) { $errors = new ErrorHandler; $errors->registerHandler(new InvalidParameterExceptionHandler); $errors->registerHandler(new FallbackExceptionHandler); $response = $errors->handle($e); $document = new Document; $document->setErrors($response->getErrors()); return new JsonResponse($document, $response->getStatus()); }
Contributing
Feel free to send pull requests or create issues if you come across problems or have great ideas. Any input is appreciated!
Running Tests
$ phpunit
License
This code is published under the The MIT License. This means you can do almost anything with it, as long as the copyright notice and the accompanying license file is left intact.
tobscure/json-api 适用场景与选型建议
tobscure/json-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.55M 次下载、GitHub Stars 达 432, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「standard」 「jsonapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tobscure/json-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tobscure/json-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tobscure/json-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
Adds the EDTF data type to Wikibase
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
CodeSniffer ruleset for the ZettaCMS coding standard
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 1.55M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 434
- 点击次数: 15
- 依赖项目数: 16
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知