1 | <?php |
||
2 | |||
3 | namespace ArgentCrusade\Forms; |
||
4 | |||
5 | use Illuminate\Support\Arr; |
||
6 | use Illuminate\Support\Str; |
||
7 | |||
8 | abstract class Form |
||
9 | { |
||
10 | const DEFAULT_LABEL_COLUMN_SIZE = 2; |
||
11 | const DEFAULT_INPUT_COLUMN_SIZE = 10; |
||
12 | |||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $excludeFromExtra = [ |
||
17 | 'method', 'action', 'class', 'enctype', |
||
18 | ]; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $formId; |
||
22 | |||
23 | /** @var string */ |
||
24 | protected $redirectUrl; |
||
25 | protected $cachedValues = []; |
||
26 | protected $cachedFields = []; |
||
27 | |||
28 | /** |
||
29 | * Get the form's HTTP method. |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | abstract public function method(); |
||
34 | |||
35 | /** |
||
36 | * Get the form's action URL. |
||
37 | * |
||
38 | * @return string |
||
39 | */ |
||
40 | abstract public function action(); |
||
41 | |||
42 | /** |
||
43 | * Get the form's fields. |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | abstract public function fields(); |
||
48 | |||
49 | /** |
||
50 | * Get the cancel button URL. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function cancelUrl() |
||
55 | { |
||
56 | return ''; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Additional form options. |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | public function options() |
||
65 | { |
||
66 | return []; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Determines if current form contains file fields. |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function withUploads() |
||
75 | { |
||
76 | return false; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Form values (editing mode). |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function values() |
||
85 | { |
||
86 | return []; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Unique form ID. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function id() |
||
95 | { |
||
96 | if (!is_null($this->formId)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
97 | return $this->formId; |
||
98 | } |
||
99 | |||
100 | return $this->formId = 'abstract-form-'.rand(); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Get the submit button label. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function submitLabel() |
||
109 | { |
||
110 | return 'Submit'; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get the cancel button label. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public function cancelLabel() |
||
119 | { |
||
120 | return 'Cancel'; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Get the label column size. |
||
125 | * |
||
126 | * @return int |
||
127 | */ |
||
128 | public function labelColumnSize() |
||
129 | { |
||
130 | return static::DEFAULT_LABEL_COLUMN_SIZE; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get the input column size. |
||
135 | * |
||
136 | * @return int |
||
137 | */ |
||
138 | public function inputColumnSize() |
||
139 | { |
||
140 | return static::DEFAULT_INPUT_COLUMN_SIZE; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get the form option value. |
||
145 | * |
||
146 | * @param string $path |
||
147 | * @param null $default |
||
0 ignored issues
–
show
|
|||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function get(string $path, $default = null) |
||
152 | { |
||
153 | return Arr::get($this->options(), $path, $default); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Form's extra attributes. |
||
158 | * |
||
159 | * @param array $attributes |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | public function extraAttributes(array $attributes = []) |
||
164 | { |
||
165 | return collect($this->onlyExtraAttributes($attributes)) |
||
166 | ->map(function (string $value, string $attribute) { |
||
167 | return $attribute.'="'.$value.'"'; |
||
168 | }) |
||
169 | ->implode(' '); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Get only extra form attributes. |
||
174 | * |
||
175 | * @param array $attributes |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function onlyExtraAttributes(array $attributes = []) |
||
180 | { |
||
181 | $attributes = $attributes ?: $this->get('attributes', []); |
||
182 | |||
183 | foreach ($this->excludeFromExtra as $attribute) { |
||
184 | if (isset($attributes[$attribute])) { |
||
185 | unset($attributes[$attribute]); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | return $attributes; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Get the cached form values. |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | protected function cachedValues() |
||
198 | { |
||
199 | if ($this->cachedValues) { |
||
0 ignored issues
–
show
The expression
$this->cachedValues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
200 | return $this->cachedValues; |
||
201 | } |
||
202 | |||
203 | return $this->cachedValues = $this->values(); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get the cached form fields. |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | protected function cachedFields() |
||
212 | { |
||
213 | if ($this->cachedFields) { |
||
0 ignored issues
–
show
The expression
$this->cachedFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
214 | return $this->cachedFields; |
||
215 | } |
||
216 | |||
217 | return $this->cachedFields = $this->fields(); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Set redirect URL. |
||
222 | * |
||
223 | * @param string $url |
||
224 | * |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function setRedirectUrl(string $url) |
||
228 | { |
||
229 | $this->redirectUrl = $url; |
||
230 | |||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Get the redirect URL. |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getRedirectUrl() |
||
240 | { |
||
241 | return $this->redirectUrl; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Get the form field by name. |
||
246 | * |
||
247 | * @param string $name |
||
248 | * |
||
249 | * @return FormField |
||
250 | */ |
||
251 | public function getField(string $name) |
||
252 | { |
||
253 | return Arr::get($this->cachedFields(), $name); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Generates ID for given input element. |
||
258 | * |
||
259 | * @param string $name |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public function inputId(string $name) |
||
264 | { |
||
265 | return Str::camel($this->id().'_input_'.Str::lower($name)); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Get the field value. |
||
270 | * |
||
271 | * @param string $name |
||
272 | * @param null $default |
||
0 ignored issues
–
show
|
|||
273 | * |
||
274 | * @return mixed|string |
||
275 | */ |
||
276 | public function fieldValue(string $name, $default = null) |
||
277 | { |
||
278 | $value = Arr::get($this->cachedValues(), $name, $default); |
||
279 | $oldValue = old($name); |
||
280 | |||
281 | if ($oldValue && method_exists($this, $mutatorMethod = Str::camel('get_'.$name.'_value'))) { |
||
282 | return call_user_func([$this, $mutatorMethod], $oldValue); |
||
283 | } |
||
284 | |||
285 | return $oldValue ?? $value; |
||
286 | } |
||
287 | } |
||
288 |