/** * @param array $array * @param bool $strict * @return Document * @throws FromArrayCompilationException */ public static function fromArray(array $array, $strict = true) { $document = new self(); if (array_key_exists('collection', $array)) { if (!is_array($array['collection'])) { throw new ExpectedArrayException('collection', gettype($array['collection'])); } try { $document->setCollection(Collection::fromArray($array['collection'], $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('collection', $e->getMessage()); } } if (array_key_exists('error', $array)) { if (!is_array($array['error'])) { throw new ExpectedArrayException('error', gettype($array['error'])); } try { $document->setError(Error::fromArray($array['error'])); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('error', $e->getMessage()); } } if (array_key_exists('template', $array)) { if (!is_array($array['template'])) { throw new ExpectedArrayException('template', gettype($array['template'])); } try { $document->setTemplate(Template::fromArray($array['template'], $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('template', $e->getMessage()); } } if (array_key_exists('queries', $array)) { if (!is_array($array['queries'])) { throw new ExpectedArrayException('queries', gettype($array['queries'])); } try { foreach ($array['queries'] as $key => $queryArray) { if (!is_array($queryArray)) { throw new ExpectedArrayException($key, gettype($queryArray)); } try { $document->getQueries()->add(Query::fromArray($queryArray, $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException($key, $e->getMessage()); } } } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('queries', $e->getMessage()); } } return $document; }
/** * @param array $array * @param bool $strict * @return Collection * @throws FromArrayCompilationException */ public static function fromArray(array $array, $strict = true) { $href = ''; $version = '1.0'; if (array_key_exists('href', $array)) { $href = $array['href']; } elseif ($strict) { throw new MissingArgumentException('href'); } if (array_key_exists('version', $array)) { $version = $array['version']; } $collection = new self($href, $version); if (array_key_exists('links', $array)) { if (!is_array($array['links'])) { throw new ExpectedArrayException('links', gettype($array['links'])); } try { foreach ($array['links'] as $key => $linkArray) { if (!is_array($linkArray)) { throw new ExpectedArrayException($key, gettype($linkArray)); } try { $collection->getLinks()->add(Link::fromArray($linkArray, $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException($key, $e->getMessage()); } } } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('links', $e->getMessage()); } } if (array_key_exists('items', $array)) { if (!is_array($array['items'])) { throw new ExpectedArrayException('items', gettype($array['items'])); } try { foreach ($array['items'] as $key => $itemArray) { if (!is_array($itemArray)) { throw new ExpectedArrayException($key, gettype($itemArray)); } try { $collection->getItems()->add(Item::fromArray($itemArray, $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException($key, $e->getMessage()); } } } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('items', $e->getMessage()); } } if (array_key_exists('queries', $array)) { if (!is_array($array['queries'])) { throw new ExpectedArrayException('queries', gettype($array['queries'])); } try { foreach ($array['queries'] as $key => $queryArray) { if (!is_array($queryArray)) { throw new ExpectedArrayException($key, gettype($queryArray)); } try { $collection->getQueries()->add(Query::fromArray($queryArray, $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException($key, $e->getMessage()); } } } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('queries', $e->getMessage()); } } if (array_key_exists('template', $array)) { if (!is_array($array['template'])) { throw new ExpectedArrayException('template', gettype($array['template'])); } try { $collection->setTemplate(Template::fromArray($array['template'], $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('template', $e->getMessage()); } } if (array_key_exists('error', $array)) { if (!is_array($array['error'])) { throw new ExpectedArrayException('error', gettype($array['error'])); } try { $collection->setError(Error::fromArray($array['error'])); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('error', $e->getMessage()); } } if (array_key_exists('paging', $array)) { if (!is_array($array['paging'])) { throw new ExpectedArrayException('paging', gettype($array['paging'])); } try { $collection->setPaging(Paging::fromArray($array['paging'], $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('paging', $e->getMessage()); } } return $collection; }