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 withWidgets() { |
96
|
|
|
$this->withWidgets = true; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function applyWidgets() { |
102
|
|
|
$field = $this; |
103
|
|
|
|
104
|
|
|
foreach ($this->widgets as $widget) { |
105
|
|
|
$widget = Artificer::widgetManager()->get($widget); |
106
|
|
|
$widget->assets(Artificer::assetManager()); |
107
|
|
|
|
108
|
|
|
$field = $widget->field($field); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $field; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $array |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function isAll($array) |
119
|
|
|
{ |
120
|
|
|
return (is_array($array) && isset($array[0]) && $array[0] == '*' || $array == '*'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $visibility [visible|hidden] |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
protected function isListedAs($visibility, $action = null) |
128
|
|
|
{ |
129
|
|
|
if (! $action) $action = Artificer::getCurrentAction(); |
130
|
|
|
|
131
|
|
|
$listOptions = Artificer::getModel()->getOption($action); |
132
|
|
|
|
133
|
|
|
if (! $listOptions || ! isset($listOptions[$visibility])) return false; |
134
|
|
|
|
135
|
|
|
$list = $listOptions[$visibility]; |
136
|
|
|
|
137
|
|
|
if ($this->isAll($list)) return true; |
138
|
|
|
|
139
|
|
|
return $this->isInArray($this->field->getName(), $list); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Hidden fields have preference. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function isVisible() |
148
|
|
|
{ |
149
|
|
|
if ($this->isHidden()) return false; |
150
|
|
|
|
151
|
|
|
return $this->isListedAs('visible'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $value |
156
|
|
|
* @param $array |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function isInArray($value, $array) |
160
|
|
|
{ |
161
|
|
|
return (is_array($array) && in_array($value, $array)) ? true : false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function isHidden() |
168
|
|
|
{ |
169
|
|
|
return $this->isListedAs('hidden'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public static function get($name) |
173
|
|
|
{ |
174
|
|
|
return array_get(\View::getShared(), 'fields')[$name]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function __get($name) { |
178
|
|
|
$accessor = 'get' . studly_case($name); |
179
|
|
|
|
180
|
|
|
if (! method_exists($this->field, $accessor)) { |
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this->field->$accessor(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function __call($method, $args) { |
188
|
|
|
if (! method_exists($this->field, $method)) { |
189
|
|
|
return null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->field->$method($args); |
193
|
|
|
} |
194
|
|
|
} |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.