Passed
Push — master ( a61831...cdc4b2 )
by Costin
03:06
created

FormElement   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Importance

Changes 9
Bugs 4 Features 1
Metric Value
wmc 30
eloc 68
c 9
b 4
f 1
dl 0
loc 205
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setLabel() 0 14 3
A setDesc() 0 3 1
A setDefaultClass() 0 2 1
A setCommonAttributes() 0 13 1
A setModel() 0 3 1
A setName() 0 3 1
A setSpecificAttributes() 0 2 1
A setReadonly() 0 3 1
A setRequired() 0 3 1
A setValue() 0 15 5
A setClass() 0 13 4
A setForm() 0 3 1
A setHelp() 0 3 1
A setDisabled() 0 3 1
A setAutocomplete() 0 4 1
A customAttributes() 0 8 2
A getId() 0 3 1
A getTheme() 0 3 1
A setAddons() 0 3 1
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 \Illuminate\Support\Collection */
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 string */
49
    public $autocomplete = 'off';
50
51
    /** @var string */
52
    public $desc;
53
54
    /** @var string */
55
    public $help;
56
57
    /** @var array */
58
    public $addons;
59
60
    public function __construct(array $params)
61
    {
62
        $this->params = collect($params);
63
64
        $this->setForm();
65
        $this->setModel();
66
        $this->setCommonAttributes();
67
        $this->setSpecificAttributes();
68
    }
69
70
    public function getId()
71
    {
72
        return md5(serialize($this));
73
    }
74
75
    public function getTheme()
76
    {
77
        return $this->form->theme;
78
    }
79
    
80
    protected function setForm()
81
    {
82
        $this->form = app(Form::class);
83
    }
84
85
    protected function setModel()
86
    {
87
        $this->model = $this->params->get('model', $this->form->model);
88
    }
89
90
    protected function setCommonAttributes()
91
    {
92
        $this->setName();
93
        $this->setLabel();
94
        $this->setValue();
95
        $this->setClass();
96
        $this->setRequired();
97
        $this->setDisabled();
98
        $this->setReadonly();
99
        $this->setAutocomplete();
100
        $this->setDesc();
101
        $this->setHelp();
102
        $this->setAddons();
103
    }
104
105
    protected function setSpecificAttributes()
106
    {
107
        // ...
108
    }
109
110
    protected function setDefaultClass()
111
    {
112
        // ...
113
    }
114
115
    protected function setName()
116
    {
117
        $this->name = $this->id = $this->params->get('name');
118
    }
119
120
    protected function setLabel()
121
    {
122
        // Check if we receive a label that is false, so we don't display it
123
        if ($this->params->get('label') === false) {
124
            $this->label = '';
125
126
            return;
127
        }
128
129
        // Fallback: construct the label from the name
130
        $fallbackLabel = ! empty($this->name) ? ucwords(str_replace('_', ' ', $this->name)) : '';
131
132
        // Check if we receive an explicit label
133
        $this->label = $this->params->get('label', $fallbackLabel);
134
    }
135
136
    protected function setRequired()
137
    {
138
        $this->required = $this->params->get('required', false);
139
    }
140
141
    protected function setDisabled()
142
    {
143
        $this->disabled = $this->params->get('disabled', false);
144
    }
145
146
    protected function setReadonly()
147
    {
148
        $this->readonly = $this->params->get('readonly', false);
149
    }
150
151
    protected function setValue()
152
    {
153
        if (empty($this->name)) {
154
            return;
155
        }
156
157
        $this->value = $this->params->get('value');
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
    protected function setAutocomplete()
169
    {
170
        // Set default autocomplete option (on/off) from cofing file
171
        $this->autocomplete = $this->params->get('autocomplete', config('blade-form-components.autocomplete'));
172
    }
173
174
    protected function setDesc()
175
    {
176
        $this->desc = $this->params->get('desc');
177
    }
178
179
    protected function setHelp()
180
    {
181
        $this->help = $this->params->get('help');
182
    }
183
184
    protected function setAddons()
185
    {
186
        $this->addons = $this->params->get('addons');
187
    }
188
189
    protected function setClass()
190
    {
191
        $this->setDefaultClass();
192
193
        // Attach the error class if an error is displayed against this field
194
        $errors = session()->get('errors', app(ViewErrorBag::class));
195
        if (! empty($this->name) && $errors->has($this->name)) {
196
            $this->class[] = config('blade-form-components.themes.'.$this->getTheme().'.fields.error');
197
        }
198
199
        // Attach other user-defined classes
200
        if ($this->params->has('class')) {
201
            $this->class[] = $this->params->get('class');
202
        }
203
    }
204
205
    protected function customAttributes()
206
    {
207
        // Additional, custom attributes set by the user (eg: data, v-model)
208
        $customAttributes = $this->params->get('attributes', []);
209
210
        return isset($customAttributes['input'])
211
                ? $customAttributes['input']
212
                : [];
213
    }
214
}
215