定制 alsvanzelf/jsonapi 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

alsvanzelf/jsonapi

Composer 安装命令:

composer require alsvanzelf/jsonapi

包简介

Human-friendly library to implement JSON:API without needing to know the specification.

README 文档

README

A simple and human-friendly library for api servers (php serving json).

It allows you to generate json output according to the JSON:API v1.1 standard, while being easy to understand for people without knowledge of the jsonapi standard.

The JSON:API standard makes it easy for clients to fetch multiple resources in one call and understand the relations between them. Read more about it at jsonapi.org.

The library's code uses strict typing and is checked against a high standard in phpstan and rector. It is tested extensively with 99% coverage.

Installation

Use Composer require to get the latest stable version:

composer require alsvanzelf/jsonapi

The library requires php 8.2. Use the latest v2.x release for lower php versions.

Upgrading from v1 or v2

If you used v1 or v2 of this library, see UPGRADE_1_TO_2.md or UPGRADE_2_TO_3.md on how to upgrade.

Getting started

A small resource example

use alsvanzelf\jsonapi\ResourceDocument;

$document = new ResourceDocument(type: 'user', id: 42);
$document->add('name', 'Zaphod Beeblebrox');
$document->add('heads', 2);
$document->sendResponse();

Which will result in:

{
	"jsonapi": {
		"version": "1.1"
	},
	"data": {
		"type": "user",
		"id": "42",
		"attributes": {
			"name": "Zaphod Beeblebrox",
			"heads": 2
		}
	}
}

A collection of resources with relationships

use alsvanzelf\jsonapi\CollectionDocument;
use alsvanzelf\jsonapi\objects\ResourceObject;

$arthur      = new ResourceObject('user', 1);
$ford        = new ResourceObject('user', 2);
$zaphod      = new ResourceObject('user', 42);
$heartOfGold = new ResourceObject('starship', 2001);

$arthur->add('name', 'Arthur Dent');
$ford->add('name', 'Ford Prefect');
$zaphod->add('name', 'Zaphod Beeblebrox');
$heartOfGold->add('name', 'Heart of Gold');

$zaphod->addRelationship('drives', $heartOfGold);

$users    = [$arthur, $ford, $zaphod];
$document = CollectionDocument::fromResources(...$users);
$document->sendResponse();

Which will result in:

{
	"jsonapi": {
		"version": "1.1"
	},
	"data": [
		{
			"type": "user",
			"id": "1",
			"attributes": {
				"name": "Arthur Dent"
			}
		},
		{
			"type": "user",
			"id": "2",
			"attributes": {
				"name": "Ford Prefect"
			}
		},
		{
			"type": "user",
			"id": "42",
			"attributes": {
				"name": "Zaphod Beeblebrox"
			},
			"relationships": {
				"drives": {
					"data": {
						"type": "starship",
						"id": "2001"
					}
				}
			}
		}
	],
	"included": [
		{
			"type": "starship",
			"id": "2001",
			"attributes": {
				"name": "Heart of Gold"
			}
		}
	]
}

Turning an exception into jsonapi

use alsvanzelf\jsonapi\ErrorsDocument;

$exception = new Exception('That is not valid', 422);

$document = ErrorsDocument::fromException($exception);
$document->sendResponse();

Which will result in:

{
	"jsonapi": {
		"version": "1.1"
	},
	"errors": [
		{
			"status": "422",
			"code": "Exception",
			"meta": {
				"class": "Exception",
				"message": "That is not valid",
				"code": 422,
				"file": "README.md",
				"line": 137,
				"trace": []
			}
		}
	]
}

This can be useful for development. For production usage, you can better construct an ErrorsDocument with only specific values.

Using extensions and profiles

The Atomic Operations extension and the Cursor Pagination profile come packaged along. Any 3rd party of self-made extension can be applied with:

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;

class ExampleExtension implements ExtensionInterface {
	public function getOfficialLink(): string {
		return 'https://example.org/extension-documentation';
	}
	
	public function getNamespace(): string {
		return 'foo';
	}
}

$document = new ResourceDocument('user', 42);
$document->add('name', 'Zaphod Beeblebrox');

$extension = new ExampleExtension();
$document->applyExtension($extension);
$document->addExtensionMember($extension, 'bar', 'baz');

$document->sendResponse();

Which will result in:

{
    "foo:bar": "baz",
    "jsonapi": {
        "version": "1.1",
        "ext": [
            "https://example.org/extension-documentation"
        ]
    },
    "data": {
        "type": "user",
        "id": "42",
        "attributes": {
            "name": "Zaphod Beeblebrox"
        }
    }
}

A similar flow can be used for profiles.

Other examples

Examples for all kind of responses are in the /examples directory.

Features

This library supports v1.1 of the JSON:API specification.

It has support for generating & sending documents with:

  • single resources
  • resource collections
  • to-one and to-many relationships
  • errors (easily turning exceptions into jsonapi output)
  • v1.1 extensions and profiles
  • v1.1 @-members for JSON-LD and others

Also there's tools to help processing of incoming requests:

  • parse request options (include paths, sparse fieldsets, sort fields, pagination, filtering)
  • parse request documents for creating, updating and deleting resources and relationships

Next to custom extensions/profiles, the following official extensions/profiles are included:

Plans for the future include:

  • validate request options (#58)
  • validate request documents (#57)

Contributing

If you use the library, please ask questions or share what can be improved by creating an issue.

For bugs issues or Pull Requests are welcome!

See /script's README to steps to setup a local development environment.

Licence

MIT

alsvanzelf/jsonapi 适用场景与选型建议

alsvanzelf/jsonapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 157.13k 次下载、GitHub Stars 达 55, 最近一次更新时间为 2015 年 05 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 alsvanzelf/jsonapi 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 157.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 56
  • 点击次数: 14
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 55
  • Watchers: 2
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-26