Hal 库

dev-master 2014-03-23 11:05 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:12:13 UTC


README

一个非常简单的 Hal 库,用于创建可以转换为 HAL 兼容有效载荷的对象。

链接

使用

创建响应的最高级资源。所有资源都需要一个 Hal\Link 实例作为构造参数,以及资源的类型。

$resource = new Resource(new Link('self', 'http://example.com/api/book/1'), 'book');

现在我们有了资源,我们可以添加额外的 HAL 属性,例如 _link

$resource->addLink(new Link('publisher', 'http://example.com/api/publisher/56'));

或者一个 _embedded 资源

$resource->addEmbedded(new Resource(
    new Link('self', 'http://exmaple.com/api/author/99'),
    'author',
    null,
    null,
    array(
        'name' => 'George Orwell',
        'born' => '25 June 1903',
        'died' => '21 January 1950'
    )
), 'author');

并添加一些 attributes 到资源

$resource->addAttributes(array(
    'title' => 'Animal Farm',
    'pages' => 112,
    'language' => 'English',
    'country' => 'United Kingdom'
));

一旦你创建了资源,你可以输出一个数组表示形式

$array = $resource->toArray();