-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #304 from alleyinteractive/http-client-cond
Adding Conditionable Method Chaining
- Loading branch information
Showing
6 changed files
with
224 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Higher_Order_When_Proxy class file | ||
* | ||
* @package Mantle | ||
*/ | ||
|
||
namespace Mantle\Support; | ||
|
||
/** | ||
* Higher Order When Proxy | ||
* | ||
* Allow a higher-order proxy that can be used conditionally. | ||
*/ | ||
class Higher_Order_When_Proxy { | ||
|
||
/** | ||
* The target being conditionally operated on. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $target; | ||
|
||
/** | ||
* The condition for proxying. | ||
* | ||
* @var bool | ||
*/ | ||
protected $condition; | ||
|
||
/** | ||
* Create a new proxy instance. | ||
* | ||
* @param mixed $target | ||
* @param bool $condition | ||
* @return void | ||
*/ | ||
public function __construct( $target, $condition ) { | ||
$this->target = $target; | ||
$this->condition = $condition; | ||
} | ||
|
||
/** | ||
* Proxy accessing an attribute onto the target. | ||
* | ||
* @param string $key | ||
* @return mixed | ||
*/ | ||
public function __get( $key ) { | ||
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 ) { | ||
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,74 @@ | ||
<?php | ||
/** | ||
* Conditionable trait file. | ||
* | ||
* phpcs:disable Squiz.Commenting.FunctionComment | ||
* | ||
* @package Mantle | ||
*/ | ||
|
||
namespace Mantle\Support\Traits; | ||
|
||
use Closure; | ||
use Mantle\Support\Higher_Order_When_Proxy; | ||
|
||
/** | ||
* Allow a class to conditionally invoke a method fluently. | ||
* | ||
* A method can use the trait to invoke a method conditionally upon itself. | ||
*/ | ||
trait Conditionable { | ||
/** | ||
* Apply the callback if the given "value" is (or resolves to) truthy. | ||
* | ||
* @template TWhenParameter | ||
* @template TWhenReturnType | ||
* | ||
* @param (\Closure($this): TWhenParameter)|TWhenParameter $value | ||
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $callback | ||
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $default | ||
* @return static|TWhenReturnType | ||
*/ | ||
public function when( $value, callable $callback = null, callable $default = null ) { | ||
$value = $value instanceof Closure ? $value( $this ) : $value; | ||
|
||
if ( func_num_args() === 1 ) { | ||
return new Higher_Order_When_Proxy( $this, $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 $value | ||
* @param (callable( $this, TUnlessParameter): TUnlessReturnType)|null $callback | ||
* @param (callable( $this, TUnlessParameter): TUnlessReturnType)|null $default | ||
* @return $this|TUnlessReturnType | ||
*/ | ||
public function unless( $value, callable $callback = null, callable $default = null ) { | ||
$value = $value instanceof Closure ? $value( $this ) : $value; | ||
|
||
if ( func_num_args() === 1 ) { | ||
return new Higher_Order_When_Proxy( $this, ! $value ); | ||
} | ||
|
||
if ( ! $value ) { | ||
return $callback( $this, $value ) ?? $this; | ||
} elseif ( $default ) { | ||
return $default( $this, $value ) ?? $this; | ||
} | ||
|
||
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
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