Passed
Push — master ( 304c1e...d7d2a3 )
by Costin
02:59
created

FormElement::setAddons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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 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
        if (! is_null($this->value)) {
143
            $computedValue = $this->value;
144
        } elseif (! is_null($this->model) && isset($this->model->{str_replace('[]', '', $this->name)})) {
145
            $computedValue = $this->model->{str_replace('[]', '', $this->name)};
146
        }
147
148
        $this->value = old($this->name, $computedValue ?? '');
149
    }
150
151
    protected function setAutocomplete()
152
    {
153
        // Set default autocomplete option (on/off) from cofing file
154
        $this->autocomplete = $this->params->get('autocomplete', config('blade-form-components.autocomplete'));
155
    }
156
157
    protected function setDesc()
158
    {
159
        $this->desc = $this->params->get('desc');
160
    }
161
162
    protected function setHelp()
163
    {
164
        $this->help = $this->params->get('help');
165
    }
166
167
    protected function setAddons()
168
    {
169
        $this->addons = $this->params->get('addons');
170
    }
171
172
    protected function setClass()
173
    {
174
        $this->setDefaultClass();
175
176
        // Attach the error class if an error is displayed against this field
177
        $errors = session()->get('errors', app(ViewErrorBag::class));
178
        if (! empty($this->name) && $errors->has($this->name)) {
179
            $this->class[] = config('blade-form-components.styles.field.error');
180
        }
181
182
        // Attach other user-defined classes
183
        if ($this->params->has('class')) {
184
            $this->class[] = $this->params->get('class');
185
        }
186
    }
187
188
    protected function setDefaultClass()
189
    {
190
        // Default class applied to all form elements (Eg 'form-control' for Bootstrap)
191
        $this->class[] = config('blade-form-components.styles.field.input');
192
    }
193
194
    protected function customAttributes()
195
    {
196
        // Additional, custom attributes set by the user (eg: data, v-model)
197
        $customAttributes = $this->params->get('attributes', []);
198
199
        return isset($customAttributes['input'])
200
                ? $customAttributes['input']
201
                : [];
202
    }
203
}
204