Completed
Push — master ( 839e9e...4e091b )
by Costin
03:53
created

FormElement::setDisabled()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 3
Ratio 50 %

Importance

Changes 0
Metric Value
dl 3
loc 6
rs 10
c 0
b 0
f 0
cc 3
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
            return;
116
        }
117
118
        // Check if we receive a label that is false, so we don't display it
119 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...
120
            $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...
121
            return;
122
        }
123
124
        // Fallback: construct the label from the name
125
        if (isset($this->name)) {
126
            $this->label = ucwords(str_replace("_", " ", $this->name));
127
        }
128
    }
129
130
    protected function setRequired()
131
    {
132 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...
133
            $this->required = true;
134
        }
135
    }
136
137
    protected function setDisabled()
138
    {
139 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...
140
            $this->disabled = true;
141
        }
142
    }
143
144
    protected function setReadonly()
145
    {
146 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...
147
            $this->readonly = true;
148
        }
149
    }
150
151
    protected function setValue()
152
    {
153
        if (is_null($this->name)) {
154
            return null;
155
        }
156
157
        if (!is_null($this->value)) {
158
            $computedValue = $this->value;
159
        } else if (!is_null($this->model) && isset($this->model->{str_replace('[]','',$this->name)})) {
160
            $computedValue = $this->model->{str_replace('[]','',$this->name)};
161
        }
162
        
163
        $this->value = old($this->name, $computedValue ?? '');
164
    }
165
166 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...
167
    {
168
        // Set default autocomplete option (true/false) from cofing file
169
        $this->autocomplete = config('blade-form-components.autocomplete');
170
171
        if (isset($this->params['autocomplete'])) {
172
            $this->autocomplete = $this->params['autocomplete'];
173
        }
174
    }
175
    
176
    protected function setDesc()
177
    {
178
        if (isset($this->params['desc'])) {
179
            $this->desc = $this->params['desc'];
180
        }
181
    }
182
    
183
    protected function setHelp()
184
    {
185
        if (isset($this->params['help'])) {
186
            $this->help = $this->params['help'];
187
        }
188
    }
189
190 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...
191
    {
192
        // Default class applied to all form elements (Eg 'form-control' for Bootstrap)
193
        $this->class[] = config('blade-form-components.styles.field.input');
194
        
195
        // Attach the error class if an error is displayed against this field
196
        $errors = session()->get('errors', app(ViewErrorBag::class));
197
        if ($errors->has($this->name)) {
198
            $this->class[] = config('blade-form-components.styles.field.error');
199
        }
200
201
        // Attach other user-defined classes
202
203
    }
204
205
    public function glueAttributes()
206
    {
207
        $pairs = [];
208
        foreach ($this->attributesList as $attr) {
209
            if (isset($this->{$attr})) {
210
211
                // Edge cases
212
                if ($attr == 'autocomplete') {
213
                    $pairs[] = sprintf('%s="%s"', $attr, $this->{$attr} ? 'on' : 'off');
214
                    continue;
215
                }
216
217
                if ($attr == 'class' && is_array($this->{$attr})) {
218
                    $pairs[] = sprintf('%s="%s"', $attr, implode(' ', $this->{$attr}));
219
                    continue;
220
                }
221
222
                // Required, disabled, readonly: true/false
223
                if (is_bool($this->{$attr})) {
224
                    if ($this->{$attr} == true) {
225
                        $pairs[] = $attr;
226
                    }
227
                    continue;
228
                }
229
                
230
                $pairs[] = sprintf('%s="%s"', $attr, $this->{$attr});
231
            }
232
        }
233
        return implode(" ", $pairs);
234
    }
235
}