Per the JSON-HAL specification, a link relation can reference a
single link or an array of links. By default, two or more links with
the same relation will be treated as an array of links. The $singular
flag will force links with the same relation to be overwritten. The
$plural flag will force links with only one relation to be treated
as an array of links. The $plural flag has no effect if $singular
is set to true.
/** * @param array $resourceInfo * @return Resource */ public function process(array $resourceInfo) { $resource = new Resource($resourceInfo['links']['self'], $resourceInfo['data']); unset($resourceInfo['links']['self']); foreach ($resourceInfo['links'] as $key => $href) { $resource->setLink(new Link($href, $key)); } if (isset($resourceInfo['embedded'])) { foreach ($resourceInfo['embedded'] as $rel) { foreach ($rel as $rel => $data) { $resource->setEmbedded($rel, new Resource($data['links']['self'], $data['data'])); } } } return $resource; }
public function testTemplatedLink() { $fixture = <<<'EOF' { "_links":{ "self":{ "href":"/dogs" }, "search":{ "href":"/dogs?q={text}", "templated": true } }, "_embedded":{ "dog":[ { "_links":{ "self":{ "href":"/dogs\/1" } }, "id":"1", "name":"tiber", "color":"black" }, { "_links":{ "self":{ "href":"/dogs\/2" } }, "id":"2", "name":"sally", "color":"white" }, { "_links":{ "self":{ "href":"/dogs\/3" } }, "id":"3", "name":"fido", "color":"gray" } ] } } EOF; $parent = new Resource('/dogs'); /* Add any relevent links */ $parent->setLink(new Link('/dogs?q={text}', 'search', null, null, null, true)); $dogs[1] = new Resource('/dogs/1'); $dogs[1]->setData(array('id' => '1', 'name' => 'tiber', 'color' => 'black')); $dogs[2] = new Resource('/dogs/2', array('id' => '2', 'name' => 'sally', 'color' => 'white')); $dogs[3] = new Resource('/dogs/3', array('id' => '3', 'name' => 'fido', 'color' => 'gray')); /* Add the embedded resources */ foreach ($dogs as $dog) { $parent->setEmbedded('dog', $dog); } $this->assertEquals(json_decode($fixture), json_decode((string) $parent)); }
public function testJsonNumericConversion() { $fixture = <<<'EOF' { "_links":{ "search":{ "href":"/dogs?q={text}" } }, "data": 98765432 } EOF; $fixture1 = <<<'EOF' { "_links":{ "search":{ "href":"/dogs?q={text}" } }, "data": "98765432" } EOF; $parent = new Resource('', array('data' => '98765432')); $parent->setLink(new Link('/dogs?q={text}', 'search')); // Default is off $this->assertEquals(json_decode($fixture1), json_decode((string) $parent)); $parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_ON); // Active On $this->assertEquals(json_decode($fixture), json_decode((string) $parent)); $parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_OFF); // Active Off $this->assertEquals(json_decode($fixture1), json_decode((string) $parent)); $parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_ON); // State Returned $this->assertEquals(json_decode($fixture), json_decode((string) $parent)); }