Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Dec 18, 2024
1 parent 55d3425 commit 94918f1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
12 changes: 12 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,18 @@ parameters:
count: 1
path: tests/Http/Middleware/GuardResponseTest.php

-
message: '#^Access to an undefined property Mollie\\Api\\Resources\\AnyResource\:\:\$customField\.$#'
identifier: property.notFound
count: 1
path: tests/Resources/ResourceFactoryTest.php

-
message: '#^Access to an undefined property Mollie\\Api\\Resources\\AnyResource\:\:\$id\.$#'
identifier: property.notFound
count: 1
path: tests/Resources/ResourceFactoryTest.php

-
message: '#^Property Tests\\Utils\\TestPropertiesClass\:\:\$privateProp is unused\.$#'
identifier: property.unused
Expand Down
8 changes: 2 additions & 6 deletions src/Http/Requests/ResourceHydratableRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class ResourceHydratableRequest extends Request
*
* @var string|null|DecorateResource
*/
protected ?string $customHydratableResource = null;
protected $customHydratableResource = null;

public function isHydratable(): bool
{
Expand All @@ -39,18 +39,14 @@ public function getHydratableResource()
}

/**
* @param string|DecorateResource $hydratableResource
* @param string|DecorateResource $hydratableResource
*/
public function setHydratableResource($hydratableResource): self
{
if (! class_exists($hydratableResource)) {
throw new \InvalidArgumentException("The resource class '{$hydratableResource}' does not exist.");
}

if ($hydratableResource instanceof DecorateResource && ! $hydratableResource->getDecorator()) {
throw new \InvalidArgumentException('The decorator class is not set.');
}

$this->customHydratableResource = $hydratableResource;

return $this;
Expand Down
10 changes: 9 additions & 1 deletion src/Resources/DecorateResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ class DecorateResource

protected ?string $decorator = null;

public function __construct(string $decoratedResource)
public function __construct(string $decoratedResource, ?string $decorator = null)
{
$this->decoratedResource = $decoratedResource;

if ($decorator) {
$this->with($decorator);
}
}

public function with(string $decorator): self
Expand All @@ -33,6 +37,10 @@ public function getDecoratedResource(): string

public function getDecorator(): ?string
{
if (! $this->decorator) {
throw new \InvalidArgumentException('The decorator class is not set.');
}

return $this->decorator;
}
}
3 changes: 2 additions & 1 deletion src/Resources/ResourceHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mollie\Api\Resources;

use Mollie\Api\Contracts\IsIteratable;
use Mollie\Api\Contracts\ResourceDecorator;
use Mollie\Api\Http\Request;
use Mollie\Api\Http\Requests\ResourceHydratableRequest;
use Mollie\Api\Http\Response;
Expand All @@ -12,7 +13,7 @@ class ResourceHydrator
/**
* Hydrate a response into a resource or collection
*
* @return Response|BaseResource|BaseCollection|LazyCollection
* @return Response|BaseResource|BaseCollection|LazyCollection|ResourceDecorator
*/
public function hydrate(ResourceHydratableRequest $request, Response $response)
{
Expand Down

0 comments on commit 94918f1

Please sign in to comment.