| @@ 16-46 (lines=31) @@ | ||
| 13 | * |
|
| 14 | * @package Mikemirten\Component\JsonApi\Hydrator\Extension |
|
| 15 | */ |
|
| 16 | class IncludedExtension implements ExtensionInterface |
|
| 17 | { |
|
| 18 | /** |
|
| 19 | * {@inheritdoc} |
|
| 20 | */ |
|
| 21 | public function hydrate($object, $source, DocumentHydrator $hydrator) |
|
| 22 | { |
|
| 23 | if (! $object instanceof IncludedResourcesAwareInterface) { |
|
| 24 | throw new InvalidDocumentException(sprintf( |
|
| 25 | 'Given instance of "%s" does not implements "%s"', |
|
| 26 | get_class($object), |
|
| 27 | IncludedResourcesAwareInterface::class |
|
| 28 | )); |
|
| 29 | } |
|
| 30 | ||
| 31 | foreach ($source as $item) |
|
| 32 | { |
|
| 33 | $resource = $hydrator->hydrateResource($item); |
|
| 34 | ||
| 35 | $object->addIncludedResource($resource); |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * {@inheritdoc} |
|
| 41 | */ |
|
| 42 | public function supports(): array |
|
| 43 | { |
|
| 44 | return ['included']; |
|
| 45 | } |
|
| 46 | } |
|
| @@ 17-45 (lines=29) @@ | ||
| 14 | * |
|
| 15 | * @package Mikemirten\Component\JsonApi\Hydrator\Extension |
|
| 16 | */ |
|
| 17 | class MetadataExtension implements ExtensionInterface |
|
| 18 | { |
|
| 19 | /** |
|
| 20 | * {@inheritdoc} |
|
| 21 | */ |
|
| 22 | public function hydrate($object, $source, DocumentHydrator $hydrator) |
|
| 23 | { |
|
| 24 | if (! $object instanceof MetadataAwareInterface) { |
|
| 25 | throw new InvalidDocumentException(sprintf( |
|
| 26 | 'Given instance of "%s" does not implements "%s"', |
|
| 27 | get_class($object), |
|
| 28 | MetadataAwareInterface::class |
|
| 29 | )); |
|
| 30 | } |
|
| 31 | ||
| 32 | foreach ($source as $name => $value) |
|
| 33 | { |
|
| 34 | $object->setMetadataAttribute($name, $value); |
|
| 35 | } |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * {@inheritdoc} |
|
| 40 | */ |
|
| 41 | public function supports(): array |
|
| 42 | { |
|
| 43 | return ['meta']; |
|
| 44 | } |
|
| 45 | } |
|