Completed
Push — master ( 96be0a...54b1af )
by Kristijan
04:31
created

FormField::prepareOptions()   D

Complexity

Conditions 13
Paths 336

Size

Total Lines 62
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 35
nc 336
nop 1
dl 0
loc 62
rs 4.5568
c 0
b 0
f 0
ccs 46
cts 46
cp 1
crap 13

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kris\LaravelFormBuilder\Fields;
4
5
use Kris\LaravelFormBuilder\Form;
6
use Illuminate\Database\Eloquent\Model;
7
use Kris\LaravelFormBuilder\FormHelper;
8
use Kris\LaravelFormBuilder\RulesParser;
9
use Illuminate\Database\Eloquent\Collection;
10
11
/**
12
 * Class FormField
13
 *
14
 * @package Kris\LaravelFormBuilder\Fields
15
 */
16
abstract class FormField
17
{
18
    /**
19
     * Name of the field
20
     *
21
     * @var
22
     */
23
    protected $name;
24
25
    /**
26
     * Type of the field
27
     *
28
     * @var
29
     */
30
    protected $type;
31
32
    /**
33
     * All options for the field
34
     *
35
     * @var
36
     */
37
    protected $options = [];
38
39
    /**
40
     * Is field rendered
41
     *
42
     * @var bool
43
     */
44
    protected $rendered = false;
45
46
    /**
47
     * @var Form
48
     */
49
    protected $parent;
50
51
    /**
52
     * @var string
53
     */
54
    protected $template;
55
56
    /**
57
     * @var FormHelper
58
     */
59
    protected $formHelper;
60
61
    /**
62
     * Name of the property for value setting
63
     *
64
     * @var string
65
     */
66
    protected $valueProperty = 'value';
67
68
    /**
69
     * Name of the property for default value
70
     *
71
     * @var string
72
     */
73
    protected $defaultValueProperty = 'default_value';
74
75
    /**
76
     * Is default value set?
77
     * @var bool
78
     */
79
    protected $hasDefault = false;
80
81
    /**
82
     * @var \Closure|null
83
     */
84
    protected $valueClosure = null;
85
86
    /**
87
     * @param             $name
88
     * @param             $type
89
     * @param Form        $parent
90
     * @param array       $options
91
     */
92 84
    public function __construct($name, $type, Form $parent, array $options = [])
93
    {
94 84
        $this->name = $name;
95 84
        $this->type = $type;
96 84
        $this->parent = $parent;
97 84
        $this->formHelper = $this->parent->getFormHelper();
98 84
        $this->setTemplate();
99 84
        $this->setDefaultOptions($options);
100 84
        $this->setupValue();
101 78
    }
102
103 84
    protected function setupValue()
104
    {
105 84
        $value = $this->getOption($this->valueProperty);
106 84
        $isChild = $this->getOption('is_child');
107
108 84
        if ($value instanceof \Closure) {
109
            $this->valueClosure = $value;
110
        }
111
112 84
        if (($value === null || $value instanceof \Closure) && !$isChild) {
113 75
            $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name));
114 78
        } elseif (!$isChild) {
115 12
            $this->hasDefault = true;
116 12
        }
117 78
    }
118
119
    /**
120
     * Get the template, can be config variable or view path
121
     *
122
     * @return string
123
     */
124
    abstract protected function getTemplate();
125
126
    /**
127
     * @return string
128
     */
129 31
    protected function getViewTemplate()
130
    {
131 31
        return $this->parent->getTemplatePrefix() . $this->getOption('template', $this->template);
132
    }
133
134
    /**
135
     * @param array $options
136
     * @param bool  $showLabel
137
     * @param bool  $showField
138
     * @param bool  $showError
139
     * @return string
140
     */
141 31
    public function render(array $options = [], $showLabel = true, $showField = true, $showError = true)
142
    {
143 31
        $this->prepareOptions($options);
144 31
        $value = $this->getValue();
145 31
        $defaultValue = $this->getDefaultValue();
146
147 31
        if ($showField) {
148 31
            $this->rendered = true;
149 31
        }
150
151
        // Override default value with value
152 31
        if (!$this->isValidValue($value) && $this->isValidValue($defaultValue)) {
153
            $this->setOption($this->valueProperty, $defaultValue);
154
        }
155
156 31
        if (!$this->needsLabel()) {
157 8
            $showLabel = false;
158 8
        }
159
160 31
        if ($showError) {
161 30
            $showError = $this->parent->haveErrorsEnabled();
162 30
        }
163
164 31
        $data = $this->getRenderData();
165
166 31
        return $this->formHelper->getView()->make(
167 31
            $this->getViewTemplate(),
168
            $data + [
169 31
                'name' => $this->name,
170 31
                'nameKey' => $this->getNameKey(),
171 31
                'type' => $this->type,
172 31
                'options' => $this->options,
173 31
                'showLabel' => $showLabel,
174 31
                'showField' => $showField,
175
                'showError' => $showError
176 31
            ]
177 31
        )->render();
178 1
    }
179
180
    /**
181
     * Return the extra render data for this form field, passed into the field's template directly.
182
     *
183
     * @return array
184
     */
185 31
    protected function getRenderData() {
186 31
        return [];
187
    }
188
189
    /**
190
     * Get the attribute value from the model by name
191
     *
192
     * @param mixed $model
193
     * @param string $name
194
     * @return mixed
195
     */
196 77
    protected function getModelValueAttribute($model, $name)
197
    {
198 77
        $transformedName = $this->transformKey($name);
199 77
        if (is_string($model)) {
200
            return $model;
201 77
        } elseif (is_object($model)) {
202 2
            return object_get($model, $transformedName);
203 77
        } elseif (is_array($model)) {
204 76
            return array_get($model, $transformedName);
205
        }
206 5
    }
207
208
    /**
209
     * Transform array like syntax to dot syntax
210
     *
211
     * @param $key
212
     * @return mixed
213
     */
214 84
    protected function transformKey($key)
215
    {
216 84
        return $this->formHelper->transformToDotSyntax($key);
217
    }
218
219
    /**
220
     * Prepare options for rendering
221
     *
222
     * @param array $options
223
     * @return array
224
     */
225 84
    protected function prepareOptions(array $options = [])
226
    {
227 84
        $helper = $this->formHelper;
228 84
        $rulesParser = new RulesParser($this);
229 84
        $rules = $this->getOption('rules');
230 84
        $parsedRules = $rules ? $rulesParser->parse($rules) : [];
231
232 84
        $this->options = $helper->mergeOptions($this->options, $options);
233
234 84
        foreach (['attr', 'label_attr', 'wrapper'] as $appendable) {
235
            // Append values to the 'class' attribute
236 84
            if ($this->getOption("{$appendable}.class_append")) {
237
                // Combine the current class attribute with the appends
238 3
                $append = $this->getOption("{$appendable}.class_append");
239 3
                $classAttribute = $this->getOption("{$appendable}.class", '').' '.$append;
240 3
                $this->setOption("{$appendable}.class", $classAttribute);
241
242
                // Then remove the class_append option to prevent it from showing up as an attribute in the HTML
243 3
                $this->setOption("{$appendable}.class_append", null);
244 3
            }
245 84
        }
246
247 84
        if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) {
248 2
            $this->name = $this->name.'[]';
249 2
            $this->setOption('tmp.multipleBracesSet', true);
250 2
        }
251
252 84
        if ($this->parent->haveErrorsEnabled()) {
253 84
            $this->addErrorClass();
254 84
        }
255
256 84
        if ($this->getOption('required') === true || isset($parsedRules['required'])) {
257 4
            $lblClass = $this->getOption('label_attr.class', '');
258 4
            $requiredClass = $helper->getConfig('defaults.required_class', 'required');
259
260 4
            if (! str_contains($lblClass, $requiredClass)) {
261 4
                $lblClass .= ' '.$requiredClass;
262 4
                $this->setOption('label_attr.class', $lblClass);
263 4
            }
264
265 4
            if ($this->parent->clientValidationEnabled()) {
266 3
                $this->setOption('attr.required', 'required');
267
268 3
                if ($parsedRules) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parsedRules 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 empty(..) or ! empty(...) instead.

Loading history...
269 1
                    $attrs = $this->getOption('attr') + $parsedRules;
270 1
                    $this->setOption('attr', $attrs);
271 1
                }
272 3
            }
273 4
        }
274
275 84
        $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper')));
276 84
        $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors')));
277
278 84
        if ($this->getOption('help_block.text')) {
279 17
            $this->setOption(
280 17
                'help_block.helpBlockAttrs',
281
                $helper->prepareAttributes($this->getOption('help_block.attr'))
282 84
            );
283 1
        }
284 1
285 1
        return $this->options;
286 1
    }
287 1
288
    /**
289 84
     * Get name of the field
290
     *
291
     * @return string
292
     */
293
    public function getName()
294
    {
295
        return $this->name;
296
    }
297 27
298
    /**
299 27
     * Set name of the field
300
     *
301
     * @param string $name
302
     * @return $this
303
     */
304
    public function setName($name)
305
    {
306
        $this->name = $name;
307
308 11
        return $this;
309
    }
310 11
311
    /**
312 11
     * Get dot notation key for fields
313
     *
314
     * @return string
315
     **/
316
    public function getNameKey()
317
    {
318
        return $this->transformKey($this->name);
319
    }
320 49
321
    /**
322 49
     * Get field options
323
     *
324
     * @return array
325
     */
326
    public function getOptions()
327
    {
328
        return $this->options;
329
    }
330 12
331
    /**
332 12
     * Get single option from options array. Can be used with dot notation ('attr.class')
333
     *
334
     * @param        $option
335
     * @param mixed  $default
336
     *
337
     * @return mixed
338
     */
339
    public function getOption($option, $default = null)
340
    {
341
        return array_get($this->options, $option, $default);
342
    }
343 84
344
    /**
345 84
     * Set field options
346
     *
347
     * @param array $options
348
     * @return $this
349
     */
350
    public function setOptions($options)
351
    {
352
        $this->options = $this->prepareOptions($options);
353
354 11
        return $this;
355
    }
356 11
357
    /**
358 11
     * Set single option on the field
359
     *
360
     * @param string $name
361
     * @param mixed $value
362
     * @return $this
363
     */
364
    public function setOption($name, $value)
365
    {
366
        array_set($this->options, $name, $value);
367
368 84
        return $this;
369
    }
370 84
371
    /**
372 84
     * Get the type of the field
373
     *
374
     * @return string
375
     */
376
    public function getType()
377
    {
378
        return $this->type;
379
    }
380 51
381
    /**
382 51
     * Set type of the field
383
     *
384
     * @param mixed $type
385
     * @return $this
386
     */
387
    public function setType($type)
388
    {
389
        if ($this->formHelper->getFieldType($type)) {
390
            $this->type = $type;
391 1
        }
392
393 1
        return $this;
394 1
    }
395 1
396
    /**
397 1
     * @return Form
398
     */
399
    public function getParent()
400
    {
401
        return $this->parent;
402
    }
403 84
404
    /**
405 84
     * Check if the field is rendered
406
     *
407
     * @return bool
408
     */
409
    public function isRendered()
410
    {
411
        return $this->rendered;
412
    }
413 4
414
    /**
415 4
     * Default options for field
416
     *
417
     * @return array
418
     */
419
    protected function getDefaults()
420
    {
421
        return [];
422
    }
423 64
424
    /**
425 64
     * Defaults used across all fields
426
     *
427
     * @return array
428
     */
429
    private function allDefaults()
430
    {
431
        return [
432
            'wrapper' => ['class' => $this->formHelper->getConfig('defaults.wrapper_class')],
433 84
            'attr' => ['class' => $this->formHelper->getConfig('defaults.field_class')],
434
            'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [
435
                'class' => $this->formHelper->getConfig('defaults.help_block_class')
436 84
            ]],
437 84
            'value' => null,
438 84
            'default_value' => null,
439 84
            'label' => null,
440 84
            'label_show' => true,
441 84
            'is_child' => false,
442 84
            'label_attr' => ['class' => $this->formHelper->getConfig('defaults.label_class')],
443 84
            'errors' => ['class' => $this->formHelper->getConfig('defaults.error_class')],
444 84
            'rules' => [],
445 84
            'error_messages' => []
446 84
        ];
447 84
    }
448 84
449 84
    /**
450 84
     * Get real name of the field without form namespace
451
     *
452
     * @return string
453
     */
454
    public function getRealName()
455
    {
456
        return $this->getOption('real_name', $this->name);
457
    }
458 83
459
    /**
460 83
     * @param $value
461
     * @return $this
462
     */
463
    public function setValue($value)
464
    {
465
        if ($this->hasDefault) {
466
            return $this;
467 78
        }
468
469 78
        $closure = $this->valueClosure;
470 1
471
        if ($closure instanceof \Closure) {
472
            $value = $closure($value ?: null);
473 78
        }
474
475 78
        if (!$this->isValidValue($value)) {
476
            $value = $this->getOption($this->defaultValueProperty);
477
        }
478
479 78
        $this->options[$this->valueProperty] = $value;
480 76
481 76
        return $this;
482
    }
483 78
484
    /**
485 78
     * Set the template property on the object
486
     */
487
    private function setTemplate()
488
    {
489
        $this->template = $this->formHelper->getConfig($this->getTemplate(), $this->getTemplate());
490
    }
491 84
492
    /**
493 84
     * Add error class to wrapper if validation errors exist
494 84
     */
495
    protected function addErrorClass()
496
    {
497
        $errors = $this->parent->getRequest()->session()->get('errors');
498
499 84
        if ($errors && $errors->has($this->getNameKey())) {
500
            $errorClass = $this->formHelper->getConfig('defaults.wrapper_error_class');
501 84
            $wrapperClass = $this->getOption('wrapper.class');
502
503 84
            if ($this->getOption('wrapper') && !str_contains($wrapperClass, $errorClass)) {
504
                $wrapperClass .= ' ' . $errorClass;
505
                $this->setOption('wrapper.class', $wrapperClass);
506
            }
507
        }
508
    }
509
510
511
    /**
512 84
     * Merge all defaults with field specific defaults and set template if passed
513
     *
514
     * @param array $options
515
     */
516
    protected function setDefaultOptions(array $options = [])
517
    {
518
        $this->options = $this->formHelper->mergeOptions($this->allDefaults(), $this->getDefaults());
519
        $this->options = $this->prepareOptions($options);
520 84
521
        $defaults = $this->setDefaultClasses($options);
522 84
        $this->options = $this->formHelper->mergeOptions($this->options, $defaults);
523 84
524
        $this->setupLabel();
525 84
    }
526 84
527
    /**
528 84
     * Creates default wrapper classes for the form element.
529 84
     *
530
     * @param array $options
531
     * @return array
532
     */
533
    protected function setDefaultClasses(array $options = [])
534
    {
535
        $wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.wrapper_class', '');
536
        $label_class = $this->formHelper->getConfig('defaults.' . $this->type . '.label_class', '');
537 84
        $field_class = $this->formHelper->getConfig('defaults.' . $this->type . '.field_class', '');
538
539 84
        $defaults = [];
540 84
        if ($wrapper_class && !array_get($options, 'wrapper.class')) {
541 84
            $defaults['wrapper']['class'] = $wrapper_class;
542
        }
543 84
        if ($label_class && !array_get($options, 'label_attr.class')) {
544 84
            $defaults['label_attr']['class'] = $label_class;
545
        }
546
        if ($field_class && !array_get($options, 'attr.class')) {
547 84
            $defaults['attr']['class'] = $field_class;
548
        }
549
        return $defaults;
550 84
    }
551 1
552 1
    protected function setupLabel()
553 84
    {
554
        if ($this->getOption('label') !== null) {
555
            return;
556 84
        }
557
558 84
        if ($langName = $this->parent->getLanguageName()) {
559 21
            $label = sprintf('%s.%s', $langName, $this->getRealName());
560
        } else {
561
            $label = $this->getRealName();
562 82
        }
563 4
564 4
        $this->setOption('label', $this->formHelper->formatLabel($label));
565 79
    }
566
567
    /**
568 82
     * Check if fields needs label
569 82
     *
570
     * @return bool
571
     */
572
    protected function needsLabel()
573
    {
574
        // If field is <select> and child of choice, we don't need label for it
575
        $isChildSelect = $this->type == 'select' && $this->getOption('is_child') === true;
576 31
577
        if ($this->type == 'hidden' || $isChildSelect) {
578
            return false;
579 31
        }
580
581 31
        return true;
582 8
    }
583
584
    /**
585 27
     * Disable field
586
     *
587
     * @return $this
588
     */
589
    public function disable()
590
    {
591
        $this->setOption('attr.disabled', 'disabled');
592
593 1
        return $this;
594
    }
595 1
596
    /**
597 1
     * Enable field
598
     *
599
     * @return $this
600
     */
601
    public function enable()
602
    {
603
        array_forget($this->options, 'attr.disabled');
604
605 1
        return $this;
606
    }
607 1
608
    /**
609 1
     * Get validation rules for a field if any with label for attributes
610
     *
611
     * @return array|null
612
     */
613
    public function getValidationRules()
614
    {
615
        $rules = $this->getOption('rules', []);
616
        $name = $this->getNameKey();
617 8
        $messages = $this->getOption('error_messages', []);
618
        $formName = $this->formHelper->transformToDotSyntax($this->parent->getName());
619 8
620 8
        if ($messages && $formName) {
621 8
            $newMessages = [];
622 8
            foreach ($messages as $messageKey => $message) {
623
                $messageKey = sprintf('%s.%s', $formName, $messageKey);
624 8
                $newMessages[$messageKey] = $message;
625 1
            }
626 1
            $messages = $newMessages;
627 1
        }
628 1
629 1
        if (!$rules) {
630 1
            return [];
631 1
        }
632
633 8
        return [
634 2
            'rules' => [$name => $rules],
635
            'attributes' => [$name => $this->getOption('label')],
636
            'error_messages' => $messages
637
        ];
638 7
    }
639 7
640
    /**
641 7
     * Get this field's attributes, probably just one.
642
     *
643
     * @return array
644
     */
645
    public function getAllAttributes()
646
    {
647
        return [$this->getNameKey()];
648
    }
649 3
650
    /**
651 3
     * Get value property
652
     *
653
     * @param mixed|null $default
654
     * @return mixed
655
     */
656
    public function getValue($default = null)
657
    {
658
        return $this->getOption($this->valueProperty, $default);
659
    }
660 34
661
    /**
662 34
     * Get default value property
663
     *
664
     * @param mixed|null $default
665
     * @return mixed
666
     */
667
    public function getDefaultValue($default = null)
668
    {
669
        return $this->getOption($this->defaultValueProperty, $default);
670
    }
671 31
672
    /**
673 31
     * Check if provided value is valid for this type
674
     *
675
     * @return bool
676
     */
677
    protected function isValidValue($value)
678
    {
679
        return $value !== null;
680
    }
681
}
682