1
|
|
|
<?php namespace Mascame\Artificer\Fields; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
use Mascame\Artificer\Artificer; |
5
|
|
|
use Mascame\Artificer\Widget\AbstractWidget; |
6
|
|
|
use Mascame\Formality\Field\FieldInterface; |
7
|
|
|
use Mascame\Formality\Field\TypeInterface; |
8
|
|
|
|
9
|
|
|
class FieldWrapper |
10
|
|
|
{ |
11
|
|
|
use Filterable; |
12
|
|
|
|
13
|
|
|
public static $widgets = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Sometimes ajax limits output, setting this to true will return all |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
public $showFullField = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var FieldInterface|TypeInterface |
24
|
|
|
*/ |
25
|
|
|
protected $field; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Field constructor. |
29
|
|
|
* @param FieldInterface|TypeInterface $field |
30
|
|
|
* @param null $relation |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function __construct(FieldInterface $field) |
33
|
|
|
{ |
34
|
|
|
$this->field = $field; |
35
|
|
|
|
36
|
|
|
$this->boot(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $widget |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function addWidget(AbstractWidget $widget) |
44
|
|
|
{ |
45
|
|
|
if ( ! in_array($widget->name, self::$widgets)) { |
46
|
|
|
self::$widgets[$widget->name] = $widget; |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Used to load custom assets, widgets, ... |
57
|
|
|
* |
58
|
|
|
*/ |
59
|
|
|
protected function boot() |
60
|
|
|
{ |
61
|
|
|
if ( ! $this->field->getOption('widgets')) { |
|
|
|
|
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$widgets = $this->field->getOption('widgets'); |
66
|
|
|
|
67
|
|
|
foreach ($widgets as $widget) { |
68
|
|
|
try { |
69
|
|
|
$this->addWidget(App::make($widget)); |
70
|
|
|
} catch (\Exception $e) { |
71
|
|
|
throw new \Exception("Widget '{$widget}' was not found"); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param null $value |
78
|
|
|
* @return null |
79
|
|
|
*/ |
80
|
|
|
public function show($value = null) |
81
|
|
|
{ |
82
|
|
|
$value = ($value) ? $this->field->setValue($value) : $this->field->getOption('default'); |
|
|
|
|
83
|
|
|
|
84
|
|
|
if ($show = $this->field->getOption('show')) { |
85
|
|
|
if (is_callable($show)) { |
86
|
|
|
return $show($value); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->field->show(); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool|mixed|null|string |
95
|
|
|
*/ |
96
|
|
|
public function output() |
97
|
|
|
{ |
98
|
|
|
if ($this->isHidden()) return null; |
99
|
|
|
|
100
|
|
|
return $this->field->output(); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function withWidgets($output) { |
104
|
|
|
foreach (self::$widgets as $widget) { |
105
|
|
|
$output = $widget->field($output); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $output; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $array |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
protected function isAll($array) |
116
|
|
|
{ |
117
|
|
|
return (is_array($array) && isset($array[0]) && $array[0] == '*' || $array == '*'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $visibility [visible|hidden] |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
protected function isListedAs($visibility, $action = null) |
125
|
|
|
{ |
126
|
|
|
if (! $action) $action = Artificer::getCurrentAction(); |
127
|
|
|
|
128
|
|
|
$listOptions = Artificer::getModel()->getOption($action); |
129
|
|
|
|
130
|
|
|
if (! $listOptions || ! isset($listOptions[$visibility])) return false; |
131
|
|
|
|
132
|
|
|
$list = $listOptions[$visibility]; |
133
|
|
|
|
134
|
|
|
if ($this->isAll($list)) return true; |
135
|
|
|
|
136
|
|
|
return $this->isInArray($this->field->getName(), $list); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Hidden fields have preference. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function isVisible() |
145
|
|
|
{ |
146
|
|
|
if ($this->isHidden()) return false; |
147
|
|
|
|
148
|
|
|
return $this->isListedAs('visible'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $value |
153
|
|
|
* @param $array |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function isInArray($value, $array) |
157
|
|
|
{ |
158
|
|
|
return (is_array($array) && in_array($value, $array)) ? true : false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function isHidden() |
165
|
|
|
{ |
166
|
|
|
return $this->isListedAs('hidden'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public static function get($name) |
170
|
|
|
{ |
171
|
|
|
return array_get(\View::getShared(), 'fields')[$name]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function __get($name) { |
175
|
|
|
$accessor = 'get' . studly_case($name); |
176
|
|
|
|
177
|
|
|
if (! method_exists($this->field, $accessor)) { |
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->field->$accessor(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function __call($method, $args) { |
185
|
|
|
if (! method_exists($this->field, $method)) { |
186
|
|
|
return null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $this->field->$method($args); |
190
|
|
|
} |
191
|
|
|
} |
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.