Total Complexity | 76 |
Total Lines | 568 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like NamedFormElement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NamedFormElement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class NamedFormElement extends FormElement |
||
18 | { |
||
19 | use HtmlAttributes; |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $path; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $name; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $modelAttributeKey; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $label; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $helpText; |
||
44 | |||
45 | /** |
||
46 | * @var mixed |
||
47 | */ |
||
48 | protected $defaultValue; |
||
49 | |||
50 | /** |
||
51 | * @var \Closure |
||
52 | */ |
||
53 | protected $mutator; |
||
54 | |||
55 | /** |
||
56 | * @param string $path |
||
57 | * @param string|null $label |
||
58 | * |
||
59 | * @throws FormElementException |
||
60 | */ |
||
61 | public function __construct($path, $label = null) |
||
62 | { |
||
63 | if (empty($path)) { |
||
64 | throw new FormElementException('You must specify field path'); |
||
65 | } |
||
66 | |||
67 | $this->setPath($path); |
||
68 | $this->setLabel($label); |
||
69 | |||
70 | $parts = explode('.', $path); |
||
71 | $this->setName($this->composeName($parts)); |
||
72 | $this->setModelAttributeKey(end($parts)); |
||
73 | |||
74 | parent::__construct(); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Compose html name from array like this: 'first[second][third]'. |
||
79 | * |
||
80 | * @param array $parts |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | private function composeName(array $parts) |
||
85 | { |
||
86 | $name = array_shift($parts); |
||
87 | |||
88 | while (! empty($parts)) { |
||
89 | $part = array_shift($parts); |
||
90 | $name .= "[$part]"; |
||
91 | } |
||
92 | |||
93 | return $name; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getPath() |
||
100 | { |
||
101 | return $this->path; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $path |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function setPath($path) |
||
110 | { |
||
111 | $this->path = $path; |
||
112 | |||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getName() |
||
120 | { |
||
121 | return $this->name; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param string $name |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function setName($name) |
||
130 | { |
||
131 | $this->name = $name; |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getLabel() |
||
140 | { |
||
141 | return $this->label; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param string $label |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function setLabel($label) |
||
150 | { |
||
151 | $this->label = $label; |
||
152 | |||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | public function getModelAttributeKey() |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param string $key |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function setModelAttributeKey($key) |
||
170 | { |
||
171 | $this->modelAttributeKey = $key; |
||
172 | |||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function getDefaultValue() |
||
180 | { |
||
181 | return $this->defaultValue; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param mixed $defaultValue |
||
186 | * |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function setDefaultValue($defaultValue) |
||
190 | { |
||
191 | $this->defaultValue = $defaultValue; |
||
192 | |||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getHelpText() |
||
200 | { |
||
201 | if ($this->helpText instanceof Htmlable) { |
||
|
|||
202 | return $this->helpText->toHtml(); |
||
203 | } |
||
204 | |||
205 | return $this->helpText; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param string|Htmlable $helpText |
||
210 | * |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function setHelpText($helpText) |
||
214 | { |
||
215 | $this->helpText = $helpText; |
||
216 | |||
217 | return $this; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param string|null $message |
||
222 | * |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function required($message = null) |
||
226 | { |
||
227 | $this->addValidationRule('required', $message); |
||
228 | |||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string|null $message |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function unique($message = null) |
||
238 | { |
||
239 | $this->addValidationRule('_unique'); |
||
240 | |||
241 | if (! is_null($message)) { |
||
242 | $this->addValidationMessage('unique', $message); |
||
243 | } |
||
244 | |||
245 | return $this; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @return array |
||
250 | */ |
||
251 | public function getValidationMessages() |
||
252 | { |
||
253 | $messages = parent::getValidationMessages(); |
||
254 | |||
255 | foreach ($messages as $rule => $message) { |
||
256 | $messages[$this->getName().'.'.$rule] = $message; |
||
257 | unset($messages[$rule]); |
||
258 | } |
||
259 | |||
260 | return $messages; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @return array |
||
265 | */ |
||
266 | public function getValidationLabels() |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * If FormElement has `_unique` rule, it will get all appropriate |
||
273 | * validation rules based on underlying model. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | public function getValidationRules() |
||
278 | { |
||
279 | $rules = parent::getValidationRules(); |
||
280 | |||
281 | foreach ($rules as &$rule) { |
||
282 | if ($rule !== '_unique') { |
||
283 | continue; |
||
284 | } |
||
285 | |||
286 | $model = $this->resolvePath(); |
||
287 | $table = $model->getTable(); |
||
288 | |||
289 | $rule = 'unique:'.$table.','.$this->getModelAttributeKey(); |
||
290 | if ($model->exists) { |
||
291 | $rule .= ','.$model->getKey(); |
||
292 | } |
||
293 | } |
||
294 | unset($rule); |
||
295 | |||
296 | return [$this->getPath() => $rules]; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Get model related to form element. |
||
301 | * |
||
302 | * @return mixed |
||
303 | */ |
||
304 | public function resolvePath() |
||
305 | { |
||
306 | $model = $this->getModel(); |
||
307 | $relations = explode('.', $this->getPath()); |
||
308 | $count = count($relations); |
||
309 | |||
310 | if ($count === 1) { |
||
311 | return $model; |
||
312 | } |
||
313 | |||
314 | foreach ($relations as $relation) { |
||
315 | if ($count === 1) { |
||
316 | return $model; |
||
317 | } |
||
318 | |||
319 | if ($model->exists && ($value = $model->getAttribute($relation)) instanceof Model) { |
||
320 | $model = $value; |
||
321 | |||
322 | $count--; |
||
323 | continue; |
||
324 | } |
||
325 | |||
326 | if (method_exists($model, $relation)) { |
||
327 | $relation = $model->{$relation}(); |
||
328 | |||
329 | if ($relation instanceof Relation) { |
||
330 | $model = $relation->getModel(); |
||
331 | $count--; |
||
332 | continue; |
||
333 | } |
||
334 | } |
||
335 | |||
336 | break; |
||
337 | } |
||
338 | |||
339 | throw new LogicException("Can not resolve path for field '{$this->getPath()}'. Probably relation definition is incorrect"); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @param \Illuminate\Http\Request $request |
||
344 | * |
||
345 | * @return array|string |
||
346 | */ |
||
347 | public function getValueFromRequest(\Illuminate\Http\Request $request) |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @return mixed |
||
358 | */ |
||
359 | public function getValueFromModel() |
||
360 | { |
||
361 | if (! is_null($value = $this->getValueFromRequest(request()))) { |
||
362 | return $value; |
||
363 | } |
||
364 | |||
365 | $model = $this->getModel(); |
||
366 | $path = $this->getPath(); |
||
367 | $value = $this->getDefaultValue(); |
||
368 | |||
369 | if (is_null($model) || ! $model->exists) { |
||
370 | return $value; |
||
371 | } |
||
372 | |||
373 | $relations = explode('.', $path); |
||
374 | $count = count($relations); |
||
375 | |||
376 | if ($count === 1) { |
||
377 | $attribute = $model->getAttribute($this->getModelAttributeKey()); |
||
378 | |||
379 | if (! empty($attribute) || $attribute === 0 || is_null($value)) { |
||
380 | return $attribute; |
||
381 | } |
||
382 | } |
||
383 | |||
384 | foreach ($relations as $relation) { |
||
385 | if ($model->{$relation} instanceof Model) { |
||
386 | $model = $model->{$relation}; |
||
387 | continue; |
||
388 | } |
||
389 | if ($count === 2) { |
||
390 | if (str_contains($relation, '->')) { |
||
391 | $parts = explode('->', $relation); |
||
392 | $relationField = array_shift($array); |
||
393 | $jsonPath = implode('.', $parts); |
||
394 | $attribute = data_get($model->{$relationField}, $jsonPath); |
||
395 | } else { |
||
396 | $attribute = $model->getAttribute($relation); |
||
397 | } |
||
398 | if (! empty($attribute) || is_null($value)) { |
||
399 | return $attribute; |
||
400 | } |
||
401 | } |
||
402 | |||
403 | if (is_null($this->getDefaultValue())) { |
||
404 | throw new LogicException("Can not fetch value for field '{$path}'. Probably relation definition is incorrect"); |
||
405 | } |
||
406 | } |
||
407 | |||
408 | /* |
||
409 | * Implement json parsing |
||
410 | */ |
||
411 | if (strpos($path, '->') !== false) { |
||
412 | $casts = collect($model->getCasts()); |
||
413 | $jsonParts = collect(explode('->', $path)); |
||
414 | |||
415 | $jsonAttr = $model->{$jsonParts->first()}; |
||
416 | |||
417 | $cast = $casts->get($jsonParts->first(), false); |
||
418 | |||
419 | if ($cast == 'object') { |
||
420 | $jsonAttr = json_decode(json_encode($jsonAttr), true); |
||
421 | } elseif ($cast != 'array') { |
||
422 | $jsonAttr = json_decode($jsonAttr); |
||
423 | } |
||
424 | |||
425 | return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.')); |
||
426 | } |
||
427 | |||
428 | return $value; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @param \Illuminate\Http\Request $request |
||
433 | * |
||
434 | * @return void |
||
435 | */ |
||
436 | public function save(\Illuminate\Http\Request $request) |
||
437 | { |
||
438 | $this->setModelAttribute( |
||
439 | $this->getValueFromRequest( |
||
440 | $request |
||
441 | ) |
||
442 | ); |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * @param mixed $value |
||
447 | * |
||
448 | * @return void |
||
449 | */ |
||
450 | public function setModelAttribute($value) |
||
451 | { |
||
452 | $model = $this->getModelByPath( |
||
453 | $this->getPath() |
||
454 | ); |
||
455 | |||
456 | if ($this->isValueSkipped()) { |
||
457 | return; |
||
458 | } |
||
459 | |||
460 | $model->setAttribute( |
||
461 | $this->getModelAttributeKey(), |
||
462 | $this->prepareValue($value) |
||
463 | ); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * @param string $path |
||
468 | * |
||
469 | * @return Model|null |
||
470 | */ |
||
471 | protected function getModelByPath($path) |
||
519 | } |
||
520 | |||
521 | protected function getForeignKeyNameFromRelation($relation) |
||
522 | { |
||
523 | return method_exists($relation, 'getForeignKeyName') |
||
524 | ? $relation->getForeignKeyName() |
||
525 | : $relation->getPlainForeignKey(); |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Field->mutateValue(function($value) { |
||
530 | * return bcrypt($value); |
||
531 | * }). |
||
532 | * |
||
533 | * @param \Closure $mutator |
||
534 | * |
||
535 | * @return $this |
||
536 | */ |
||
537 | public function mutateValue(\Closure $mutator) |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * @return bool |
||
546 | */ |
||
547 | public function hasMutator() |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * @param mixed $value |
||
554 | * |
||
555 | * @return mixed |
||
556 | */ |
||
557 | public function prepareValue($value) |
||
558 | { |
||
559 | if ($this->hasMutator()) { |
||
560 | $value = call_user_func($this->mutator, $value); |
||
561 | } |
||
562 | |||
563 | return $value; |
||
564 | } |
||
565 | |||
566 | /** |
||
567 | * @return array |
||
568 | */ |
||
569 | public function toArray() |
||
585 | ]); |
||
586 | } |
||
587 | } |
||
588 |