/**
  * @param object $object The link object
  *
  * @return self
  *
  * @throws ValidationException
  */
 public function __construct($object, FactoryManagerInterface $manager)
 {
     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');
     }
     $this->manager = $manager;
     $this->container = new DataContainer();
     if (property_exists($object, 'self')) {
         if (!is_string($object->self)) {
             throw new ValidationException('property "self" has to be a string, "' . gettype($object->self) . '" given.');
         }
         $this->container->set('self', strval($object->self));
     }
     if (property_exists($object, 'related')) {
         if (!is_string($object->related)) {
             throw new ValidationException('property "related" has to be a string, "' . gettype($object->related) . '" given.');
         }
         $this->container->set('related', strval($object->related));
     }
     if (property_exists($object, 'pagination')) {
         $this->container->set('pagination', $this->manager->getFactory()->make('Pagination', [$object->pagination, $this->manager]));
     }
 }
 /**
  * @param object $object The error object
  *
  * @return self
  *
  * @throws ValidationException
  */
 public function parse($object)
 {
     if (!is_object($object)) {
         throw new ValidationException('Resource has to be an object, "' . gettype($object) . '" given.');
     }
     if (!property_exists($object, 'type')) {
         throw new ValidationException('A resource object MUST contain a type');
     }
     if (!property_exists($object, 'id')) {
         throw new ValidationException('A resource object MUST contain an id');
     }
     if (is_object($object->type) or is_array($object->type)) {
         throw new ValidationException('Resource type cannot be an array or object');
     }
     if (is_object($object->id) or is_array($object->id)) {
         throw new ValidationException('Resource Id cannot be an array or object');
     }
     $this->container->set('type', strval($object->type));
     $this->container->set('id', strval($object->id));
     if (property_exists($object, 'meta')) {
         $meta = $this->manager->getFactory()->make('Meta', [$this->manager, $this]);
         $meta->parse($object->meta);
         $this->container->set('meta', $meta);
     }
     return $this;
 }
 /**
  * @param object $object The error object
  *
  * @return self
  *
  * @throws ValidationException
  */
 public function __construct($object, FactoryManagerInterface $manager)
 {
     if (!is_object($object)) {
         throw new ValidationException('Resource has to be an object, "' . gettype($object) . '" given.');
     }
     if (!property_exists($object, 'type')) {
         throw new ValidationException('A resource object MUST contain a type');
     }
     if (!property_exists($object, 'id')) {
         throw new ValidationException('A resource object MUST contain an id');
     }
     if (is_object($object->type) or is_array($object->type)) {
         throw new ValidationException('Resource type cannot be an array or object');
     }
     if (is_object($object->id) or is_array($object->id)) {
         throw new ValidationException('Resource Id cannot be an array or object');
     }
     $this->manager = $manager;
     $this->container = new DataContainer();
     $this->container->set('type', strval($object->type));
     $this->container->set('id', strval($object->id));
     if (property_exists($object, 'meta')) {
         $this->container->set('meta', $this->manager->getFactory()->make('Meta', [$object->meta, $this->manager]));
     }
     return $this;
 }
 /**
  * Parse the data value
  *
  * @throws ValidationException If $data isn't null or an object
  *
  * @param null|array|object $data Data value
  * @return null|Resource\Identifier|Resource\IdentifierCollection The parsed data
  */
 protected function parseData($data)
 {
     if ($data === null) {
         return $data;
     }
     if (is_array($data)) {
         return $this->manager->getFactory()->make('Resource\\IdentifierCollection', [$data, $this->manager]);
     }
     return $this->manager->getFactory()->make('Resource\\Identifier', [$data, $this->manager]);
 }
Exemple #5
0
 /**
  * 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('Error has to be an object, "' . gettype($object) . '" given.');
     }
     if (property_exists($object, 'id')) {
         if (!is_string($object->id)) {
             throw new ValidationException('property "id" has to be a string, "' . gettype($object->id) . '" given.');
         }
         $this->container->set('id', strval($object->id));
     }
     if (property_exists($object, 'links')) {
         $links = $this->manager->getFactory()->make('ErrorLink', [$this->manager, $this]);
         $links->parse($object->links);
         $this->container->set('links', $links);
     }
     if (property_exists($object, 'status')) {
         if (!is_string($object->status)) {
             throw new ValidationException('property "status" has to be a string, "' . gettype($object->status) . '" given.');
         }
         $this->container->set('status', strval($object->status));
     }
     if (property_exists($object, 'code')) {
         if (!is_string($object->code)) {
             throw new ValidationException('property "code" has to be a string, "' . gettype($object->code) . '" given.');
         }
         $this->container->set('code', strval($object->code));
     }
     if (property_exists($object, 'title')) {
         if (!is_string($object->title)) {
             throw new ValidationException('property "title" has to be a string, "' . gettype($object->title) . '" given.');
         }
         $this->container->set('title', strval($object->title));
     }
     if (property_exists($object, 'detail')) {
         if (!is_string($object->detail)) {
             throw new ValidationException('property "detail" has to be a string, "' . gettype($object->detail) . '" given.');
         }
         $this->container->set('detail', strval($object->detail));
     }
     if (property_exists($object, 'source')) {
         $source = $this->manager->getFactory()->make('ErrorSource', [$this->manager, $this]);
         $source->parse($object->source);
         $this->container->set('source', $source);
     }
     if (property_exists($object, 'meta')) {
         $meta = $this->manager->getFactory()->make('Meta', [$this->manager, $this]);
         $meta->parse($object->meta);
         $this->container->set('meta', $meta);
     }
     return $this;
 }
 /**
  * Parses the data for this element
  *
  * @param mixed $object The data
  *
  * @return self
  *
  * @throws ValidationException
  */
 public function parse($errors)
 {
     if (!is_array($errors)) {
         throw new ValidationException('Errors for a collection has to be in an array, "' . gettype($errors) . '" given.');
     }
     if (count($errors) === 0) {
         throw new ValidationException('Errors array cannot be empty and MUST have at least one object');
     }
     foreach ($errors as $err) {
         $error = $this->manager->getFactory()->make('Error', [$this->manager, $this]);
         $error->parse($err);
         $this->container->set('', $error);
     }
     return $this;
 }
 /**
  * @param array $resources The resources as array
  *
  * @return self
  *
  * @throws ValidationException
  */
 public function __construct($errors, FactoryManagerInterface $manager)
 {
     if (!is_array($errors)) {
         throw new ValidationException('Errors for a collection has to be in an array, "' . gettype($errors) . '" given.');
     }
     if (count($errors) === 0) {
         throw new ValidationException('Errors array cannot be empty and MUST have at least one object');
     }
     $this->manager = $manager;
     $this->container = new DataContainer();
     foreach ($errors as $error) {
         $this->container->set('', $this->manager->getFactory()->make('Error', [$error, $this->manager]));
     }
     return $this;
 }
 /**
  * Generate a new resource from an object
  *
  * @param object $data The resource data
  * @return ResourceInterface The resource
  */
 protected function parseResource($data)
 {
     if (!is_object($data)) {
         throw new ValidationException('Resources inside a collection MUST be objects, "' . gettype($data) . '" given.');
     }
     $object_vars = get_object_vars($data);
     // the 2 properties must be type and id
     // or the 3 properties must be type, id and meta
     if (count($object_vars) === 2 or count($object_vars) === 3 and property_exists($data, 'meta')) {
         $resource = $this->manager->getFactory()->make('Resource\\Identifier', [$data, $this->manager]);
     } else {
         $resource = $this->manager->getFactory()->make('Resource\\Item', [$data, $this->manager]);
     }
     return $resource;
 }
Exemple #9
0
 /**
  * @param object $object The error object
  *
  * @return self
  *
  * @throws ValidationException
  */
 public function __construct($object, FactoryManagerInterface $manager)
 {
     if (!is_object($object)) {
         throw new ValidationException('Error has to be an object, "' . gettype($object) . '" given.');
     }
     $this->manager = $manager;
     $this->container = new DataContainer();
     if (property_exists($object, 'id')) {
         if (!is_string($object->id)) {
             throw new ValidationException('property "id" has to be a string, "' . gettype($object->id) . '" given.');
         }
         $this->container->set('id', strval($object->id));
     }
     if (property_exists($object, 'links')) {
         $this->container->set('links', $this->manager->getFactory()->make('ErrorLink', [$object->links, $this->manager]));
     }
     if (property_exists($object, 'status')) {
         if (!is_string($object->status)) {
             throw new ValidationException('property "status" has to be a string, "' . gettype($object->status) . '" given.');
         }
         $this->container->set('status', strval($object->status));
     }
     if (property_exists($object, 'code')) {
         if (!is_string($object->code)) {
             throw new ValidationException('property "code" has to be a string, "' . gettype($object->code) . '" given.');
         }
         $this->container->set('code', strval($object->code));
     }
     if (property_exists($object, 'title')) {
         if (!is_string($object->title)) {
             throw new ValidationException('property "title" has to be a string, "' . gettype($object->title) . '" given.');
         }
         $this->container->set('title', strval($object->title));
     }
     if (property_exists($object, 'detail')) {
         if (!is_string($object->detail)) {
             throw new ValidationException('property "detail" has to be a string, "' . gettype($object->detail) . '" given.');
         }
         $this->container->set('detail', strval($object->detail));
     }
     if (property_exists($object, 'source')) {
         $this->container->set('source', $this->manager->getFactory()->make('ErrorSource', [$object->source, $this->manager]));
     }
     if (property_exists($object, 'meta')) {
         $this->container->set('meta', $this->manager->getFactory()->make('Meta', [$object->meta, $this->manager]));
     }
     return $this;
 }
Exemple #10
0
 /**
  * Parse the data value
  *
  * @throws ValidationException If $data isn't null or an object
  *
  * @param null|object $data Data value
  * @return ElementInterface The parsed data
  */
 protected function parseData($data)
 {
     if ($data === null) {
         $resource = $this->manager->getFactory()->make('ResourceNull', [$this->manager, $this]);
         $resource->parse($data);
         return $resource;
     }
     if (is_array($data)) {
         $collection = $this->manager->getFactory()->make('ResourceCollection', [$this->manager, $this]);
         $collection->parse($data);
         return $collection;
     }
     if (!is_object($data)) {
         throw new ValidationException('Data value has to be null or an object, "' . gettype($data) . '" given.');
     }
     $object_vars = get_object_vars($data);
     // the properties must be type and id or
     // the 3 properties must be type, id and meta
     if (count($object_vars) === 2 or count($object_vars) === 3 and property_exists($data, 'meta')) {
         $resource = $this->manager->getFactory()->make('ResourceIdentifier', [$this->manager, $this]);
         $resource->parse($data);
     } else {
         $resource = $this->manager->getFactory()->make('ResourceItem', [$this->manager, $this]);
         $resource->parse($data);
     }
     return $resource;
 }
Exemple #11
0
 /**
  * Add a new value to the meta element and creates it if not exists
  * @param $key
  * @param $value
  */
 public function addMetaData($key, $value)
 {
     if ($this->has('meta')) {
         $this->get('meta')->set($key, $value);
     } else {
         $this->container->set('meta', $this->manager->getFactory()->make('Meta', [(object) [$key => $value], $this->manager]));
     }
 }
Exemple #12
0
 /**
  * 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('Jsonapi has to be an object, "' . gettype($object) . '" given.');
     }
     if (property_exists($object, 'version')) {
         if (is_object($object->version) or is_array($object->version)) {
             throw new ValidationException('property "version" cannot be an object or array, "' . gettype($object->version) . '" given.');
         }
         $this->container->set('version', strval($object->version));
     }
     if (property_exists($object, 'meta')) {
         $meta = $this->manager->getFactory()->make('Meta', [$this->manager, $this]);
         $meta->parse($object->meta);
         $this->container->set('meta', $meta);
     }
     return $this;
 }
 /**
  * @param object $object The error object
  *
  * @return self
  *
  * @throws ValidationException
  */
 public function __construct($object, FactoryManagerInterface $manager)
 {
     if (!is_object($object)) {
         throw new ValidationException('Jsonapi has to be an object, "' . gettype($object) . '" given.');
     }
     $this->manager = $manager;
     $this->container = new DataContainer();
     if (property_exists($object, 'version')) {
         if (is_object($object->version) or is_array($object->version)) {
             throw new ValidationException('property "version" cannot be an object or array, "' . gettype($object->version) . '" given.');
         }
         $this->container->set('version', strval($object->version));
     }
     if (property_exists($object, 'meta')) {
         $this->container->set('meta', $this->manager->getFactory()->make('Meta', [$object->meta, $this->manager]));
     }
     return $this;
 }
 /**
  * @param object $object The link object
  *
  * @return self
  *
  * @throws ValidationException
  */
 public function __construct($object, FactoryManagerInterface $manager)
 {
     if (!is_object($object)) {
         throw new ValidationException('Link has to be an object, "' . gettype($object) . '" given.');
     }
     if (!property_exists($object, 'about')) {
         throw new ValidationException('ErrorLink MUST contain these properties: about');
     }
     if (!is_string($object->about) and !is_object($object->about)) {
         throw new ValidationException('Link has to be an object or string, "' . gettype($object->about) . '" given.');
     }
     $this->manager = $manager;
     $this->container = new DataContainer();
     if (is_string($object->about)) {
         $this->container->set('about', strval($object->about));
         return $this;
     }
     $this->container->set('about', $this->manager->getFactory()->make('Link', [$object->about, $this->manager]));
 }
 /**
  * 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;
 }
Exemple #16
0
 /**
  * Set a link
  *
  * @param string $name The Name
  * @param string|object $link The Link
  *
  * @return self
  */
 protected function set($name, $link)
 {
     if ($name === 'meta') {
         $this->container->set($name, $this->manager->getFactory()->make('Meta', [$link, $this->manager]));
         return $this;
     }
     // from spec: an object ("link object") which can contain the following members:
     // - href: a string containing the link's URL.
     if ($name === 'href' or !is_object($link)) {
         if (!is_string($link)) {
             throw new ValidationException('Link has to be an object or string, "' . gettype($link) . '" given.');
         }
         $this->container->set($name, strval($link));
         return $this;
     }
     // Now $link can only be an object
     // Create Link object if needed
     if (!$link instanceof LinkInterface) {
         $this->container->set($name, $this->manager->getFactory()->make('Link', [$link, $this->manager]));
     }
     return $this;
 }
Exemple #17
0
 /**
  * Parse the data value
  *
  * @throws ValidationException If $data isn't null or an object
  *
  * @param null|array|object $data Data value
  * @return null|ResourceIdentifier|ResourceIdentifierCollection The parsed data
  */
 protected function parseData($data)
 {
     if ($data === null) {
         return $data;
     }
     if (is_array($data)) {
         $collection = $this->manager->getFactory()->make('ResourceIdentifierCollection', [$this->manager, $this]);
         $collection->parse($data);
         return $collection;
     }
     $identifier = $this->manager->getFactory()->make('ResourceIdentifier', [$this->manager, $this]);
     $identifier->parse($data);
     return $identifier;
 }
 /**
  * @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;
 }
Exemple #19
0
 /**
  * Set a link
  *
  * @param string $name The name of the link
  * @param string $link The link
  * @return self
  */
 private function setLink($name, $link)
 {
     if (!is_string($link) and !is_object($link)) {
         throw new ValidationException('Link attribute has to be an object or string, "' . gettype($link) . '" given.');
     }
     if (is_string($link)) {
         $this->container->set($name, strval($link));
         return $this;
     }
     // Now $link can only be an object
     $link_object = $this->manager->getFactory()->make('Link', [$this->manager, $this]);
     $link_object->parse($link);
     $this->container->set($name, $link_object);
     return $this;
 }
Exemple #20
0
 /**
  * Set a link
  *
  * @param string $name The Name
  * @param string|object $link The Link
  *
  * @return self
  */
 protected function set($name, $link)
 {
     if ($name === 'meta') {
         $meta = $this->manager->getFactory()->make('Meta', [$this->manager, $this]);
         $meta->parse($link);
         $this->container->set($name, $meta);
         return $this;
     }
     // every link must be an URL
     if (!is_string($link)) {
         throw new ValidationException('Every link attribute has to be a string, "' . gettype($link) . '" given.');
     }
     $this->container->set($name, strval($link));
     return $this;
 }
 /**
  * Set a link
  *
  * @param string $name The Name
  * @param string|object $link The Link
  *
  * @return self
  */
 protected function set($name, $link)
 {
     // from spec: aA link MUST be represented as either:
     // - a string containing the link's URL.
     // - an object ("link object") which can contain the following members:
     if (!is_object($link) and !is_string($link)) {
         throw new ValidationException('Link attribute has to be an object or string, "' . gettype($link) . '" given.');
     }
     if (is_string($link)) {
         $this->container->set($name, strval($link));
         return $this;
     }
     // Now $link can only be an object
     $link_object = $this->manager->getFactory()->make('Link', [$this->manager, $this]);
     $link_object->parse($link);
     $this->container->set($name, $link_object);
     return $this;
 }
 /**
  * Parse the data value
  *
  * @throws ValidationException If $data isn't null or an object
  *
  * @param null|object $data Data value
  * @return ResourceInterface The parsed data
  */
 protected function parseData($data)
 {
     if ($data === null) {
         return $this->manager->getFactory()->make('Resource\\NullResource', [$data, $this->manager]);
     }
     if (is_array($data)) {
         return $this->manager->getFactory()->make('Resource\\Collection', [$data, $this->manager]);
     }
     if (!is_object($data)) {
         throw new ValidationException('Data value has to be null or an object, "' . gettype($data) . '" given.');
     }
     $object_vars = get_object_vars($data);
     // the properties must be type and id
     if (count($object_vars) === 2) {
         $resource = $this->manager->getFactory()->make('Resource\\Identifier', [$data, $this->manager]);
     } elseif (count($object_vars) === 3 and property_exists($data, 'meta')) {
         $resource = $this->manager->getFactory()->make('Resource\\Identifier', [$data, $this->manager]);
     } else {
         $resource = $this->manager->getFactory()->make('Resource\\Item', [$data, $this->manager]);
     }
     return $resource;
 }
 public function add($error)
 {
     $this->container->set('', $this->manager->getFactory()->make('Error', [$error, $this->manager]));
 }
 /**
  * Generate a new resource from an object
  *
  * @param object $data The resource data
  * @return ElementInterface The resource
  */
 protected function parseResource($data)
 {
     $identifier = $resource = $this->manager->getFactory()->make('ResourceIdentifier', [$this->manager, $this]);
     $identifier->parse($data);
     return $identifier;
 }
 /**
  * Generate a new resource from an object
  *
  * @param object $data The resource data
  * @return ResourceInterface The resource
  */
 protected function parseResource($data)
 {
     return $resource = $this->manager->getFactory()->make('Resource\\Identifier', [$data, $this->manager]);
 }