From d835ee5568c7161bdd15dd9efdd0b7c9667ad357 Mon Sep 17 00:00:00 2001 From: Martin Boer Date: Sun, 9 Mar 2025 19:41:00 +0100 Subject: [PATCH] :recycle: accept UnitEnum or string for resource functions --- src/Resources/ResourceIdentifier.php | 8 ++++++-- src/Transformers/AbstractTransformer.php | 4 ++-- tests/Stubs/TransformerStub.php | 5 +++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Resources/ResourceIdentifier.php b/src/Resources/ResourceIdentifier.php index 4d3cef8..1b2c935 100644 --- a/src/Resources/ResourceIdentifier.php +++ b/src/Resources/ResourceIdentifier.php @@ -5,12 +5,16 @@ namespace MyParcelCom\JsonApi\Resources; use JsonSerializable; +use MyParcelCom\JsonApi\Traits\EnumTrait; +use UnitEnum; class ResourceIdentifier implements JsonSerializable { + use EnumTrait; + public function __construct( private string $id, - private string $type, + private UnitEnum|string $type, private ?string $parentId = null, ) { } @@ -22,7 +26,7 @@ public function getId(): string public function getType(): string { - return $this->type; + return $this->getEnumValue($this->type); } public function getParentId(): ?string diff --git a/src/Transformers/AbstractTransformer.php b/src/Transformers/AbstractTransformer.php index 78efc2e..bb34f3a 100644 --- a/src/Transformers/AbstractTransformer.php +++ b/src/Transformers/AbstractTransformer.php @@ -90,7 +90,7 @@ protected function getTimestamp(?DateTime $dateTime): ?int protected function transformRelationshipForIdentifier( string $id, - string $type, + UnitEnum|string $type, string $class, string $parentId = null, ): array { @@ -107,7 +107,7 @@ protected function transformRelationshipForIdentifier( return $relationship; } - protected function transformRelationshipForIdentifiers(array $ids, string $type, array $links = null): array + protected function transformRelationshipForIdentifiers(array $ids, UnitEnum|string $type, array $links = []): array { return array_filter([ 'data' => array_map(fn ($id) => (new ResourceIdentifier($id, $type))->jsonSerialize(), $ids), diff --git a/tests/Stubs/TransformerStub.php b/tests/Stubs/TransformerStub.php index 3af9681..a828c12 100644 --- a/tests/Stubs/TransformerStub.php +++ b/tests/Stubs/TransformerStub.php @@ -8,6 +8,7 @@ use Illuminate\Contracts\Routing\UrlGenerator; use MyParcelCom\JsonApi\Resources\ResourceIdentifier; use MyParcelCom\JsonApi\Transformers\AbstractTransformer; +use UnitEnum; class TransformerStub extends AbstractTransformer { @@ -84,14 +85,14 @@ public function getTimestamp(?DateTime $dateTime): ?int public function transformRelationshipForIdentifier( string $id, - string $type, + UnitEnum|string $type, string $class, string $parentId = null, ): array { return parent::transformRelationshipForIdentifier($id, $type, $class, $parentId); } - public function transformRelationshipForIdentifiers(array $ids, string $type, array $links = null): array + public function transformRelationshipForIdentifiers(array $ids, UnitEnum|string $type, array $links = []): array { return parent::transformRelationshipForIdentifiers($ids, $type, $links); }