/** * 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; }
/** * @param Hal $hal * @param array $links */ private static function addJsonLinkData($hal, $links) { foreach ($links as $rel => $links) { if (!isset($links[0]) or !is_array($links[0])) { $links = array($links); } foreach ($links as $link) { $href = $link['href']; unset($link['href']); $hal->addLink($rel, $href, $link); } } }
/** * Decode a application/hal+xml document into a Nocarrier\Hal object. * * @param string $text * @static * @access public * @return \Nocarrier\Hal */ public static function fromXml($text) { $data = new \SimpleXMLElement($text); $children = $data->children(); $links = clone $children->link; unset($children->link); $embedded = clone $children->resource; unset($children->resource); $hal = new Hal($data->attributes()->href, (array) $children); foreach ($links as $links) { if (!is_array($links)) { $links = array($links); } foreach ($links as $link) { $attributes = (array) $link->attributes(); $attributes = $attributes['@attributes']; $rel = $attributes['rel']; $href = $attributes['href']; unset($attributes['rel'], $attributes['href']); $hal->addLink($rel, $href, $attributes); } } return $hal; }