serafim/json-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

serafim/json-api

Composer 安装命令:

composer require serafim/json-api

包简介

JSON-API responses in PHP

README 文档

README

Latest Stable Version Latest Stable Version Latest Unstable Version Total Downloads License MIT

JSON-API responses in PHP.

Works with version 1.0 of the spec.

⚠️ The project is fork https://github.com/tobyzerner/json-api-php with support PHP 8.0+

Install

via Composer:

composer require serafim/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\Resource represents a resource object
  • Tobscure\JsonApi\Collection represents 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.

serafim/json-api 适用场景与选型建议

serafim/json-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.41k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「api」 「standard」 「jsonapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 serafim/json-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 serafim/json-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 serafim/json-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 81
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-27