Art4\JsonApiClient\Document::parseData PHP Method

parseData() protected method

Parse the data value
protected parseData ( null | object $data ) : Art4\JsonApiClient\ElementInterface
$data null | object Data value
return Art4\JsonApiClient\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;
    }