-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Conditionable trait, and HigherOrderWhenProxy class
- Loading branch information
Showing
4 changed files
with
479 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace SlimFacades\Support; | ||
|
||
class HigherOrderWhenProxy | ||
{ | ||
/** | ||
* The target being conditionally operated on. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $target; | ||
|
||
/** | ||
* The condition for proxying. | ||
* | ||
* @var bool | ||
*/ | ||
protected $condition; | ||
|
||
/** | ||
* Indicates whether the proxy has a condition. | ||
* | ||
* @var bool | ||
*/ | ||
protected $hasCondition = false; | ||
|
||
/** | ||
* Determine whether the condition should be negated. | ||
* | ||
* @var bool | ||
*/ | ||
protected $negateConditionOnCapture; | ||
|
||
/** | ||
* Create a new proxy instance. | ||
* | ||
* @param mixed $target | ||
* @return void | ||
*/ | ||
public function __construct($target) | ||
{ | ||
$this->target = $target; | ||
} | ||
|
||
/** | ||
* Set the condition on the proxy. | ||
* | ||
* @param bool $condition | ||
* @return $this | ||
*/ | ||
public function condition($condition) | ||
{ | ||
[$this->condition, $this->hasCondition] = [$condition, true]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Indicate that the condition should be negated. | ||
* | ||
* @return $this | ||
*/ | ||
public function negateConditionOnCapture() | ||
{ | ||
$this->negateConditionOnCapture = true; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Proxy accessing an attribute onto the target. | ||
* | ||
* @param string $key | ||
* @return mixed | ||
*/ | ||
public function __get($key) | ||
{ | ||
if (! $this->hasCondition) { | ||
$condition = $this->target->{$key}; | ||
|
||
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); | ||
} | ||
|
||
return $this->condition | ||
? $this->target->{$key} | ||
: $this->target; | ||
} | ||
|
||
/** | ||
* Proxy a method call on the target. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
if (! $this->hasCondition) { | ||
$condition = $this->target->{$method}(...$parameters); | ||
|
||
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); | ||
} | ||
|
||
return $this->condition | ||
? $this->target->{$method}(...$parameters) | ||
: $this->target; | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
|
||
namespace SlimFacades\Support\Traits; | ||
|
||
use Closure; | ||
use SlimFacades\Support\HigherOrderWhenProxy; | ||
|
||
trait Conditionable | ||
{ | ||
/** | ||
* Apply the callback if the given "value" is (or resolves to) truthy. | ||
* | ||
* @template TWhenParameter | ||
* @template TWhenReturnType | ||
* | ||
* @param (\Closure($this): TWhenParameter)|TWhenParameter|null $value | ||
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $callback | ||
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $default | ||
* @return $this|TWhenReturnType | ||
*/ | ||
public function when($value = null, callable $callback = null, callable $default = null) | ||
{ | ||
$value = $value instanceof Closure ? $value($this) : $value; | ||
|
||
if (func_num_args() === 0) { | ||
return new HigherOrderWhenProxy($this); | ||
} | ||
|
||
if (func_num_args() === 1) { | ||
return (new HigherOrderWhenProxy($this))->condition($value); | ||
} | ||
|
||
if ($value) { | ||
return $callback($this, $value) ?? $this; | ||
} elseif ($default) { | ||
return $default($this, $value) ?? $this; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Apply the callback if the given "value" is (or resolves to) falsy. | ||
* | ||
* @template TUnlessParameter | ||
* @template TUnlessReturnType | ||
* | ||
* @param (\Closure($this): TUnlessParameter)|TUnlessParameter|null $value | ||
* @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $callback | ||
* @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $default | ||
* @return $this|TUnlessReturnType | ||
*/ | ||
public function unless($value = null, callable $callback = null, callable $default = null) | ||
{ | ||
$value = $value instanceof Closure ? $value($this) : $value; | ||
|
||
if (func_num_args() === 0) { | ||
return (new HigherOrderWhenProxy($this))->negateConditionOnCapture(); | ||
} | ||
|
||
if (func_num_args() === 1) { | ||
return (new HigherOrderWhenProxy($this))->condition(! $value); | ||
} | ||
|
||
if (! $value) { | ||
return $callback($this, $value) ?? $this; | ||
} elseif ($default) { | ||
return $default($this, $value) ?? $this; | ||
} | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.