kwattro/hypermedia-collection-json

PHP 的 Hypermedia Collection+Json 库

dev-master / 0.1.x-dev 2014-08-27 00:06 UTC

This package is not auto-updated.

Last update: 2024-09-10 08:18:09 UTC


README

! 进行中状态

链接到 Collection+Json 规范:https://github.com/collection-json/spec

安装

将库添加到 composer.json 文件中

{
    "require": {
        // your other deps,
        "kwattro/hypermedia-collection-json": "0.1@dev"
    }
}

用法

库的用法很简单

创建一个新的集合

require_once 'vendor/autoload.php';

use Kwattro\Hypermedia\CollectionJson\Collection;

$collection = new Collection();
$collection->addHref('http://example.com/api/movies');

向集合中添加项目

use Kwattro\Hypermedia\CollectionJson\Collection;
use Kwattro\Hypermedia\CollectionJson\Item;

$collection = new Collection();
$item = new Item();
$item->addData(array('name' => 'John', 'city' => 'Paradise City');
$item->addHref('http://example.com/user/1234/john');
$collection->addItem($item);

序列化和反序列化

为了序列化和反序列化对象,您只需调用 Serializer 的 create 静态方法

use Kwattro\Hypermedia\CollectionJson\Serializer;

// $myCollection = ... creating collection and items objects

$json = Serializer::create()->serialize($myCollection); // Returns a Json representation

// Deserializing :
$myObject = Serializer::create()->deserialize($json); // Returns a Collection object

测试

该库使用 phpspec

运行测试套件

bin/phpspec run

待办事项

  • 实现 JMS/Serializer(部分完成)
  • 完全符合 Hypermedia 规范