1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Ui; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Provides an entity with a condition. |
9
|
|
|
* |
10
|
|
|
* Implementation of {@see \Charcoal\Ui\ConditionalizableInterface} |
11
|
|
|
*/ |
12
|
|
|
trait ConditionalizableTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Condition needed to render the entity. |
16
|
|
|
* |
17
|
|
|
* @var string|boolean |
18
|
|
|
*/ |
19
|
|
|
private $condition; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* rendered condition. |
23
|
|
|
* |
24
|
|
|
* @var string|boolean |
25
|
|
|
*/ |
26
|
|
|
private $resolvedCondition; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return boolean |
30
|
|
|
*/ |
31
|
|
|
public function resolvedCondition() |
32
|
|
|
{ |
33
|
|
|
if (!isset($this->resolvedCondition)) { |
34
|
|
|
if (!isset($this->condition)) { |
35
|
|
|
$this->resolvedCondition = true; |
36
|
|
|
} else { |
37
|
|
|
$this->resolvedCondition = $this->parseConditionalLogic( |
38
|
|
|
$this->condition() |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->resolvedCondition; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return boolean|string |
48
|
|
|
*/ |
49
|
|
|
public function condition() |
50
|
|
|
{ |
51
|
|
|
return $this->condition; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param boolean|string $condition A condition to evaluate. |
56
|
|
|
* @throws InvalidArgumentException If the condition is not a string nor boolean. |
57
|
|
|
* @return self |
58
|
|
|
*/ |
59
|
|
|
public function setCondition($condition) |
60
|
|
|
{ |
61
|
|
|
if (!is_bool($condition) && !is_string($condition)) { |
62
|
|
|
throw new InvalidArgumentException( |
63
|
|
|
'Condition must be a string or boolean' |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
unset($this->resolvedCondition); |
68
|
|
|
$this->condition = $condition; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Resolve the conditional logic. |
74
|
|
|
* |
75
|
|
|
* @param mixed $condition The condition. |
76
|
|
|
* @return boolean|null |
77
|
|
|
*/ |
78
|
|
|
final protected function parseConditionalLogic($condition) |
79
|
|
|
{ |
80
|
|
|
if ($condition === null) { |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (is_bool($condition)) { |
85
|
|
|
return $condition; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$not = false; |
89
|
|
|
if (is_string($condition)) { |
90
|
|
|
$not = ($condition[0] === '!'); |
91
|
|
|
if ($not) { |
92
|
|
|
$condition = ltrim($condition, '!'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$result = $this->resolveConditionalLogic($condition); |
97
|
|
|
|
98
|
|
|
return $not ? !$result : $result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Parse the widget's conditional logic. |
103
|
|
|
* |
104
|
|
|
* @param callable|string $condition The callable or renderable condition. |
105
|
|
|
* @return boolean |
106
|
|
|
*/ |
107
|
|
|
protected function resolveConditionalLogic($condition) |
108
|
|
|
{ |
109
|
|
|
if (is_callable([ $this, $condition ])) { |
110
|
|
|
return !!$this->{$condition}(); |
111
|
|
|
} elseif (is_callable($condition)) { |
112
|
|
|
return !!$condition(); |
113
|
|
|
} elseif ($this->form()->obj()->view()) { |
|
|
|
|
114
|
|
|
return !!$this->form()->obj()->renderTemplate($condition); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return !!$condition; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.