Passed
Pull Request — master (#1060)
by Florian
06:01 queued 03:20
created

InputGroupComponent::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
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 21
     */
86
    public $errorKey;
87
88
    /**
89
     * Create a new component instance.
90 21
     *
91 21
     * @return void
92 21
     */
93 21
    public function __construct(
94 21
        $name, $id = null, $label = null, $igroupSize = null, $labelClass = null,
95 21
        $fgroupClass = null, $igroupClass = null, $disableFeedback = null,
96 21
        $errorKey = null
97 21
    ) {
98
        $this->id = $id ?? $name;
99
        $this->name = $name;
100
        $this->label = $label;
101 21
        $this->size = $igroupSize;
102 21
        $this->fgroupClass = $fgroupClass;
103
        $this->labelClass = $labelClass;
104
        $this->igroupClass = $igroupClass;
105
        $this->disableFeedback = $disableFeedback;
106
107
        // Setup the lookup key for validation errors.
108
109 1
        $this->errorKey = $errorKey ?? $this->makeErrorKey();
110
111 1
        // Initialize the internal errors bag holder variable.
112
113 1
        $this->errorsBag = null;
114 1
    }
115
116
    /**
117 1
     * Make the class attribute for the "form-group" element.
118
     *
119
     * @return string
120
     */
121
    public function makeFormGroupClass()
122
    {
123
        $classes = ['form-group'];
124
125 1
        if (isset($this->fgroupClass)) {
126
            $classes[] = $this->fgroupClass;
127 1
        }
128
129 1
        return implode(' ', $classes);
130 1
    }
131
132
    /**
133 1
     * Make the class attribute for the "input-group" element.
134 1
     *
135
     * @return string
136
     */
137 1
    public function makeInputGroupClass()
138 1
    {
139
        $classes = ['input-group'];
140
141 1
        if (isset($this->size) && in_array($this->size, ['sm', 'lg'])) {
142
            $classes[] = "input-group-{$this->size}";
143
        }
144
145
        if ($this->isInvalid() && ! isset($this->disableFeedback)) {
146
            $classes[] = 'adminlte-invalid-igroup';
147
        }
148
149 6
        if (isset($this->igroupClass)) {
150
            $classes[] = $this->igroupClass;
151 6
        }
152
153 6
        return implode(' ', $classes);
154 6
    }
155
156
    /**
157 6
     * Make the class attribute for the input group item.
158
     *
159
     * @return string
160
     */
161
    public function makeItemClass()
162
    {
163
        $classes = ['form-control'];
164
165
        if ($this->isInvalid() && ! isset($this->disableFeedback)) {
166 11
            $classes[] = 'is-invalid';
167
        }
168
169
        return implode(' ', $classes);
170
    }
171 11
172
    /**
173
     * Check if there exists validation errors on the session related to the
174
     * configured error key.
175 11
     *
176
     * @return bool
177
     */
178
    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
        $errors = $this->errorsBag ?? session()->get('errors');
186
187 21
        // Check if exists any error related to the configured error key.
188
189 21
        return ! empty($errors) && $errors->has($this->errorKey);
190
    }
191 21
192
    /**
193
     * Setup the internal errors bag.
194
     *
195
     * @param  \Illuminate\Support\MessageBag  $errorsBag
196
     * @return void
197
     */
198
    public function setErrorsBag($errorsBag)
199 1
    {
200
        $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
    protected function makeErrorKey()
213
    {
214
        $errKey = preg_replace('@\[\]$@', '', $this->name);
215
216
        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
    public function render()
225
    {
226
        return view('adminlte::components.form.input-group-component');
227
    }
228
}
229