mvieira/collection-json
Composer 安装命令:
composer require mvieira/collection-json
包简介
PHP implementation of the Collection+JSON Media Type
README 文档
README
PHP implementation of the Collection+JSON Media Type
Specification:
Installation
CollectionJson requires php >= 7.0
Install CollectionJson with Composer
{
"require": {
"mvieira/collection-json": "dev-master"
}
}
or
$ composer require mvieira/collection-json
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.
Documentation
Creating a collection
$collection = (new Collection()) ->withItem((new Item('https://example.co/item/1')) ->withDataSet([ new Data('data 1'), new Data('data 2', 'value 2') ]) ->withLink( new Link('https://example.co/item/1', Relation::ITEM) ) ); print json_encode($collection);
{
"collection": {
"version": "1.0",
"items": [
{
"data": [
{
"name": "data 1",
"value": null
},
{
"name": "data 2",
"value": "value 2"
}
],
"href": "http:\/\/example.com\/item\/1",
"links": [
{
"href": "https:\/\/example.co\/item\/1",
"rel": "item",
"render": "link"
}
]
}
]
}
}
Creating an entity
All entities Collection, Data, Error, Item, Link, Query, Template can be created by using the static method fromArray...
$data = Data::fromArray([ 'name' => 'email', 'value' => 'hello@example.co' ]);
...or by using the accessors (Note that entities are immutable)
$data = (new Data('email')) ->withValue('hello@example.co');
...or via the constructor
$data = new Data('email', 'hello@example.co');
Printing the data
Note: Apart from the data's value property, which allows having a NULL value (see. specification), All NULL properties and empty arrays will be excluded from the JSON and Array representation.
Printing a JSON representation
All entities implement the JsonSerializable interface,
you can therefore call at any time the method json_encode().
print json_encode($collection);
{
"collection": {
"version": "1.0",
"items": [
...
],
"links": [
...
]
}
}
Printing an Array representation
All entities implement a custom interface named ArrayConvertible, so you can call at any time the method toArray().
This method will be called recursively on all nested entities.
print_r($collection->toArray());
Array ( [collection] => Array ( [version] => 1.0 [items] => Array ... [links] => Array ... ) )
Wrapping
The CollectionJson\Entity\Collection entity will be wrapped...
echo json_encode($collection);
{
"collection": {
"version": "1.0"
}
}
...however others entities will not be wrapped when they are converted in a JSON or an Array.
$template = new Template(); echo json_encode($template);
{
"data": [
...
]
}
But you can wrap the json or the array representation by calling the method wrap()
$template->wrap(); echo json_encode($template);
{
"template": {
"data": [
...
]
}
}
Examples
Examples are available in the directory ./examples/, you can execute them on the command line by running:
$ make examples
Or separately
$ php ./examples/client-collection.php
Working with data and links
In order to work with CollectionJson Arrays Data, Links, the API provides 2 interfaces that implement a similar logic.
- The interface
DataAwareimplemented byItem,QueryandTemplateentities, provides the methodswithData,withoutData,withDataSet,getDataSet,getFirstDataandgetLastData - The interface
LinkAwareimplemented byCollectionandItementities, provides the methodswithLink,withoutLink,withLinkSet,getLinks,getFirstLinkandgetLastLink
They allows you to add the corresponding entities to objects that implement them.
// this... $item = (new Item('https://example.co/item/1')) ->withData([ 'name' => 'email', 'value' => 'email value' ]); // ...is similar to $data = Data::fromArray([ 'name' => 'email', 'value' => 'email value' ]); $item = (new Item('https://example.co/item/1')) ->withData($data); // and that... $item = (new Item('https://example.co/item/1')) ->withDataSet([ new Data('email', 'hello@example.co'), new Data('tel', '0000000000') ]); // ...is similar to $data1 = Data::fromArray([ 'name' => 'email', 'value' => 'hello@example.co' ]); $data2 = Data::fromArray([ 'name' => 'tel', 'value' => '0000000000' ]); $item = (new Item('https://example.co/item/1')) ->withDataSet([ $data1, $data2 ]);
Validation
It is now possible to validate the data entering your API by using the Symfony validator.
use CollectionJson\Validator\Dataset as DatasetValidator; use Symfony\Component\Validator\Constraints; $constraints = [ 'id' => [ new Constraints\NotBlank(), ], 'url' => [ new Constraints\NotBlank(), new Constraints\Url(), ], 'email' => [ new Constraints\NotBlank(), new Constraints\Email(), ], ]; $template = (new Template()) ->withData(new Data('id', '123')) ->withData(new Data('url', 'http://example.co')) ->withData(new Data('email', 'test@example.co')); $errors = (new DatasetValidator()) ->validate($template->getDataSet(), $constraints);
It will return the list of errors.
mvieira/collection-json 适用场景与选型建议
mvieira/collection-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「http」 「rest」 「restful」 「hypermedia」 「HATEOAS」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mvieira/collection-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mvieira/collection-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mvieira/collection-json 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
统计信息
- 总下载量: 69
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-03