-
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
145 additions
and
30 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Mollie\Api\Resources; | ||
|
||
use Mollie\Api\Contracts\IsWrapper; | ||
use Mollie\Api\Traits\ForwardsCalls; | ||
|
||
abstract class ResourceWrapper implements IsWrapper | ||
{ | ||
use ForwardsCalls; | ||
|
||
protected $resource; | ||
|
||
public function setResource($resource): static | ||
{ | ||
$this->resource = $resource; | ||
|
||
return $this; | ||
} | ||
|
||
public function __call($name, $arguments) | ||
{ | ||
return $this->forwardDecoratedCallTo($this->resource, $name, $arguments); | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
namespace Mollie\Api\Resources; | ||
|
||
use Mollie\Api\Contracts\IsWrapper; | ||
|
||
class WrapResource | ||
{ | ||
protected string $wrappedResource; | ||
|
||
protected ?string $wrapper = null; | ||
|
||
public function __construct(string $decoratedResource, ?string $wrapper = null) | ||
{ | ||
$this->wrappedResource = $decoratedResource; | ||
|
||
if ($wrapper) { | ||
$this->with($wrapper); | ||
} | ||
} | ||
|
||
public function with(string $wrapper): self | ||
{ | ||
if (! is_subclass_of($wrapper, IsWrapper::class)) { | ||
throw new \InvalidArgumentException("The wrapper class '{$wrapper}' does not implement the IsWrapper interface."); | ||
} | ||
|
||
$this->wrapper = $wrapper; | ||
|
||
return $this; | ||
} | ||
|
||
public function getWrappedResource(): string | ||
{ | ||
return $this->wrappedResource; | ||
} | ||
|
||
public function getWrapper(): ?string | ||
{ | ||
if (! $this->wrapper) { | ||
throw new \InvalidArgumentException('The wrapper class is not set.'); | ||
} | ||
|
||
return $this->wrapper; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace Mollie\Api\Traits; | ||
|
||
use BadMethodCallException; | ||
|
||
trait ForwardsCalls | ||
{ | ||
/** | ||
* Forward a method call to the given object. | ||
* | ||
* @param mixed $object | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
* | ||
* @throws \BadMethodCallException | ||
*/ | ||
protected function forwardCallTo($object, $method, $parameters) | ||
{ | ||
if (! method_exists($object, $method)) { | ||
throw new BadMethodCallException("Method {$method} does not exist on {$object}."); | ||
} | ||
|
||
return $object->{$method}(...$parameters); | ||
} | ||
|
||
/** | ||
* Forward a method call to the given object, returning $this if the forwarded call returned itself. | ||
* | ||
* @param mixed $object | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
* | ||
* @throws \BadMethodCallException | ||
*/ | ||
protected function forwardDecoratedCallTo($object, $method, $parameters) | ||
{ | ||
$result = $this->forwardCallTo($object, $method, $parameters); | ||
|
||
return $result === $object ? $this : $result; | ||
} | ||
} |
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