Completed
Push — master ( 4ac224...b6916c )
by Costin
02:52
created

FormElement::getErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SoareCostin\BladeFormComponents;
4
5
use SoareCostin\BladeFormComponents\Traits\GluesAttributes;
6
7
abstract class FormElement
8
{
9
    use GluesAttributes;
10
11
    /** @var \Illuminate\Support\Collection */
12
    protected $params;
13
14
    /** @var Form */
15
    protected $form;
16
17
    /** @var object */
18
    public $model = null;
19
20
    /** @var string */
21
    public $id;
22
23
    /** @var string */
24
    public $name = '';
25
26
    /** @var mixed */
27
    public $value;
28
29
    /** @var string */
30
    public $label = '';
31
32
    /** @var array */
33
    public $class = [];
34
35
    /** @var array */
36
    public $labelClass = [];
37
38
    /** @var bool */
39
    public $required = false;
40
41
    /** @var bool */
42
    public $disabled = false;
43
44
    /** @var bool */
45
    public $readonly = false;
46
47
    /** @var string */
48
    public $autocomplete = 'off';
49
50
    /** @var string */
51
    public $desc;
52
53
    /** @var string */
54
    public $help;
55
56
    /** @var array */
57
    public $addons;
58
59
    public function __construct(array $params)
60
    {
61
        $this->params = collect($params);
62
63
        $this->setForm();
64
        $this->setModel();
65
        $this->setCommonAttributes();
66
        $this->setSpecificAttributes();
67
        $this->setStyles();
68
    }
69
70
    public function getId()
71
    {
72
        return md5(serialize($this));
73
    }
74
75
    public function getTheme()
76
    {
77
        return optional($this->form)->theme ?? config('blade-form-components.theme');
78
    }
79
80
    public function getErrors()
81
    {
82
        return $this->form->errors->getBag('default');
83
    }
84
85
    protected function setForm()
86
    {
87
        $this->form = app(Form::class);
88
    }
89
90
    protected function setModel()
91
    {
92
        $this->model = $this->params->get('model', $this->form->model);
93
    }
94
95
    protected function setCommonAttributes()
96
    {
97
        $this->setName();
98
        $this->setLabel();
99
        $this->setValue();
100
        $this->setClass();
101
        $this->setRequired();
102
        $this->setDisabled();
103
        $this->setReadonly();
104
        $this->setAutocomplete();
105
        $this->setDesc();
106
        $this->setHelp();
107
        $this->setAddons();
108
    }
109
110
    protected function setSpecificAttributes()
111
    {
112
        // ...
113
    }
114
115
    protected function setStyles()
116
    {
117
        // ...
118
    }
119
120
    protected function setName()
121
    {
122
        $this->name = $this->id = $this->params->get('name');
123
    }
124
125
    protected function setLabel()
126
    {
127
        // Check if we receive a label that is false, so we don't display it
128
        if ($this->params->get('label') === false) {
129
            $this->label = '';
130
131
            return;
132
        }
133
134
        // Fallback: construct the label from the name
135
        $fallbackLabel = ! empty($this->name) ? ucwords(str_replace('_', ' ', $this->name)) : '';
136
137
        // Check if we receive an explicit label
138
        $this->label = $this->params->get('label', $fallbackLabel);
139
    }
140
141
    protected function setRequired()
142
    {
143
        $this->required = $this->params->get('required', false);
144
    }
145
146
    protected function setDisabled()
147
    {
148
        $this->disabled = $this->params->get('disabled', false);
149
    }
150
151
    protected function setReadonly()
152
    {
153
        $this->readonly = $this->params->get('readonly', false);
154
    }
155
156
    protected function setValue()
157
    {
158
        if (empty($this->name)) {
159
            return;
160
        }
161
162
        $this->value = $this->params->get('value');
163
164
        if (! is_null($this->value)) {
165
            $computedValue = $this->value;
166
        } elseif (! is_null($this->model) && isset($this->model->{str_replace('[]', '', $this->name)})) {
167
            $computedValue = $this->model->{str_replace('[]', '', $this->name)};
168
        }
169
170
        $this->value = old($this->name, $computedValue ?? '');
171
    }
172
173
    protected function setAutocomplete()
174
    {
175
        // Set default autocomplete option (on/off) from cofing file
176
        $this->autocomplete = $this->params->get('autocomplete', config('blade-form-components.autocomplete'));
177
    }
178
179
    protected function setDesc()
180
    {
181
        $this->desc = $this->params->get('desc');
182
    }
183
184
    protected function setHelp()
185
    {
186
        $this->help = $this->params->get('help');
187
    }
188
189
    protected function setAddons()
190
    {
191
        $this->addons = $this->params->get('addons');
192
    }
193
194
    protected function setClass()
195
    {
196
        // Attach the error class if an error is displayed against this field
197
        if (! empty($this->name) && $this->form->errors->has($this->name)) {
198
            $this->class[] = config('blade-form-components.themes.'.$this->getTheme().'.fields.is-error');
199
        }
200
201
        // Attach other user-defined classes
202
        if ($this->params->has('class')) {
203
            $this->class[] = $this->params->get('class');
204
        }
205
    }
206
207
    protected function customAttributes()
208
    {
209
        // Additional, custom attributes set by the user (eg: data, v-model)
210
        $customAttributes = $this->params->get('attributes', []);
211
212
        return isset($customAttributes['input'])
213
                ? $customAttributes['input']
214
                : [];
215
    }
216
}
217