/** * @param Data $object * @return DataGroup */ public function add(Data $object) { $duplicates = []; $this->group->each(function (Data $data) use($duplicates, $object) { if ($data->getName() === $object->getName()) { $duplicates[] = $data; } }); foreach ($duplicates as $duplicate) { $this->remove($duplicate); } $this->group->add($object); return $this; }
/** * @param array $array * @param bool $strict * @return Template * @throws FromArrayCompilationException */ public static function fromArray(array $array, $strict = true) { $template = new self(); $data = []; if (array_key_exists('data', $array)) { $data = $array['data']; } elseif ($strict) { throw new MissingArgumentException('data'); } try { foreach ($data as $key => $dataArray) { if (!is_array($dataArray)) { throw new ExpectedArrayException($key, gettype($dataArray)); } try { $template->getData()->add(Data::fromArray($dataArray, $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException($key, $e->getMessage()); } } } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('data', $e->getMessage()); } return $template; }
/** * @param array $array * @param bool $strict * @return Query * @throws FromArrayCompilationException */ public static function fromArray(array $array, $strict = true) { $href = ''; $rel = ''; $name = ''; $prompt = ''; if (array_key_exists('href', $array)) { $href = $array['href']; } elseif ($strict) { throw new MissingArgumentException('href'); } if (array_key_exists('rel', $array)) { $rel = $array['rel']; } elseif ($strict) { throw new MissingArgumentException('rel'); } if (array_key_exists('name', $array)) { $name = $array['name']; } if (array_key_exists('prompt', $array)) { $prompt = $array['prompt']; } $query = new self($href, $rel, $name, $prompt); if (array_key_exists('data', $array)) { if (!is_array($array['data'])) { throw new ExpectedArrayException('data', gettype($array['data'])); } try { foreach ($array['data'] as $key => $dataArray) { if (!is_array($dataArray)) { throw new ExpectedArrayException($key, gettype($dataArray)); } try { $query->getData()->add(Data::fromArray($dataArray, $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException($key, $e->getMessage()); } } } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('data', $e->getMessage()); } } return $query; }
/** * @param array $array * @param bool $strict * @return Item * @throws FromArrayCompilationException */ public static function fromArray(array $array, $strict = true) { $href = ''; if (array_key_exists('href', $array)) { $href = $array['href']; } elseif ($strict) { throw new MissingArgumentException('href'); } $item = new self($href); if (array_key_exists('data', $array)) { if (!is_array($array['data'])) { throw new ExpectedArrayException('data', gettype($array['data'])); } try { foreach ($array['data'] as $key => $dataArray) { if (!is_array($dataArray)) { throw new ExpectedArrayException($key, gettype($dataArray)); } try { $item->getData()->add(Data::fromArray($dataArray, $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException($key, $e->getMessage()); } } } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('data', $e->getMessage()); } } 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 { $item->getLinks()->add(Link::fromArray($linkArray, $strict)); } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException($key, $e->getMessage()); } } } catch (FromArrayCompilationException $e) { throw new FromArrayCompilationException('links', $e->getMessage()); } } return $item; }