Passed
Push — master ( 8ffcfc...5c51c3 )
by Diego
02:44
created

InputGroupComponent::makeFormGroupClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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