/** * Decode a application/hal+xml document into a Nocarrier\Hal object. * * @param Hal $hal * @param $data * @param int $depth * * @throws \RuntimeException * @static * @access public * @return \Nocarrier\Hal */ public static function fromXml(Hal $hal, $data, $depth = 0) { if (!$data instanceof \SimpleXMLElement) { try { $data = new \SimpleXMLElement($data); } catch (\Exception $e) { throw new \RuntimeException('The $data parameter must be valid XML'); } } $children = $data->children(); $links = clone $children->link; unset($children->link); $embedded = clone $children->resource; unset($children->resource); $hal->setUri((string) $data->attributes()->href); $hal->setData((array) $children); foreach ($links as $links) { if (!is_array($links)) { $links = array($links); } foreach ($links as $link) { list($rel, $href, $attributes) = self::extractKnownData($link); $hal->addLink($rel, $href, $attributes); } } if ($depth > 0) { foreach ($embedded as $embed) { list($rel, $href, $attributes) = self::extractKnownData($embed); $hal->addResource($rel, self::fromXml($embed, $depth - 1)); } } $hal->setShouldStripAttributes(false); return $hal; }
/** * Decode a application/hal+json document into a Nocarrier\Hal object. * * @param string $text * @param int $depth * @static * @access public * @return \Nocarrier\Hal */ public static function fromJson(Hal $hal, $text, $depth = 0) { list($uri, $links, $embedded, $data) = self::prepareJsonData($text); $hal->setUri($uri)->setData($data); self::addJsonLinkData($hal, $links); if ($depth > 0) { self::setEmbeddedResources($hal, $embedded, $depth); } $hal->setShouldStripAttributes(false); return $hal; }