/** * Returns the data for output and sets the appropriate headers * @param \Nocarrier\Hal $hal * @return string */ public function renderOutput(\Nocarrier\Hal $hal) { $this->sendHeaders(); if ($this->getSystemErrors()) { $system_errors = array(); foreach ($this->getSystemErrors() as $key => $value) { $system_errors[$key] = $this->m62Lang($key); } $hal->setData($hal->getData() + array('_system_errors' => $system_errors)); } if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'xml') !== false) { header('Content-Type: application/hal+xml'); return $hal->asXml(true); } else { header('Content-Type: application/hal+json'); return $hal->asJson(true); } }
public function testSetResourceXmlResponse() { $hal = new Hal('http://example.com/'); $res = new Hal('/resource/1', array('field1' => 'value1', 'field2' => 'value2')); $hal->setResource('resource', $res); $result = new \SimpleXmlElement($hal->asXml()); $this->assertEquals('/resource/1', $result->resource->attributes()->href); $this->assertEquals('value1', $result->resource->field1); $this->assertEquals('value2', $result->resource->field2); }
public function testLinksWithAttributesUnserialiseCorrectlyXml() { $x = new Hal('/'); $x->addCurie('x:test', 'http://test'); $this->assertEquals($x->asXml(), Hal::fromXml($x->asXml())->asXml()); }
public function testAttributesInXmlRepresentation() { $hal = new Hal('/', array('error' => array('@id' => 6, '@xml:lang' => 'en', 'message' => 'This is a message'))); $xml = new \SimpleXMLElement($hal->asXml()); $this->assertEquals(6, (string) $xml->error->attributes()->id); $this->assertEquals('en', (string) $xml->error->attributes()->lang); $this->assertEquals('This is a message', (string) $xml->error->message); $json = json_decode($hal->asJson(true)); $this->markTestIncomplete(); $this->assertEquals(6, $json->error->id); }