lemonphp/json-api

PHP7实现的JSON API规范

dev-master 2017-07-03 07:48 UTC

This package is auto-updated.

Last update: 2024-09-08 07:44:54 UTC


README

Build Status Coverage Status Code Climate Issue Count

PHP7实现的 JSON API规范

要求

  • php >= 7.0

安装

$ composer require lemonphp/json-api

用法

示例代码

<?php

use Lemon\JsonApi\Document;
use Lemon\JsonApi\Document\Resource\Relationship\Linkage;
use Lemon\JsonApi\Document\Resource\Relationship\Relationship;
use Lemon\JsonApi\Document\Resource\ResourceIdentifier;
use Lemon\JsonApi\Document\Resource\ResourceObject;

include_once 'vendor/autoload.php';

$author = Relationship::fromLinkage(
    Linkage::fromSingleIdentifier(
        new ResourceIdentifier('people', '9')
    )
);
$author->setLink('self', '/articles/1/relationships/author');
$author->setLink('related', '/articles/1/author');

$article = new ResourceObject('articles', '1');
$article->setAttribute('title', 'Hello word');
$article->setRelationship('author', $author);

$doc = Document::fromResource($article);
echo json_encode($doc, JSON_PRETTY_PRINT);

输出

{
    "data": {
        "type": "articles",
        "id": "1",
        "attributes": {
            "title": "Hello word"
        },
        "relationships": {
            "author": {
                "data": {
                    "type": "people",
                    "id": "9"
                },
                "links": {
                    "self": "\/articles\/1\/relationships\/author",
                    "related": "\/articles\/1\/author"
                }
            }
        }
    }
}

变更日志

CHANGELOG.md 中查看所有变更日志

贡献

所有代码贡献必须通过拉取请求,并在合并前由核心开发者批准。这是为了确保对所有代码的正确审查。

分支项目,创建功能分支,并发送拉取请求。

为了确保代码库的一致性,你应该确保代码遵循 PSR-2

如果你想要帮助查看 问题列表

许可证

本项目采用MIT许可证发布。
版权所有 © 2017 LemonPHP 团队。