Passed
Push — master ( 8f6522...eef865 )
by Florian
06:34
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
     * The id attribute for the underlying input group item. The input group
11
     * item may be an "input", a "select", a "textarea", etc.
12
     *
13
     * @var string
14
     */
15
    public $id;
16
17
    /**
18
     * The name attribute for the underlying input group item. This value will
19
     * be used as the default id attribute when not provided. The input group
20
     * item may be an "input", a "select", a "textarea", etc.
21
     *
22
     * @var string
23
     */
24
    public $name;
25
26
    /**
27
     * The label of the input group.
28
     *
29
     * @var string
30
     */
31
    public $label;
32
33
    /**
34
     * The input group size (you can specify 'sm' or 'lg').
35
     *
36
     * @var string
37
     */
38
    public $size;
39
40
    /**
41
     * Additional classes for "input-group" element. This provides a way to
42
     * customize the input group container style.
43
     *
44
     * @var string
45
     */
46
    public $igroupClass;
47
48
    /**
49
     * Extra classes for the label container. This provides a way to customize
50
     * the label style.
51
     *
52
     * @var string
53
     */
54
    public $labelClass;
55
56
    /**
57
     * Extra classes for the "form-group" element. This provides a way to
58
     * customize the main container style.
59
     *
60
     * @var string
61
     */
62
    public $fgroupClass;
63
64
    /**
65
     * Indicates if the invalid feedback is disabled for the input group.
66
     *
67
     * @var bool
68
     */
69
    public $disableFeedback;
70
71
    /**
72
     * The lookup key to use when searching for validation errors. The lookup
73
     * key is automatically generated from the name property. This provides a
74
     * way to overwrite that value.
75
     *
76
     * @var string
77
     */
78
    public $errorKey;
79
80
    /**
81
     * Create a new component instance.
82
     *
83
     * @return void
84
     */
85 12
    public function __construct(
86
        $name, $id = null, $label = null, $igroupSize = null, $labelClass = null,
87
        $fgroupClass = null, $igroupClass = null, $disableFeedback = null,
88
        $errorKey = null
89
    ) {
90 12
        $this->id = $id ?? $name;
91 12
        $this->name = $name;
92 12
        $this->label = $label;
93 12
        $this->size = $igroupSize;
94 12
        $this->fgroupClass = $fgroupClass;
95 12
        $this->labelClass = $labelClass;
96 12
        $this->igroupClass = $igroupClass;
97 12
        $this->disableFeedback = $disableFeedback;
98
99
        // Setup the lookup key for validation errors.
100
101 12
        $this->errorKey = $errorKey ?? $this->makeErrorKey();
102 12
    }
103
104
    /**
105
     * Make the class attribute for the "form-group" element.
106
     *
107
     * @return string
108
     */
109 1
    public function makeFormGroupClass()
110
    {
111 1
        $classes = ['form-group'];
112
113 1
        if (isset($this->fgroupClass)) {
114 1
            $classes[] = $this->fgroupClass;
115
        }
116
117 1
        return implode(' ', $classes);
118
    }
119
120
    /**
121
     * Make the class attribute for the "input-group" element.
122
     *
123
     * @return string
124
     */
125 1
    public function makeInputGroupClass()
126
    {
127 1
        $classes = ['input-group'];
128
129 1
        if (isset($this->size) && in_array($this->size, ['sm', 'lg'])) {
130 1
            $classes[] = "input-group-{$this->size}";
131
        }
132
133 1
        if ($this->isInvalid() && ! isset($this->disableFeedback)) {
134 1
            $classes[] = 'adminlte-invalid-igroup';
135
        }
136
137 1
        if (isset($this->igroupClass)) {
138 1
            $classes[] = $this->igroupClass;
139
        }
140
141 1
        return implode(' ', $classes);
142
    }
143
144
    /**
145
     * Make the class attribute for the input group item.
146
     *
147
     * @return string
148
     */
149 4
    public function makeItemClass()
150
    {
151 4
        $classes = ['form-control'];
152
153 4
        if ($this->isInvalid() && ! isset($this->disableFeedback)) {
154 4
            $classes[] = 'is-invalid';
155
        }
156
157 4
        return implode(' ', $classes);
158
    }
159
160
    /**
161
     * Check if there exists validation errors on the session related to the
162
     * configured error key.
163
     *
164
     * @return bool
165
     */
166 9
    public function isInvalid()
167
    {
168
        // Get the errors bag from session. The errors bag will be an instance
169
        // of the Illuminate\Support\MessageBag class.
170
171 9
        $errors = session()->get('errors');
172
173
        // Check if exists any error related to the configured error key.
174
175 9
        return ! empty($errors) && ! empty($errors->first($this->errorKey));
176
    }
177
178
    /**
179
     * Make the error key that will be used to search for validation errors.
180
     * The error key is generated from the 'name' property.
181
     * Examples:
182
     * $name = 'files[]'         => $errorKey = 'files'.
183
     * $name = 'person[2][name]' => $errorKey = 'person.2.name'.
184
     *
185
     * @return string
186
     */
187 12
    protected function makeErrorKey()
188
    {
189 12
        $errKey = preg_replace('@\[\]$@', '', $this->name);
190
191 12
        return preg_replace('@\[([^]]+)\]@', '.$1', $errKey);
192
    }
193
194
    /**
195
     * Get the view / contents that represent the component.
196
     *
197
     * @return \Illuminate\View\View|string
198
     */
199 1
    public function render()
200
    {
201 1
        return view('adminlte::components.form.input-group-component');
202
    }
203
}
204