Completed
Push — master ( 4e091b...a0e7e0 )
by Costin
06:08 queued 03:39
created

FormElement::setHelp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace SoareCostin\BladeFormComponents;
4
5
use Illuminate\Support\ViewErrorBag;
6
7
abstract class FormElement
8
{
9
    /** @var array */
10
    protected $params;
11
12
    /** @var Form */
13
    protected $form;
14
15
    /** @var object */
16
    public $model = null;
17
18
    /** @var string */
19
    public $id;
20
21
    /** @var string */
22
    public $name;
23
24
    /** @var mixed */
25
    public $value;
26
27
    /** @var string */
28
    public $label;
29
30
    /** @var array */
31
    public $class = [];
32
33
    /** @var array */
34
    public $labelClass = [];
35
36
    /** @var bool */
37
    public $required = false;
38
39
    /** @var bool */
40
    public $disabled = false;
41
42
    /** @var bool */
43
    public $readonly = false;
44
45
    /** @var bool */
46
    public $autocomplete;
47
48
    /** @var array */
49
    public $attributesList = [];
50
51
    /** @var string */
52
    public $desc;
53
54
    /** @var string */
55
    public $help;
56
57
    public function __construct(array $params)
58
    {
59
        $this->params = $params;
60
61
        $this->setForm();
62
        $this->setModel();
63
        $this->setCommonAttributes();
64
        $this->setSpecificAttributes();
65
    }
66
67
    protected function setForm()
68
    {
69
        $this->form = app(Form::class);
70
    }
71
72
    protected function setModel()
73
    {
74
        if (isset($this->params['model']) && ! empty($this->params['model'])) {
75
            $this->model = $this->params['model'];
76
        }
77
78
        // Check to see if the model was added on the form opening tag
79
        if (is_null($this->model) && ! is_null($this->form->model)) {
80
            $this->model = $this->form->model;
81
        }
82
    }
83
84
    protected function setCommonAttributes()
85
    {
86
        $this->setName();
87
        $this->setLabel();
88
        $this->setValue();
89
        $this->setClass();
90
        $this->setRequired();
91
        $this->setDisabled();
92
        $this->setReadonly();
93
        $this->setAutocomplete();
94
        $this->setDesc();
95
        $this->setHelp();
96
    }
97
98
    protected function setSpecificAttributes()
99
    {
100
        // ...
101
    }
102
103
    protected function setName()
104
    {
105 View Code Duplication
        if (isset($this->params['name']) && ! empty($this->params['name'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            $this->name = $this->id = $this->params['name'];
107
        }
108
    }
109
110
    protected function setLabel()
111
    {
112
        // First, check if we receive an explicit label
113 View Code Duplication
        if (isset($this->params['label']) && ! empty($this->params['label'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
            $this->label = $this->params['label'];
115
116
            return;
117
        }
118
119
        // Check if we receive a label that is false, so we don't display it
120 View Code Duplication
        if (isset($this->params['label']) && $this->params['label'] === false) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
            $this->label = false;
0 ignored issues
show
Documentation Bug introduced by
The property $label was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
122
123
            return;
124
        }
125
126
        // Fallback: construct the label from the name
127
        if (isset($this->name)) {
128
            $this->label = ucwords(str_replace('_', ' ', $this->name));
129
        }
130
    }
131
132
    protected function setRequired()
133
    {
134 View Code Duplication
        if (isset($this->params['required']) && $this->params['required'] == true) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
            $this->required = true;
136
        }
137
    }
138
139
    protected function setDisabled()
140
    {
141 View Code Duplication
        if (isset($this->params['disabled']) && $this->params['disabled'] == true) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
            $this->disabled = true;
143
        }
144
    }
145
146
    protected function setReadonly()
147
    {
148 View Code Duplication
        if (isset($this->params['readonly']) && $this->params['readonly'] == true) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
            $this->readonly = true;
150
        }
151
    }
152
153
    protected function setValue()
154
    {
155
        if (is_null($this->name)) {
156
            return;
157
        }
158
159
        if (! is_null($this->value)) {
160
            $computedValue = $this->value;
161
        } elseif (! is_null($this->model) && isset($this->model->{str_replace('[]', '', $this->name)})) {
162
            $computedValue = $this->model->{str_replace('[]', '', $this->name)};
163
        }
164
165
        $this->value = old($this->name, $computedValue ?? '');
166
    }
167
168 View Code Duplication
    protected function setAutocomplete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        // Set default autocomplete option (true/false) from cofing file
171
        $this->autocomplete = config('blade-form-components.autocomplete');
172
173
        if (isset($this->params['autocomplete'])) {
174
            $this->autocomplete = $this->params['autocomplete'];
175
        }
176
    }
177
178
    protected function setDesc()
179
    {
180
        if (isset($this->params['desc'])) {
181
            $this->desc = $this->params['desc'];
182
        }
183
    }
184
185
    protected function setHelp()
186
    {
187
        if (isset($this->params['help'])) {
188
            $this->help = $this->params['help'];
189
        }
190
    }
191
192 View Code Duplication
    protected function setClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
    {
194
        // Default class applied to all form elements (Eg 'form-control' for Bootstrap)
195
        $this->class[] = config('blade-form-components.styles.field.input');
196
197
        // Attach the error class if an error is displayed against this field
198
        $errors = session()->get('errors', app(ViewErrorBag::class));
199
        if ($errors->has($this->name)) {
200
            $this->class[] = config('blade-form-components.styles.field.error');
201
        }
202
203
        // Attach other user-defined classes
204
    }
205
206
    public function glueAttributes()
207
    {
208
        $pairs = [];
209
        foreach ($this->attributesList as $attr) {
210
            if (isset($this->{$attr})) {
211
212
                // Edge cases
213
                if ($attr == 'autocomplete') {
214
                    $pairs[] = sprintf('%s="%s"', $attr, $this->{$attr} ? 'on' : 'off');
215
                    continue;
216
                }
217
218
                if ($attr == 'class' && is_array($this->{$attr})) {
219
                    $pairs[] = sprintf('%s="%s"', $attr, implode(' ', $this->{$attr}));
220
                    continue;
221
                }
222
223
                // Required, disabled, readonly: true/false
224
                if (is_bool($this->{$attr})) {
225
                    if ($this->{$attr} == true) {
226
                        $pairs[] = $attr;
227
                    }
228
                    continue;
229
                }
230
231
                $pairs[] = sprintf('%s="%s"', $attr, $this->{$attr});
232
            }
233
        }
234
235
        return implode(' ', $pairs);
236
    }
237
}
238