-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
529 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Mollie\Api\Contracts; | ||
|
||
use Mollie\Api\Http\Response; | ||
use Mollie\Api\Resources\BaseCollection; | ||
use Mollie\Api\Resources\BaseResource; | ||
use Mollie\Api\Resources\LazyCollection; | ||
|
||
interface ResourceDecorator | ||
{ | ||
/** | ||
* @param Response|BaseResource|BaseCollection|LazyCollection $resource | ||
*/ | ||
public static function fromResource($resource): self; | ||
} |
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 |
---|---|---|
|
@@ -3,42 +3,63 @@ | |
namespace Mollie\Api\Http\Requests; | ||
|
||
use Mollie\Api\Http\Request; | ||
use Mollie\Api\Resources\DecorateResource; | ||
|
||
abstract class ResourceHydratableRequest extends Request | ||
{ | ||
/** | ||
* The resource class the request should be hydrated into. | ||
* The original resource class the request should be hydrated into. | ||
* | ||
* @var string|null | ||
*/ | ||
protected $hydratableResource = null; | ||
|
||
/** | ||
* The custom resource class the request should be hydrated into. | ||
* | ||
* @var string|null|DecorateResource | ||
*/ | ||
protected ?string $customHydratableResource = null; | ||
Check failure on line 22 in src/Http/Requests/ResourceHydratableRequest.php
|
||
|
||
public function isHydratable(): bool | ||
{ | ||
return $this->hydratableResource !== null; | ||
return $this->hydratableResource !== null || $this->customHydratableResource !== null; | ||
} | ||
|
||
public function getHydratableResource(): string | ||
/** | ||
* @return string|DecorateResource | ||
*/ | ||
public function getHydratableResource() | ||
{ | ||
if (! $this->isHydratable()) { | ||
throw new \RuntimeException('Resource class is not set.'); | ||
} | ||
|
||
return $this->hydratableResource; | ||
return $this->customHydratableResource ?? $this->hydratableResource; | ||
} | ||
|
||
public function setHydratableResource(string $hydratableResource): self | ||
/** | ||
* @param string|DecorateResource $hydratableResource | ||
* @return self | ||
*/ | ||
public function setHydratableResource($hydratableResource): self | ||
{ | ||
if (! class_exists($hydratableResource)) { | ||
throw new \InvalidArgumentException("The resource class '{$hydratableResource}' does not exist."); | ||
} | ||
|
||
/** @phpstan-ignore-next-line */ | ||
if ($this->hydratableResource && ! is_subclass_of($hydratableResource, $this->hydratableResource)) { | ||
throw new \InvalidArgumentException("The resource class '{$hydratableResource}' does not match the existing resource class '{$this->hydratableResource}'."); | ||
if ($hydratableResource instanceof DecorateResource && ! $hydratableResource->getDecorator()) { | ||
Check failure on line 51 in src/Http/Requests/ResourceHydratableRequest.php
|
||
throw new \InvalidArgumentException("The decorator class is not set."); | ||
} | ||
|
||
$this->hydratableResource = $hydratableResource; | ||
$this->customHydratableResource = $hydratableResource; | ||
|
||
return $this; | ||
} | ||
|
||
public function resetHydratableResource(): self | ||
{ | ||
$this->customHydratableResource = null; | ||
|
||
return $this; | ||
} | ||
|
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,38 @@ | ||
<?php | ||
|
||
namespace Mollie\Api\Resources; | ||
|
||
use Mollie\Api\Contracts\ResourceDecorator; | ||
|
||
class DecorateResource | ||
{ | ||
protected string $decoratedResource; | ||
|
||
protected ?string $decorator = null; | ||
|
||
public function __construct(string $decoratedResource) | ||
{ | ||
$this->decoratedResource = $decoratedResource; | ||
} | ||
|
||
public function with(string $decorator): self | ||
{ | ||
if (! is_subclass_of($decorator, ResourceDecorator::class)) { | ||
throw new \InvalidArgumentException("The decorator class '{$decorator}' does not implement the DecoratedResource interface."); | ||
} | ||
|
||
$this->decorator = $decorator; | ||
|
||
return $this; | ||
} | ||
|
||
public function getDecoratedResource(): string | ||
{ | ||
return $this->decoratedResource; | ||
} | ||
|
||
public function getDecorator(): ?string | ||
{ | ||
return $this->decorator; | ||
} | ||
} |
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
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
Oops, something went wrong.