Debian Codegraph @

lib/3rdParty/composer/illuminate/conditionable/HigherOrderWhenProxy.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 class HigherOrderWhenProxy
6 {
7 /**
8 * The target being conditionally operated on.
9 *
10 * @var mixed
11 */
12 protected $target;
13
14 /**
15 * The condition for proxying.
16 *
17 * @var bool
18 */
19 protected $condition;
20
21 /**
22 * Indicates whether the proxy has a condition.
23 *
24 * @var bool
25 */
26 protected $hasCondition = false;
27
28 /**
29 * Determine whether the condition should be negated.
30 *
31 * @var bool
32 */
33 protected $negateConditionOnCapture;
34
35 /**
36 * Create a new proxy instance.
37 *
38 * @param mixed $target
39 */
40 public function __construct($target)
41 {
42 $this->target = $target;
43 }
44
45 /**
46 * Set the condition on the proxy.
47 *
48 * @param bool $condition
49 * @return $this
50 */
51 public function condition($condition)
52 {
53 [$this->condition, $this->hasCondition] = [$condition, true];
54
55 return $this;
56 }
57
58 /**
59 * Indicate that the condition should be negated.
60 *
61 * @return $this
62 */
63 public function negateConditionOnCapture()
64 {
65 $this->negateConditionOnCapture = true;
66
67 return $this;
68 }
69
70 /**
71 * Proxy accessing an attribute onto the target.
72 *
73 * @param string $key
74 * @return mixed
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 * Proxy a method call on the target.
91 *
92 * @param string $method
93 * @param array $parameters
94 * @return mixed
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 }