1 | <?php |
||
12 | trait CanCascadeFields |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $conditions = []; |
||
18 | |||
19 | /** |
||
20 | * @param $operator |
||
21 | * @param $value |
||
22 | * @param $closure |
||
23 | * |
||
24 | * @return $this |
||
25 | */ |
||
26 | public function when($operator, $value, $closure = null) |
||
27 | { |
||
28 | if (func_num_args() == 2) { |
||
29 | $closure = $value; |
||
30 | $value = $operator; |
||
31 | $operator = '='; |
||
32 | } |
||
33 | |||
34 | $this->formatValues($operator, $value); |
||
35 | |||
36 | $this->addDependents($operator, $value, $closure); |
||
37 | |||
38 | return $this; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param string $operator |
||
43 | * @param mixed $value |
||
44 | */ |
||
45 | protected function formatValues(string $operator, &$value) |
||
46 | { |
||
47 | if (in_array($operator, ['in', 'notIn'])) { |
||
48 | $value = Arr::wrap($value); |
||
49 | } |
||
50 | |||
51 | if (is_array($value)) { |
||
52 | $value = array_map('strval', $value); |
||
53 | } else { |
||
54 | $value = strval($value); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param string $operator |
||
60 | * @param mixed $value |
||
61 | * @param \Closure $closure |
||
62 | */ |
||
63 | protected function addDependents(string $operator, $value, \Closure $closure) |
||
64 | { |
||
65 | $this->conditions[] = compact('operator', 'value', 'closure'); |
||
66 | |||
67 | $this->form->cascadeGroup($closure, [ |
||
68 | 'column' => $this->column(), |
||
|
|||
69 | 'index' => count($this->conditions) - 1, |
||
70 | 'class' => $this->getCascadeClass($value), |
||
71 | ]); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function fill($data) |
||
83 | |||
84 | /** |
||
85 | * @param mixed $value |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | protected function getCascadeClass($value) |
||
97 | |||
98 | /** |
||
99 | * Apply conditions to dependents fields. |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | protected function applyCascadeConditions() |
||
114 | |||
115 | /** |
||
116 | * @param CascadeGroup $group |
||
117 | * |
||
118 | * @throws \Exception |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | protected function hitsCondition(CascadeGroup $group) |
||
157 | |||
158 | /** |
||
159 | * Add cascade scripts to contents. |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | protected function addCascadeScript() |
||
223 | |||
224 | /** |
||
225 | * @return string |
||
226 | */ |
||
227 | protected function getFormFrontValue() |
||
250 | } |
||
251 |
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.