/** * Render a HAL document in JSON format * * @param array $options Rendering options. You can currently only set json_options (json_encode options) * * @return string The JSON representation of the HAL document */ public function render($options = array()) { if (isset($options['data_key'])) { $this->_dataKey = $options['data_key']; } if (isset($options['json_options'])) { $jsonOptions = $options['json_options']; } else { $jsonOptions = 0; } $serialiseThis = new \stdClass(); // Add links $collection = $this->_document->getLinks(); $serialiseThis->_links = new \stdClass(); foreach ($collection as $rel => $links) { if (!is_array($links)) { $serialiseThis->_links->{$rel} = $this->_getLink($links); } else { $serialiseThis->_links->{$rel} = array(); foreach ($links as $link) { array_push($serialiseThis->_links->{$rel}, $this->_getLink($link)); } } } // Add embedded documents $collection = $this->_document->getEmbedded(); if (!empty($collection)) { $serialiseThis->_embedded = new \stdClass(); foreach ($collection as $rel => $embeddeddocs) { $serialiseThis->_embedded->{$rel} = array(); if (!is_array($embeddeddocs)) { $embeddeddocs = array($embeddeddocs); } foreach ($embeddeddocs as $embedded) { $renderer = new static($embedded); array_push($serialiseThis->_embedded->{$rel}, $renderer->render($options)); } } } // Add data $data = $this->_document->getData(); if (is_object($data)) { if ($data instanceof DataModel) { $data = $data->toArray(); } else { $data = (array) $data; } if (!empty($data)) { foreach ($data as $k => $v) { $serialiseThis->{$k} = $v; } } } elseif (is_array($data)) { $serialiseThis->{$this->_dataKey} = $data; } return json_encode($serialiseThis, $jsonOptions); }
/** * @covers FOF30\Hal\Document::getLinks */ public function testGetLinks() { $data = array('test1' => 'one', 'test2' => 'two', 'testArray' => array('testUno' => 'uno', 'testDue' => 'Due')); $document = new Document($data); $myLink = new Link('http://www.example.com/foo.json', false, 'test', null, 'A test link'); $document->addLink('foo', $myLink); // Make sure trying to add links with replace=false adds, doesn't replace, links $myOtherLink = new Link('http://www.example.com/bar.json', false, 'test', null, 'Another test link'); $document->addLink('foo', $myOtherLink, false); $extraData = array('newData' => 'something'); $document->addData($extraData, false); $allLinks = $document->getLinks(); $this->assertInternalType('array', $allLinks, 'Line: ' . __LINE__ . '.'); $this->assertArrayHasKey('foo', $allLinks, 'Line: ' . __LINE__ . '.'); $links = $document->getLinks('foo'); $this->assertInternalType('array', $links, 'Line: ' . __LINE__ . '.'); $this->assertEquals($links, $allLinks['foo'], 'Line: ' . __LINE__ . '.'); $this->assertCount(2, $links, 'Line: ' . __LINE__ . '.'); $this->assertTrue($links[0] instanceof Link, 'Line: ' . __LINE__ . '.'); $this->assertEquals('http://www.example.com/foo.json', $links[0]->href, 'Line: ' . __LINE__ . '.'); $this->assertTrue($links[1] instanceof Link, 'Line: ' . __LINE__ . '.'); $this->assertEquals('http://www.example.com/bar.json', $links[1]->href, 'Line: ' . __LINE__ . '.'); }