定制 level3/resource 二次开发

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

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

level3/resource

最新稳定版本:v0.0.1

Composer 安装命令:

composer require level3/resource

包简介

Level3 Resource is a library for representing resources and consuming in different hypermedia formats.

README 文档

README

Level3 Resource is a library for representing and consuming resources in different hypermedia formats.

A resource in a [HATEOAS API] (http://en.wikipedia.org/wiki/HATEOAS) must describe its own capabilities and interconnections, which is the third level of Three Levels of the REST Maturity Model

Why Hypermedia?

As you can read in the prologue of Designing Hypermedia APIs book:

Hypermedia APIs embrace the principles that make the web great: flexibility, standardization, and loose coupling to any given service. They take into account the principles of systems design enumerated by Roy Fielding in his thesis, but with a little less sytems theory jargon.

Hypermedia designs scale better, are more easily changed and promote decoupling and encapsulation, with all the benefits those things bring. On the downside, it is not necessarily the most latency-tolerant design, and caches can get stale if you're not careful. It may not be as efficient on an individual request level as other designs.

-- Steve Klabnik

Which Hypermedia specification should I use?

Hypermedia is being defined these days. Only the best APIs implement Hypermedia. Currently there is no de facto standard, so you must choose between the differents specifications.

Level3 Resource currenly implements or is planned to implement these specifications:

  • HAL: This is the most common and active. It has a JSON and a XML version.
  • Siren: Currently being defined. It implements some useful things like actions, classes, etc.
  • Collection+JSON: This is fully designed to be a CRUD oriented API.

Requirements

  • PHP 5.4.x
  • hampel/json >= 1.0

Installation

The recommended way to install Level3 Resource is through composer. You can see the package information on Packagist.

{
    "require": {
        "level3/resource": "dev-master"
    }
}

Examples

Writer

Basic Resource with Link as application/hal+json

use Level3\Resource\Link;
use Level3\Resource\Resource;
use Level3\Resource\Format\Writer\HAL;

$resource = new Resource();
$resource->setURI('/foo');
$resource->setLink('foo', new Link('/bar'));
$resource->setData([
    'foo' => 'bar',
    'baz' => 'qux'
]);

$writer = new HAL\JsonWriter(true);
echo $writer->execute($resource);
{
    "foo": "bar",
    "baz": "qux",
    "_links": {
        "self": {
            "href": "/foo"
        },
        "foo": {
            "href": "/bar"
        }
    }
}

Resource with embedded resources as aapplication/vnd.siren+json

use Level3\Resource\Link;
use Level3\Resource\Resource;
use Level3\Resource\Format\Writer\Siren;

$resource = new Resource();
$resource->setRepositoryKey('index');
$resource->setURI('/index?page=2');
$resource->setLink('prev', new Link('/index?page=1'));
$resource->setLink('next', new Link('/index?page=3'));
$resource->addData('count', 5);

$subresource = [];
foreach (range(1, 5) as $value) {
    $subresource = new Resource();
    $subresource->addData('value', $value);

    $subresources[] = $subresource;
}

$resource->addResources('subresources', $subresources);

$writer = new Siren\JsonWriter(true);
echo $writer->execute($resource);
{
    "class": [
        "index"
    ],
    "properties": {
        "count": 5
    },
    "entities": [
        {
            "rel": "subresources",
            "class": [
                "index",
                "subresources"
            ],
            "properties": {
                "value": 1
            }
        },
        ...
        {
            "rel": "subresources",
            "class": [
                "index",
                "subresources"
            ],
            "properties": {
                "value": 5
            }
        }
    ],
    "links": [
        {
            "rel": "self",
            "href": "/index?page=2"
        },
        {
            "rel": "prev",
            "href": "/index?page=1"
        },
        {
            "rel": "next",
            "href": "/index?page=3"
        }
    ]
}

Resource with linked resource as application/hal+xml

use Level3\Resource\Link;
use Level3\Resource\Resource;
use Level3\Resource\Format\Writer\HAL;

$author = new Resource();
$author->setURI('/john-doe');
$author->setTitle('John Doe');

$article = new Resource();
$article->setURI('/lorem-ipsum');
$article->addData('description', 'Lorem ipsum dolor sit amet ...');
$article->linkResource('author', $author);

$writer = new HAL\XMLWriter(true);
echo $writer->execute($article);
<?xml version="1.0"?>
<resource href="/lorem-ipsum">
  <description>Lorem ipsum dolor sit amet ...</description>
  <link rel="author" href="/john-doe" title="John Doe"/>
</resource>

Reader

Basic Resource with Link as application/hal+json

use Level3\Resource\Format\Reader\HAL;

$json = '{"foo":"bar","baz":"qux","_links":{"self":{"href":"/foo"},"foo":{"href":"/bar"}}}';

$reader = new HAL\JsonReader();
$resource = $reader->execute($json);
print_r($resource);
Level3\Resource\Resource Object
(
    [uri:protected] => /foo
    [links:protected] => Array
        (
            [foo] => Level3\Resource\Link Object
                (
                    [href:protected] => /bar
                )

        )

    [data:protected] => Array
        (
            [foo] => bar
            [baz] => qux
        )

)

Tests

Tests are in the tests folder. To run them, you need PHPUnit. Example:

$ phpunit --configuration phpunit.xml.dist

License

MIT, see LICENSE

统计信息

  • 总下载量: 40.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 1
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-11-18