|
1
|
|
|
<?php namespace Mascame\Artificer\Fields; |
|
2
|
|
|
|
|
3
|
|
|
use Mascame\Artificer\Artificer; |
|
4
|
|
|
use Mascame\Formality\Field\FieldInterface; |
|
5
|
|
|
use Mascame\Formality\Field\TypeInterface; |
|
6
|
|
|
|
|
7
|
|
|
class FieldWrapper |
|
8
|
|
|
{ |
|
9
|
|
|
use Filterable; |
|
10
|
|
|
|
|
11
|
|
|
protected $widgets = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Sometimes ajax limits output, setting this to true will return all |
|
15
|
|
|
* |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
public $showFullField = false; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $withWidgets = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var FieldInterface|TypeInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
public $field; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Field constructor. |
|
32
|
|
|
* @param FieldInterface|TypeInterface $field |
|
33
|
|
|
* @param null $relation |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(FieldInterface $field) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->field = $field; |
|
38
|
|
|
|
|
39
|
|
|
$this->widgets = $this->getInstalledWidgets(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Only get widgets that are installed |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getInstalledWidgets() |
|
48
|
|
|
{ |
|
49
|
|
|
$installedWidgets = []; |
|
50
|
|
|
$widgetManager = Artificer::widgetManager(); |
|
51
|
|
|
$widgets = $this->field->getOption('widgets', []); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
foreach ($widgets as $widget) { |
|
54
|
|
|
if ($widgetManager->isInstalled($widget)) { |
|
55
|
|
|
$installedWidgets[] = $widget; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $installedWidgets; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param null $value |
|
64
|
|
|
* @return null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function show($value = null) |
|
67
|
|
|
{ |
|
68
|
|
|
$value = ($value) ? $this->field->setValue($value) : $this->field->getOption('default'); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
if ($show = $this->field->getOption('show')) { |
|
71
|
|
|
if (is_callable($show)) { |
|
72
|
|
|
return $show($value); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->field->show(); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return bool|mixed|null|string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function output() |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->isHidden()) return null; |
|
85
|
|
|
|
|
86
|
|
|
$field = $this; |
|
87
|
|
|
|
|
88
|
|
|
if ($this->withWidgets) { |
|
89
|
|
|
$field = $this->applyWidgets(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $field->field->output(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function protectGuarded() { |
|
96
|
|
|
if (! $this->isFillable()) { |
|
97
|
|
|
$this->field->setOptions([ |
|
|
|
|
|
|
98
|
|
|
'attributes' => array_merge($this->field->getAttributes(), ['disabled' => 'disabled']) |
|
|
|
|
|
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function withWidgets() { |
|
106
|
|
|
$this->withWidgets = true; |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function applyWidgets() { |
|
112
|
|
|
$field = $this; |
|
113
|
|
|
|
|
114
|
|
|
foreach ($this->widgets as $widget) { |
|
115
|
|
|
$widget = Artificer::widgetManager()->get($widget); |
|
116
|
|
|
$widget->assets(Artificer::assetManager()); |
|
117
|
|
|
|
|
118
|
|
|
$field = $widget->field($field); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $field; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param $array |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function isAll($array) |
|
129
|
|
|
{ |
|
130
|
|
|
return (is_array($array) && isset($array[0]) && $array[0] == '*' || $array == '*'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $visibility [visible|hidden] |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function isListedAs($visibility, $action = null) |
|
138
|
|
|
{ |
|
139
|
|
|
if (! $action) $action = Artificer::getCurrentAction(); |
|
140
|
|
|
|
|
141
|
|
|
$listOptions = Artificer::getModelManager()->getOption($action); |
|
142
|
|
|
|
|
143
|
|
|
if (! $listOptions || ! isset($listOptions[$visibility])) return false; |
|
144
|
|
|
|
|
145
|
|
|
$list = $listOptions[$visibility]; |
|
146
|
|
|
|
|
147
|
|
|
if ($this->isAll($list)) return true; |
|
148
|
|
|
|
|
149
|
|
|
return $this->isInArray($this->field->getName(), $list); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Hidden fields have preference. |
|
154
|
|
|
* |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
|
|
public function isVisible() |
|
158
|
|
|
{ |
|
159
|
|
|
if ($this->isHidden()) return false; |
|
160
|
|
|
|
|
161
|
|
|
return $this->isListedAs('visible'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param $value |
|
166
|
|
|
* @param $array |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
|
|
public function isInArray($value, $array) |
|
170
|
|
|
{ |
|
171
|
|
|
return (is_array($array) && in_array($value, $array)) ? true : false; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
|
|
public function isHidden() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->isListedAs('hidden'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public static function get($name) |
|
183
|
|
|
{ |
|
184
|
|
|
return array_get(\View::getShared(), 'fields')[$name]; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function __get($name) { |
|
188
|
|
|
$accessor = 'get' . studly_case($name); |
|
189
|
|
|
|
|
190
|
|
|
return $this->useFieldMethod($accessor); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
protected function useFieldMethod($method, $args = []) { |
|
194
|
|
|
if (! method_exists($this->field, $method)) { |
|
195
|
|
|
return null; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return (empty($args)) ? $this->field->$method() : $this->field->$method($args); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function __call($method, $args) { |
|
202
|
|
|
return $this->useFieldMethod($method, $args); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function isFillable() { |
|
206
|
|
|
$fillable = Artificer::getModelManager()->getFillable(); |
|
207
|
|
|
|
|
208
|
|
|
return $this->isAll($fillable) || in_array($this->field->getName(), $fillable); |
|
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param array|string $classes |
|
|
|
|
|
|
213
|
|
|
*/ |
|
214
|
|
|
protected function mergeClassAttribute($class) { |
|
215
|
|
|
if (! is_array($class)) $class = [$class]; |
|
216
|
|
|
|
|
217
|
|
|
$attributes = $this->field->getAttributes(); |
|
|
|
|
|
|
218
|
|
|
$classes = isset($attributes['class']) ? explode(' ', $attributes['class']) : []; |
|
219
|
|
|
|
|
220
|
|
|
return join(' ', array_merge($classes, $class)); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $attribute |
|
225
|
|
|
* @param array|string $value |
|
226
|
|
|
*/ |
|
227
|
|
|
public function addAttribute($attribute, $value) { |
|
228
|
|
|
if ($attribute == 'class') { |
|
229
|
|
|
$value = $this->mergeClassAttribute($value); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
$this->field->setOptions(['attributes' => [$attribute => $value]]); |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.