Completed
Push — master ( a0e7e0...d1cff9 )
by Costin
01:48 queued 12s
created

FormElement::setRequired()   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
use SoareCostin\BladeFormComponents\Traits\GluesAttributes;
7
8
abstract class FormElement
9
{
10
    use GluesAttributes;
11
12
    /** @var array */
13
    protected $params;
14
15
    /** @var Form */
16
    protected $form;
17
18
    /** @var object */
19
    public $model = null;
20
21
    /** @var string */
22
    public $id;
23
24
    /** @var string */
25
    public $name;
26
27
    /** @var mixed */
28
    public $value;
29
30
    /** @var string */
31
    public $label;
32
33
    /** @var array */
34
    public $class = [];
35
36
    /** @var array */
37
    public $labelClass = [];
38
39
    /** @var bool */
40
    public $required = false;
41
42
    /** @var bool */
43
    public $disabled = false;
44
45
    /** @var bool */
46
    public $readonly = false;
47
48
    /** @var bool */
49
    public $autocomplete;
50
51
    /** @var array */
52
    public $attributesList = [];
53
54
    /** @var string */
55
    public $desc;
56
57
    /** @var string */
58
    public $help;
59
60
    public function __construct(array $params)
61
    {
62
        $this->params = $params;
63
64
        $this->setForm();
65
        $this->setModel();
66
        $this->setCommonAttributes();
67
        $this->setSpecificAttributes();
68
    }
69
70
    protected function setForm()
71
    {
72
        $this->form = app(Form::class);
73
    }
74
75
    protected function setModel()
76
    {
77
        if (isset($this->params['model']) && ! empty($this->params['model'])) {
78
            $this->model = $this->params['model'];
79
        }
80
81
        // Check to see if the model was added on the form opening tag
82
        if (is_null($this->model) && ! is_null($this->form->model)) {
83
            $this->model = $this->form->model;
84
        }
85
    }
86
87
    protected function setCommonAttributes()
88
    {
89
        $this->setName();
90
        $this->setLabel();
91
        $this->setValue();
92
        $this->setClass();
93
        $this->setRequired();
94
        $this->setDisabled();
95
        $this->setReadonly();
96
        $this->setAutocomplete();
97
        $this->setDesc();
98
        $this->setHelp();
99
    }
100
101
    protected function setSpecificAttributes()
102
    {
103
        // ...
104
    }
105
106
    protected function setName()
107
    {
108 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...
109
            $this->name = $this->id = $this->params['name'];
110
        }
111
    }
112
113
    protected function setLabel()
114
    {
115
        // First, check if we receive an explicit label
116 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...
117
            $this->label = $this->params['label'];
118
119
            return;
120
        }
121
122
        // Check if we receive a label that is false, so we don't display it
123 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...
124
            $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...
125
126
            return;
127
        }
128
129
        // Fallback: construct the label from the name
130
        if (isset($this->name)) {
131
            $this->label = ucwords(str_replace('_', ' ', $this->name));
132
        }
133
    }
134
135
    protected function setRequired()
136
    {
137 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...
138
            $this->required = true;
139
        }
140
    }
141
142
    protected function setDisabled()
143
    {
144 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...
145
            $this->disabled = true;
146
        }
147
    }
148
149
    protected function setReadonly()
150
    {
151 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...
152
            $this->readonly = true;
153
        }
154
    }
155
156
    protected function setValue()
157
    {
158
        if (is_null($this->name)) {
159
            return;
160
        }
161
162
        if (! is_null($this->value)) {
163
            $computedValue = $this->value;
164
        } elseif (! is_null($this->model) && isset($this->model->{str_replace('[]', '', $this->name)})) {
165
            $computedValue = $this->model->{str_replace('[]', '', $this->name)};
166
        }
167
168
        $this->value = old($this->name, $computedValue ?? '');
169
    }
170
171 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...
172
    {
173
        // Set default autocomplete option (true/false) from cofing file
174
        $this->autocomplete = config('blade-form-components.autocomplete');
175
176
        if (isset($this->params['autocomplete'])) {
177
            $this->autocomplete = $this->params['autocomplete'];
178
        }
179
    }
180
181
    protected function setDesc()
182
    {
183
        if (isset($this->params['desc'])) {
184
            $this->desc = $this->params['desc'];
185
        }
186
    }
187
188
    protected function setHelp()
189
    {
190
        if (isset($this->params['help'])) {
191
            $this->help = $this->params['help'];
192
        }
193
    }
194
195 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...
196
    {
197
        // Default class applied to all form elements (Eg 'form-control' for Bootstrap)
198
        $this->class[] = config('blade-form-components.styles.field.input');
199
200
        // Attach the error class if an error is displayed against this field
201
        $errors = session()->get('errors', app(ViewErrorBag::class));
202
        if ($errors->has($this->name)) {
203
            $this->class[] = config('blade-form-components.styles.field.error');
204
        }
205
206
        // Attach other user-defined classes
207
    }
208
}
209