-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed some issues with the HydratorInterface and ClassHydrator
- Loading branch information
1 parent
fb53960
commit eba24d5
Showing
7 changed files
with
727 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WoohooLabs\Yang\JsonApi\Hydrator; | ||
|
||
use stdClass; | ||
use WoohooLabs\Yang\JsonApi\Schema\Document; | ||
use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject; | ||
|
||
final class ClassDocumentHydrator implements DocumentHydratorInterface | ||
{ | ||
/** | ||
* @return stdClass[] | ||
*/ | ||
public function hydrate(Document $document): iterable | ||
{ | ||
if ($document->hasAnyPrimaryResources() === false) { | ||
return []; | ||
} | ||
|
||
if ($document->isSingleResourceDocument()) { | ||
return [$this->hydratePrimaryResource($document)]; | ||
} | ||
|
||
return $this->hydratePrimaryResources($document); | ||
} | ||
|
||
public function hydrateSingleResource(Document $document): stdClass | ||
{ | ||
if ($document->isSingleResourceDocument() === false) { | ||
return new stdClass(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kocsismate
Author
Member
|
||
} | ||
|
||
if ($document->hasAnyPrimaryResources() === false) { | ||
return new stdClass(); | ||
} | ||
|
||
return $this->hydratePrimaryResource($document); | ||
} | ||
|
||
/** | ||
* @return stdClass[] | ||
*/ | ||
public function hydrateCollection(Document $document): iterable | ||
{ | ||
if ($document->hasAnyPrimaryResources() === false) { | ||
return []; | ||
} | ||
|
||
if ($document->isSingleResourceDocument()) { | ||
return []; | ||
} | ||
|
||
return $this->hydratePrimaryResources($document); | ||
} | ||
|
||
private function hydratePrimaryResources(Document $document): array | ||
{ | ||
$result = []; | ||
$resourceMap = []; | ||
|
||
foreach ($document->primaryResources() as $primaryResource) { | ||
$result[] = $this->hydrateResource($primaryResource, $document, $resourceMap); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
return $result; | ||
} | ||
|
||
private function hydratePrimaryResource(Document $document): stdClass | ||
{ | ||
$resourceMap = []; | ||
|
||
return $this->hydrateResource($document->primaryResource(), $document, $resourceMap); | ||
} | ||
|
||
/** | ||
* @param stdClass[] $resourceMap | ||
*/ | ||
private function hydrateResource(ResourceObject $resource, Document $document, array &$resourceMap): stdClass | ||
{ | ||
// Fill basic attributes of the resource | ||
$result = new stdClass(); | ||
$result->type = $resource->type(); | ||
$result->id = $resource->id(); | ||
foreach ($resource->attributes() as $attribute => $value) { | ||
$result->{$attribute} = $value; | ||
} | ||
|
||
//Save resource to the identity map | ||
$this->saveObjectToMap($result, $resourceMap); | ||
|
||
//Fill relationships | ||
foreach ($resource->relationships() as $name => $relationship) { | ||
foreach ($relationship->resourceLinks() as $link) { | ||
$object = $this->getObjectFromMap($link["type"], $link["id"], $resourceMap); | ||
|
||
if ($object === null && $document->hasIncludedResource($link["type"], $link["id"])) { | ||
$relatedResource = $document->resource($link["type"], $link["id"]); | ||
$object = $this->hydrateResource($relatedResource, $document, $resourceMap); | ||
} | ||
|
||
if ($object === null) { | ||
continue; | ||
} | ||
|
||
if ($relationship->isToOneRelationship()) { | ||
$result->{$name} = $object; | ||
} else { | ||
$result->{$name}[] = $object; | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param stdClass[] $resourceMap | ||
*/ | ||
private function getObjectFromMap(string $type, string $id, array $resourceMap): ?stdClass | ||
{ | ||
return $resourceMap[$type . "-" . $id] ?? null; | ||
} | ||
|
||
/** | ||
* @param stdClass[] $resourceMap | ||
*/ | ||
private function saveObjectToMap(stdClass $object, array &$resourceMap): void | ||
{ | ||
$resourceMap[$object->type . "-" . $object->id] = $object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WoohooLabs\Yang\JsonApi\Hydrator; | ||
|
||
use WoohooLabs\Yang\JsonApi\Schema\Document; | ||
|
||
interface DocumentHydratorInterface | ||
{ | ||
/** | ||
* Hydrates a document into an array/list of items regardless if the primary data is a single resource or a | ||
* collection of primary resources. | ||
* | ||
* @return iterable<mixed> | ||
*/ | ||
public function hydrate(Document $document): iterable; | ||
|
||
/** | ||
* Hydrates a document when its primary data is a single resource. | ||
* | ||
* @return mixed | ||
*/ | ||
public function hydrateSingleResource(Document $document); | ||
|
||
/** | ||
* Hydrates a document when its primary data is a collection of resources. | ||
* | ||
* @return iterable<mixed> | ||
*/ | ||
public function hydrateCollection(Document $document): iterable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Instead of returning an "empty"
stdClass
here, would it not be better to returnnull
here and use the nullable?stdClass
as return type?Then it can be used like: