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
|
|
|
public 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
|
|
|
/** |
104
|
|
|
* @param $array |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
protected function isAll($array) |
108
|
|
|
{ |
109
|
|
|
return (is_array($array) && isset($array[0]) && $array[0] == '*' || $array == '*'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $visibility [visible|hidden] |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function isListedAs($visibility, $action = null) |
117
|
|
|
{ |
118
|
|
|
if (! $action) $action = Artificer::getCurrentAction(); |
119
|
|
|
|
120
|
|
|
$listOptions = Artificer::getModel()->getOption($action); |
121
|
|
|
|
122
|
|
|
if (! $listOptions || ! isset($listOptions[$visibility])) return false; |
123
|
|
|
|
124
|
|
|
$list = $listOptions[$visibility]; |
125
|
|
|
|
126
|
|
|
if ($this->isAll($list)) return true; |
127
|
|
|
|
128
|
|
|
return $this->isInArray($this->field->getName(), $list); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Hidden fields have preference. |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function isVisible() |
137
|
|
|
{ |
138
|
|
|
if ($this->isHidden()) return false; |
139
|
|
|
|
140
|
|
|
return $this->isListedAs('visible'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $value |
145
|
|
|
* @param $array |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function isInArray($value, $array) |
149
|
|
|
{ |
150
|
|
|
return (is_array($array) && in_array($value, $array)) ? true : false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function isHidden() |
157
|
|
|
{ |
158
|
|
|
return $this->isListedAs('hidden'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public static function get($name) |
162
|
|
|
{ |
163
|
|
|
return array_get(\View::getShared(), 'fields')[$name]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function __get($name) { |
167
|
|
|
$accessor = 'get' . studly_case($name); |
168
|
|
|
|
169
|
|
|
if (! method_exists($this->field, $accessor)) { |
170
|
|
|
return null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->field->$accessor(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function __call($method, $args) { |
177
|
|
|
if (! method_exists($this->field, $method)) { |
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->field->$method($args); |
182
|
|
|
} |
183
|
|
|
} |
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.