diff --git a/src/ItemHydrator.php b/src/ItemHydrator.php index 747ca1d..1daaaba 100644 --- a/src/ItemHydrator.php +++ b/src/ItemHydrator.php @@ -14,9 +14,9 @@ class ItemHydrator { /** - * @var \Swis\JsonApi\Client\TypeMapper + * @var \Swis\JsonApi\Client\Interfaces\TypeMapperInterface */ - private $typeMapper; + protected $typeMapper; /** * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface $typeMapper @@ -44,9 +44,9 @@ public function hydrate(ItemInterface $item, array $attributes): ItemInterface /** * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param array|null $attributes + * @param array $attributes */ - protected function fill(ItemInterface $item, array $attributes = null) + protected function fill(ItemInterface $item, array $attributes) { $item->fill(array_diff_key($attributes, array_combine($item->getAvailableRelations(), $item->getAvailableRelations()))); } @@ -59,7 +59,7 @@ protected function fill(ItemInterface $item, array $attributes = null) * * @throws \Exception */ - protected function fillRelations(ItemInterface $item, array $attributes = null) + protected function fillRelations(ItemInterface $item, array $attributes) { // Fill Relations foreach ($item->getAvailableRelations() as $availableRelation) { diff --git a/src/JsonApi/Parser.php b/src/JsonApi/Parser.php index d06f6c6..1d5a813 100644 --- a/src/JsonApi/Parser.php +++ b/src/JsonApi/Parser.php @@ -84,7 +84,6 @@ public function deserialize(string $json): DocumentInterface */ private function getJsonApiDocument(string $json): Art4JsonApiDocumentInterface { - /** @var \Art4\JsonApiClient\DocumentInterface $jsonApiDocument */ $jsonApiDocument = $this->manager->parse($json); if (!$jsonApiDocument instanceof Art4JsonApiDocumentInterface) { @@ -109,20 +108,20 @@ protected function buildDataDocument(Art4JsonApiDocumentInterface $jsonApiDocume $allHydratedItems = new Collection(); $allJsonApiItems = new Collection(); - if ($data instanceof ResourceCollectionInterface) { - $collection = $this->hydrator->hydrateCollection($jsonApiDocument->get('data')); - $allHydratedItems = $allHydratedItems->concat($collection); - $allJsonApiItems = $allJsonApiItems->concat($jsonApiDocument->get('data')->asArray()); - - $document = new CollectionDocument(); - $document->setData($collection); - } elseif ($data instanceof ResourceItemInterface) { - $item = $this->hydrator->hydrateItem($jsonApiDocument->get('data')); + if ($data instanceof ResourceItemInterface) { + $item = $this->hydrator->hydrateItem($data); $allHydratedItems->push($item); - $allJsonApiItems->push($jsonApiDocument->get('data')); + $allJsonApiItems->push($data); $document = new ItemDocument(); $document->setData($item); + } elseif ($data instanceof ResourceCollectionInterface) { + $collection = $this->hydrator->hydrateCollection($data); + $allHydratedItems = $allHydratedItems->concat($collection); + $allJsonApiItems = $allJsonApiItems->concat(new Collection($data->asArray())); + + $document = new CollectionDocument(); + $document->setData($collection); } else { throw new \DomainException('Data is not Collection or Item'); } @@ -131,7 +130,7 @@ protected function buildDataDocument(Art4JsonApiDocumentInterface $jsonApiDocume if ($includedInDocument) { $included = $this->hydrator->hydrateCollection($includedInDocument); $allHydratedItems = $allHydratedItems->concat($included); - $allJsonApiItems = $allJsonApiItems->concat($includedInDocument->asArray()); + $allJsonApiItems = $allJsonApiItems->concat(new Collection($includedInDocument->asArray())); } $this->hydrator->hydrateRelationships($allJsonApiItems, $allHydratedItems); diff --git a/src/Providers/TypeMapperServiceProvider.php b/src/Providers/TypeMapperServiceProvider.php index 54dbbd1..03422fa 100644 --- a/src/Providers/TypeMapperServiceProvider.php +++ b/src/Providers/TypeMapperServiceProvider.php @@ -2,10 +2,10 @@ namespace Swis\JsonApi\Client\Providers; -use Illuminate\Support\ServiceProvider; +use Illuminate\Support\ServiceProvider as BaseServiceProvider; use Swis\JsonApi\Client\Interfaces\TypeMapperInterface; -class TypeMapperServiceProvider extends ServiceProvider +class TypeMapperServiceProvider extends BaseServiceProvider { /** * A list of class names implementing \Swis\JsonApi\Client\Interfaces\ItemInterface.