Completed
Push — master ( 2926a1...76e2b9 )
by Costin
02:09
created

FormElement   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 29
eloc 68
c 8
b 3
f 1
dl 0
loc 201
rs 10

19 Methods

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