1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use SleepingOwl\Admin\Traits\Assets; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use SleepingOwl\Admin\Traits\Renderable; |
9
|
|
|
use SleepingOwl\Admin\Traits\VisibleCondition; |
10
|
|
|
use SleepingOwl\Admin\Contracts\FormElementInterface; |
11
|
|
|
|
12
|
|
|
abstract class FormElement implements FormElementInterface |
13
|
|
|
{ |
14
|
|
|
use Assets, VisibleCondition, Renderable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \SleepingOwl\Admin\Contracts\TemplateInterface |
18
|
|
|
*/ |
19
|
|
|
protected $template; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Model |
23
|
|
|
*/ |
24
|
|
|
protected $model; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $validationRules = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $validationMessages = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $readonly = false; |
40
|
|
|
|
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
$this->initializePackage(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function initialize() |
47
|
|
|
{ |
48
|
|
|
$this->includePackage(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getValidationMessages() |
55
|
|
|
{ |
56
|
|
|
return $this->validationMessages; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $rule |
61
|
|
|
* @param string $message |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function addValidationMessage($rule, $message) |
66
|
|
|
{ |
67
|
|
|
if (($pos = strpos($rule, ':')) !== false) { |
68
|
|
|
$rule = substr($rule, 0, $pos); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->validationMessages[$rule] = $message; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $validationMessages |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setValidationMessages(array $validationMessages) |
82
|
|
|
{ |
83
|
|
|
$this->validationMessages = $validationMessages; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getValidationLabels() |
92
|
|
|
{ |
93
|
|
|
return []; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function getValidationRules() |
100
|
|
|
{ |
101
|
|
|
return $this->validationRules; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $rule |
106
|
|
|
* @param string|null $message |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function addValidationRule($rule, $message = null) |
111
|
|
|
{ |
112
|
|
|
$this->validationRules[] = $rule; |
113
|
|
|
|
114
|
|
|
if (is_null($message)) { |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->addValidationMessage($rule, $message); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array|string $validationRules |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setValidationRules($validationRules) |
127
|
|
|
{ |
128
|
|
|
if (! is_array($validationRules)) { |
129
|
|
|
$validationRules = func_get_args(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->validationRules = []; |
133
|
|
|
foreach ($validationRules as $rule) { |
134
|
|
|
$rules = explode('|', $rule); |
135
|
|
|
|
136
|
|
|
foreach ($rules as $rule) { |
137
|
|
|
$this->addValidationRule($rule); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return Model |
146
|
|
|
*/ |
147
|
|
|
public function getModel() |
148
|
|
|
{ |
149
|
|
|
return $this->model; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param Model $model |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function setModel(Model $model) |
158
|
|
|
{ |
159
|
|
|
$this->model = $model; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function isReadonly() |
168
|
|
|
{ |
169
|
|
|
if (is_callable($this->readonly)) { |
170
|
|
|
return (bool) call_user_func($this->readonly, $this->getModel()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return (bool) $this->readonly; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param Closure|bool $readonly |
178
|
|
|
* |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
|
|
public function setReadonly($readonly) |
182
|
|
|
{ |
183
|
|
|
$this->readonly = $readonly; |
|
|
|
|
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
public function getValue() |
192
|
|
|
{ |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
public function save() |
199
|
|
|
{ |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public function afterSave() |
206
|
|
|
{ |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
public function toArray() |
213
|
|
|
{ |
214
|
|
|
return [ |
215
|
|
|
'value' => $this->getValue(), |
216
|
|
|
'readonly' => $this->isReadonly(), |
217
|
|
|
'model' => $this->getModel(), |
218
|
|
|
]; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.