Passed
Pull Request — master (#1151)
by Diego
03:33
created

InputGroupComponent::makeInputGroupClass()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 8
nop 0
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\View\Components\Form;
4
5
use Illuminate\View\Component;
6
use JeroenNoten\LaravelAdminLte\Helpers\UtilsHelper;
7
8
class InputGroupComponent extends Component
9
{
10
    /**
11
     * Holds an instance of the Laravel errors bag. This will be mainly used to
12
     * detect when the input group has associated errors.
13
     *
14
     * @var \Illuminate\Support\MessageBag;
15
     */
16
    protected $errorsBag;
17
18
    /**
19
     * The id attribute for the underlying input group item. The input group
20
     * item may be an "input", a "select", a "textarea", etc.
21
     *
22
     * @var string
23
     */
24
    public $id;
25
26
    /**
27
     * The name attribute for the underlying input group item. This value will
28
     * be also used as the default id attribute when no other is provided. The
29
     * input group item may be an "input", a "select", a "textarea", etc.
30
     *
31
     * @var string
32
     */
33
    public $name;
34
35
    /**
36
     * The label of the input group.
37
     *
38
     * @var string
39
     */
40
    public $label;
41
42
    /**
43
     * The input group size (you can specify 'sm' or 'lg').
44
     *
45
     * @var string
46
     */
47
    public $size;
48
49
    /**
50
     * Additional classes for the "input-group" element. This provides a way to
51
     * customize the input group container style.
52
     *
53
     * @var string
54
     */
55
    public $igroupClass;
56
57
    /**
58
     * Extra classes for the label container. This provides a way to customize
59
     * the label style.
60
     *
61
     * @var string
62
     */
63
    public $labelClass;
64
65
    /**
66
     * Extra classes for the "form-group" element. This provides a way to
67
     * customize the main container style.
68
     *
69
     * @var string
70
     */
71
    public $fgroupClass;
72
73
    /**
74
     * Indicates if the invalid feedback is disabled for the input group.
75
     *
76
     * @var bool
77
     */
78
    public $disableFeedback;
79
80
    /**
81
     * The lookup key to use when searching for validation errors. The lookup
82
     * key is automatically generated from the name property. This provides a
83
     * way to overwrite that value.
84
     *
85
     * @var string
86
     */
87
    public $errorKey;
88
89
    /**
90
     * Create a new component instance.
91
     *
92
     * @return void
93
     */
94 22
    public function __construct(
95
        $name, $id = null, $label = null, $igroupSize = null, $labelClass = null,
96
        $fgroupClass = null, $igroupClass = null, $disableFeedback = null,
97
        $errorKey = null
98
    ) {
99 22
        $this->id = $id ?? $name;
100 22
        $this->name = $name;
101 22
        $this->label = UtilsHelper::applyHtmlEntityDecoder($label);
102 22
        $this->size = $igroupSize;
103 22
        $this->fgroupClass = $fgroupClass;
104 22
        $this->labelClass = $labelClass;
105 22
        $this->igroupClass = $igroupClass;
106 22
        $this->disableFeedback = $disableFeedback;
107
108
        // Setup the lookup key for validation errors.
109
110 22
        $this->errorKey = $errorKey ?? $this->makeErrorKey();
111
112
        // Initialize the internal errors bag holder variable.
113
114 22
        $this->errorsBag = null;
115
    }
116
117
    /**
118
     * Make the class attribute for the "form-group" element.
119
     *
120
     * @return string
121
     */
122 2
    public function makeFormGroupClass()
123
    {
124 2
        $classes = ['form-group'];
125
126 2
        if (isset($this->fgroupClass)) {
127 2
            $classes[] = $this->fgroupClass;
128
        }
129
130 2
        return implode(' ', $classes);
131
    }
132
133
    /**
134
     * Make the class attribute for the "input-group" element.
135
     *
136
     * @return string
137
     */
138 2
    public function makeInputGroupClass()
139
    {
140 2
        $classes = ['input-group'];
141
142 2
        if (isset($this->size) && in_array($this->size, ['sm', 'lg'])) {
143 2
            $classes[] = "input-group-{$this->size}";
144
        }
145
146 2
        if ($this->isInvalid()) {
147 2
            $classes[] = 'adminlte-invalid-igroup';
148
        }
149
150 2
        if (isset($this->igroupClass)) {
151 2
            $classes[] = $this->igroupClass;
152
        }
153
154 2
        return implode(' ', $classes);
155
    }
156
157
    /**
158
     * Make the class attribute for the input group item.
159
     *
160
     * @return string
161
     */
162 7
    public function makeItemClass()
163
    {
164 7
        $classes = ['form-control'];
165
166 7
        if ($this->isInvalid()) {
167 7
            $classes[] = 'is-invalid';
168
        }
169
170 7
        return implode(' ', $classes);
171
    }
172
173
    /**
174
     * Check if there are validation errors in the session related to the
175
     * error key.
176
     *
177
     * @return bool
178
     */
179 12
    public function isInvalid()
180
    {
181
        // Get the errors bag from session. The errors bag will be an instance
182
        // of the Illuminate\Support\MessageBag class. First we will check if
183
        // the errors bag is available within this instance, otherwise we will
184
        // try to get it from the session.
185
186 12
        $errors = $this->errorsBag ?? session()->get('errors');
187
188
        // Check if the invalid feedback is enabled and there exists an error
189
        // related to the configured error key.
190 12
191
        return ! isset($this->disableFeedback)
192
            && ! empty($errors)
193
            && $errors->has($this->errorKey);
194
    }
195
196
    /**
197
     * Setup the errors bag internally.
198
     *
199 1
     * @param  \Illuminate\Support\MessageBag  $errorsBag
200
     * @return void
201 1
     */
202
    public function setErrorsBag($errorsBag)
203
    {
204
        $this->errorsBag = $errorsBag;
205
    }
206
207
    /**
208
     * Make the error key that will be used to search for validation errors.
209
     * The error key is generated from the 'name' property.
210
     * Examples:
211
     * $name = 'files[]'         => $errorKey = 'files'.
212
     * $name = 'person[2][name]' => $errorKey = 'person.2.name'.
213 21
     *
214
     * @return string
215 21
     */
216
    protected function makeErrorKey()
217 21
    {
218
        $errKey = preg_replace('@\[\]$@', '', $this->name);
219
220
        return preg_replace('@\[([^]]+)\]@', '.$1', $errKey);
221
    }
222
223
    /**
224
     * Get the view / contents that represent the component.
225 1
     *
226
     * @return \Illuminate\View\View|string
227 1
     */
228
    public function render()
229
    {
230
        return view('adminlte::components.form.input-group-component');
231
    }
232
}
233