Passed
Push — master ( 9e595b...a235a8 )
by Florian
02:58 queued 40s
created

InputGroupComponent::makeInputGroupClass()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
nc 8
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 6
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Components;
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 10
    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 10
        $this->id = $id ?? $name;
91 10
        $this->name = $name;
92 10
        $this->label = $label;
93 10
        $this->size = $igroupSize;
94 10
        $this->fgroupClass = $fgroupClass;
95 10
        $this->labelClass = $labelClass;
96 10
        $this->igroupClass = $igroupClass;
97 10
        $this->disableFeedback = $disableFeedback;
98
99
        // Setup the lookup key for validation errors.
100
101 10
        $this->errorKey = $errorKey ?? $this->makeErrorKey();
102 10
    }
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
     * @param string $invalid
124
     * @return string
125
     */
126 1
    public function makeInputGroupClass($invalid = null)
127
    {
128 1
        $classes = ['input-group'];
129
130 1
        if (isset($this->size) && in_array($this->size, ['sm', 'lg'])) {
131 1
            $classes[] = "input-group-{$this->size}";
132
        }
133
134 1
        if (! empty($invalid) && ! isset($this->disableFeedback)) {
135 1
            $classes[] = 'adminlte-invalid-igroup';
136
        }
137
138 1
        if (isset($this->igroupClass)) {
139 1
            $classes[] = $this->igroupClass;
140
        }
141
142 1
        return implode(' ', $classes);
143
    }
144
145
    /**
146
     * Make the class attribute for the input group item.
147
     *
148
     * @param string $invalid
149
     * @return string
150
     */
151 4
    public function makeItemClass($invalid = null)
152
    {
153 4
        $classes = ['form-control'];
154
155 4
        if (! empty($invalid) && ! isset($this->disableFeedback)) {
156 4
            $classes[] = 'is-invalid';
157
        }
158
159 4
        return implode(' ', $classes);
160
    }
161
162
    /**
163
     * Make the error key that will be used to search for validation errors.
164
     * The error key is generated from the 'name' property.
165
     * Examples:
166
     * $name = 'files[]'         => $errorKey = 'files'.
167
     * $name = 'person[2][name]' => $errorKey = 'person.2.name'.
168
     *
169
     * @return string
170
     */
171 10
    protected function makeErrorKey()
172
    {
173 10
        $errKey = preg_replace('@\[\]$@', '', $this->name);
174
175 10
        return preg_replace('@\[([^]]+)\]@', '.$1', $errKey);
176
    }
177
178
    /**
179
     * Get the view / contents that represent the component.
180
     *
181
     * @return \Illuminate\View\View|string
182
     */
183 1
    public function render()
184
    {
185 1
        return view('adminlte::components.input-group-component');
186
    }
187
}
188