| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
class HigherOrderWhenProxy |
| 6 |
{ |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
protected $target; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
protected $condition; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected $hasCondition = false; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected $negateConditionOnCapture; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function __construct($target) |
| 41 |
{ |
| 42 |
$this->target = $target; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function condition($condition) |
| 52 |
{ |
| 53 |
[$this->condition, $this->hasCondition] = [$condition, true]; |
| 54 |
|
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function negateConditionOnCapture() |
| 64 |
{ |
| 65 |
$this->negateConditionOnCapture = true; |
| 66 |
|
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function __get($key) |
| 77 |
{ |
| 78 |
if (! $this->hasCondition) { |
| 79 |
$condition = $this->target->{$key}; |
| 80 |
|
| 81 |
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); |
| 82 |
} |
| 83 |
|
| 84 |
return $this->condition |
| 85 |
? $this->target->{$key} |
| 86 |
: $this->target; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function __call($method, $parameters) |
| 97 |
{ |
| 98 |
if (! $this->hasCondition) { |
| 99 |
$condition = $this->target->{$method}(...$parameters); |
| 100 |
|
| 101 |
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); |
| 102 |
} |
| 103 |
|
| 104 |
return $this->condition |
| 105 |
? $this->target->{$method}(...$parameters) |
| 106 |
: $this->target; |
| 107 |
} |
| 108 |
} |