public function parse($object)
{
if (!is_object($object)) {
throw new ValidationException('Document has to be an object, "' . gettype($object) . '" given.');
}
if (!property_exists($object, 'data') and !property_exists($object, 'meta') and !property_exists($object, 'errors')) {
throw new ValidationException('Document MUST contain at least one of the following properties: data, errors, meta');
}
if (property_exists($object, 'data') and property_exists($object, 'errors')) {
throw new ValidationException('The properties `data` and `errors` MUST NOT coexist in Document.');
}
if (property_exists($object, 'data')) {
$this->container->set('data', $this->parseData($object->data));
}
if (property_exists($object, 'meta')) {
$meta = $this->manager->getFactory()->make('Meta', [$this->manager, $this]);
$meta->parse($object->meta);
$this->container->set('meta', $meta);
}
if (property_exists($object, 'errors')) {
$errors = $this->manager->getFactory()->make('ErrorCollection', [$this->manager, $this]);
$errors->parse($object->errors);
$this->container->set('errors', $errors);
}
if (property_exists($object, 'included')) {
if (!property_exists($object, 'data')) {
throw new ValidationException('If Document does not contain a `data` property, the `included` property MUST NOT be present either.');
}
$collection = $this->manager->getFactory()->make('ResourceCollection', [$this->manager, $this]);
$collection->parse($object->included);
$this->container->set('included', $collection);
}
if (property_exists($object, 'jsonapi')) {
$jsonapi = $this->manager->getFactory()->make('Jsonapi', [$this->manager, $this]);
$jsonapi->parse($object->jsonapi);
$this->container->set('jsonapi', $jsonapi);
}
if (property_exists($object, 'links')) {
$links = $this->manager->getFactory()->make('DocumentLink', [$this->manager, $this]);
$links->parse($object->links);
$this->container->set('links', $links);
}
return $this;
}