Passed
Push — master ( 567445...4ac224 )
by Costin
02:45
created

FormElement::setStyles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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