Skip to content

Commit

Permalink
Merge pull request #29 from holtkamp/patch-attribute-hydrator-interface
Browse files Browse the repository at this point in the history
Introduce AttributeHydratorInterface
  • Loading branch information
kocsismate authored Apr 19, 2020
2 parents d806109 + a836a26 commit 1e8ed9f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
6 changes: 4 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ parameters:
-
message: '#^Access to an undefined property object::.*$#'
paths:
- '%currentWorkingDirectory%/src/JsonApi/Hydrator/ClassDocumentHydrator'
- '%currentWorkingDirectory%/src/JsonApi/Hydrator/AttributeHydrator'
- '%currentWorkingDirectory%/tests/JsonApi/Hydrator/ClassDocumentHydratorTest'
-
message: '#^Variable property access on object\.$#'
path: '%currentWorkingDirectory%/src/JsonApi/Hydrator/ClassDocumentHydrator'
paths:
- '%currentWorkingDirectory%/src/JsonApi/Hydrator/AttributeHydrator'
- '%currentWorkingDirectory%/src/JsonApi/Hydrator/ClassDocumentHydrator'
- '#^Dynamic call to static method PHPUnit\\Framework\\.*$#'
-
message: '#^.*$#'
Expand Down
21 changes: 21 additions & 0 deletions src/JsonApi/Hydrator/AttributeHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace WoohooLabs\Yang\JsonApi\Hydrator;

use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject;

class AttributeHydrator implements AttributeHydratorInterface
{
public function hydrateResourceAttributes(object $result, ResourceObject $resource): object
{
$result->type = $resource->type();
$result->id = $resource->id();
foreach ($resource->attributes() as $attribute => $value) {
$result->{$attribute} = $value;
}

return $result;
}
}
12 changes: 12 additions & 0 deletions src/JsonApi/Hydrator/AttributeHydratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace WoohooLabs\Yang\JsonApi\Hydrator;

use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject;

interface AttributeHydratorInterface
{
public function hydrateResourceAttributes(object $result, ResourceObject $resource): object;
}
18 changes: 11 additions & 7 deletions src/JsonApi/Hydrator/ClassDocumentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

class ClassDocumentHydrator extends AbstractClassDocumentHydrator
{
/**
* @var AttributeHydratorInterface
*/
private $attributeHydrator;

public function __construct(?AttributeHydratorInterface $attributeHydrator = null)
{
$this->attributeHydrator = $attributeHydrator ?? new AttributeHydrator();
}

/**
* @return object[]
*/
Expand Down Expand Up @@ -39,13 +49,7 @@ protected function createObject(ResourceObject $resource): object

protected function hydrateResourceAttributes(object $result, ResourceObject $resource): object
{
$result->type = $resource->type();
$result->id = $resource->id();
foreach ($resource->attributes() as $attribute => $value) {
$result->{$attribute} = $value;
}

return $result;
return $this->attributeHydrator->hydrateResourceAttributes($result, $resource);
}

protected function hydrateResourceRelationship(object $result, Relationship $relationship, string $name, object $object): object
Expand Down

0 comments on commit 1e8ed9f

Please sign in to comment.