/** * Parses the data for this element * * @param mixed $object The data * * @return self * * @throws ValidationException */ public function parse($object) { if (!is_object($object)) { throw new ValidationException('Relationships has to be an object, "' . gettype($object) . '" given.'); } if (property_exists($object, 'type') or property_exists($object, 'id')) { throw new ValidationException('These properties are not allowed in attributes: `type`, `id`'); } $object_vars = get_object_vars($object); if (count($object_vars) === 0) { return $this; } foreach ($object_vars as $name => $value) { if ($this->parent->has('attributes.' . $name)) { throw new ValidationException('"' . $name . '" property cannot be set because it exists already in parents Resource object.'); } $relationship = $this->manager->getFactory()->make('Relationship', [$this->manager, $this]); $relationship->parse($value); $this->container->set($name, $relationship); } return $this; }
/** * Parses the data for this element * * @param mixed $object The data * * @return self * * @throws ValidationException */ public function parse($object) { if (!is_object($object)) { throw new ValidationException('RelationshipLink has to be an object, "' . gettype($object) . '" given.'); } if (!property_exists($object, 'self') and !property_exists($object, 'related')) { throw new ValidationException('RelationshipLink has to be at least a "self" or "related" link'); } $links = get_object_vars($object); if (array_key_exists('self', $links)) { if (!is_string($links['self'])) { throw new ValidationException('property "self" has to be a string, "' . gettype($links['self']) . '" given.'); } $this->container->set('self', strval($links['self'])); unset($links['self']); } if (array_key_exists('related', $links)) { if (!is_string($links['related']) and !is_object($links['related'])) { throw new ValidationException('property "related" has to be a string or object, "' . gettype($links['related']) . '" given.'); } $this->setLink('related', $links['related']); unset($links['related']); } // Pagination links if ($this->parent->has('data') and $this->parent->get('data') instanceof ResourceIdentifierCollectionInterface) { if (array_key_exists('first', $links)) { $this->setPaginationLink('first', $links['first']); unset($links['first']); } if (array_key_exists('last', $links)) { $this->setPaginationLink('last', $links['last']); unset($links['last']); } if (array_key_exists('prev', $links)) { $this->setPaginationLink('prev', $links['prev']); unset($links['prev']); } if (array_key_exists('next', $links)) { $this->setPaginationLink('next', $links['next']); unset($links['next']); } } // custom links foreach ($links as $name => $value) { $this->setLink($name, $value); } }
/** * Parses the data for this element * * @param mixed $object The data * * @return self * * @throws ValidationException */ public function parse($object) { if (!is_object($object)) { throw new ValidationException('DocumentLink has to be an object, "' . gettype($object) . '" given.'); } $links = get_object_vars($object); if (array_key_exists('self', $links)) { if (!is_string($links['self']) and !is_object($links['self'])) { throw new ValidationException('property "self" has to be a string or object, "' . gettype($links['self']) . '" given.'); } $this->setLink('self', $links['self']); unset($links['self']); } if (array_key_exists('related', $links)) { if (!is_string($links['related']) and !is_object($links['related'])) { throw new ValidationException('property "related" has to be a string or object, "' . gettype($links['related']) . '" given.'); } $this->setLink('related', $links['related']); unset($links['related']); } // Pagination links, if data in parent attributes exists if ($this->parent->has('data')) { if (array_key_exists('first', $links)) { $this->setPaginationLink('first', $links['first']); unset($links['first']); } if (array_key_exists('last', $links)) { $this->setPaginationLink('last', $links['last']); unset($links['last']); } if (array_key_exists('prev', $links)) { $this->setPaginationLink('prev', $links['prev']); unset($links['prev']); } if (array_key_exists('next', $links)) { $this->setPaginationLink('next', $links['next']); unset($links['next']); } } // custom links foreach ($links as $name => $value) { $this->setLink($name, $value); } }
/** * @param object $object The object * * @return self * * @throws ValidationException */ public function __construct($object, FactoryManagerInterface $manager, AccessInterface $resource) { if (!is_object($object)) { throw new ValidationException('Relationships has to be an object, "' . gettype($object) . '" given.'); } if (property_exists($object, 'type') or property_exists($object, 'id')) { throw new ValidationException('These properties are not allowed in attributes: `type`, `id`'); } $this->manager = $manager; $this->container = new DataContainer(); $object_vars = get_object_vars($object); if (count($object_vars) === 0) { return $this; } foreach ($object_vars as $name => $value) { // #FIXME: Work here with parent strategy if ($resource->has('attributes.' . $name)) { throw new ValidationException('"' . $name . '" property cannot be set because it exists already in parents Resource object.'); } $this->container->set($name, $this->manager->getFactory()->make('Relationship', [$value, $this->manager])); } return $this; }