TWrappers::addWrapperInput()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Controls;
4
5
6
use kalanis\kw_forms\Exceptions\RenderException;
7
use kalanis\kw_templates\HtmlElement;
8
use kalanis\kw_templates\Interfaces\IHtmlElement;
9
10
11
/**
12
 * Wrapper trait for form inputs
13
 * @author Petr Plsek
14
 * @author Adam Dornak
15
 * @author Petr Bolehovsky
16
 */
17
trait TWrappers
18
{
19
    use TTemplateError;
20
21
    /** @var IHtmlElement[] */
22
    protected array $wrappers = [];
23
    /** @var IHtmlElement[] */
24
    protected array $wrappersLabel = [];
25
    /** @var IHtmlElement[] */
26
    protected array $wrappersInput = [];
27
    /** @var IHtmlElement[] */
28
    protected array $wrappersChild = [];
29
    /** @var IHtmlElement[] */
30
    protected array $wrappersChildren = [];
31
    /** @var IHtmlElement[] */
32
    protected array $wrappersError = [];
33
    /** @var IHtmlElement[] */
34
    protected array $wrappersErrors = [];
35
    protected string $errorMustBeAnInstance = 'Wrapper must be an instance of IHtmlElement or array of its instances';
36
37
    /**
38
     * Pack string into preset html element
39
     * @param string $string
40
     * @param IHtmlElement|IHtmlElement[] $wrappers
41
     * @throws RenderException
42
     * @return string
43
     */
44 38
    protected function wrapIt(string $string, $wrappers): string
45
    {
46 38
        $return = $string;
47 38
        if (is_array($wrappers)) {
48 36
            foreach ($wrappers as $wrapper) {
49 2
                $return = $this->wrapIt($return, $wrapper);
50
            }
51 4
        } elseif ($wrappers instanceof IHtmlElement) {
0 ignored issues
show
introduced by
$wrappers is always a sub-type of kalanis\kw_templates\Interfaces\IHtmlElement.
Loading history...
52 3
            $wrappers->addChild($return);
53 3
            $return = $wrappers->render();
54
        } else {
55 1
            throw new RenderException($this->errorMustBeAnInstance);
56
        }
57
58 37
        return $return;
59
    }
60
61
    /**
62
     * Add wrapper into predefined stack
63
     * @param IHtmlElement[] $stack
64
     * @param IHtmlElement|IHtmlElement[]|string|string[] $wrapper
65
     * @param array<string, string> $attributes
66
     */
67 6
    protected function addWrapperToStack(array &$stack, $wrapper, array $attributes = []): void
68
    {
69 6
        if (is_array($wrapper)) {
70 4
            foreach ($wrapper as $_wrapper) {
71 2
                $this->addWrapperToStack($stack, $_wrapper);
72
            }
73
        } else {
74 4
            if (!($wrapper instanceof IHtmlElement)) {
75 4
                $wrapper = HtmlElement::init($wrapper, $attributes);
76 2
            } elseif (!empty($attributes)) {
77 1
                $wrapper->setAttributes($attributes);
78
            }
79 4
            if (!in_array($wrapper, $stack)) {
80 4
                $stack[] = $wrapper;
81
            }
82
        }
83 6
    }
84
85
    /**
86
     * Add wrapper for the whole object
87
     * @param string|string[]|IHtmlElement|IHtmlElement[] $wrapper
88
     * @param array<string, string> $attributes
89
     * @see AControl::render
90
     */
91 3
    public function addWrapper($wrapper, array $attributes = []): void
92
    {
93 3
        $this->addWrapperToStack($this->wrappers, $wrapper, $attributes);
94 3
    }
95
96
    /**
97
     * Add wrapper for each child
98
     * @param string|string[]|IHtmlElement|IHtmlElement[] $wrapper
99
     * @param array<string, string> $attributes
100
     * @see AControl::renderChild
101
     */
102 2
    public function addWrapperChild($wrapper, array $attributes = []): void
103
    {
104 2
        $this->addWrapperToStack($this->wrappersChild, $wrapper, $attributes);
105 2
    }
106
107
    /**
108
     * Add wrapper for labels
109
     * @param string|string[]|IHtmlElement|IHtmlElement[] $wrapper
110
     * @param array<string, string> $attributes
111
     * @see AControl::renderLabel
112
     */
113 5
    public function addWrapperLabel($wrapper, array $attributes = []): void
114
    {
115 5
        $this->addWrapperToStack($this->wrappersLabel, $wrapper, $attributes);
116 5
    }
117
118
    /**
119
     * Add wrapper for inputs
120
     * @param string|string[]|IHtmlElement|IHtmlElement[] $wrapper
121
     * @param array<string, string> $attributes
122
     * @see AControl::renderInput
123
     */
124 3
    public function addWrapperInput($wrapper, array $attributes = []): void
125
    {
126 3
        $this->addWrapperToStack($this->wrappersInput, $wrapper, $attributes);
127 3
    }
128
129
    /**
130
     * Add wrapper for content of children
131
     * @param string|string[]|IHtmlElement|IHtmlElement[] $wrapper
132
     * @param array<string, string> $attributes
133
     * @see AControl::renderChildren
134
     */
135 2
    public function addWrapperChildren($wrapper, array $attributes = []): void
136
    {
137 2
        $this->addWrapperToStack($this->wrappersChildren, $wrapper, $attributes);
138 2
    }
139
140
    /**
141
     * Add wrapper for error messages
142
     * @param string|string[]|IHtmlElement|IHtmlElement[] $wrapper
143
     * @param array<string, string> $attributes
144
     * @see AControl::renderErrors
145
     */
146 2
    public function addWrapperError($wrapper, array $attributes = []): void
147
    {
148 2
        $this->addWrapperToStack($this->wrappersError, $wrapper, $attributes);
149 2
    }
150
151
    /**
152
     * Add wrapper for error messages
153
     * @param string|string[]|IHtmlElement|IHtmlElement[] $wrapper
154
     * @param array<string, string> $attributes
155
     * @see AControl::renderErrors
156
     */
157 4
    public function addWrapperErrors($wrapper, array $attributes = []): void
158
    {
159 4
        $this->addWrapperToStack($this->wrappersErrors, $wrapper, $attributes);
160 4
    }
161
162
    /**
163
     * @return IHtmlElement[]
164
     */
165 3
    public function wrappers(): array
166
    {
167 3
        return $this->wrappers;
168
    }
169
170
    /**
171
     * @return IHtmlElement[]
172
     */
173 5
    public function wrappersLabel(): array
174
    {
175 5
        return $this->wrappersLabel;
176
    }
177
178
    /**
179
     * @return IHtmlElement[]
180
     */
181 3
    public function wrappersInput(): array
182
    {
183 3
        return $this->wrappersInput;
184
    }
185
186
    /**
187
     * @return IHtmlElement[]
188
     */
189 1
    public function wrappersChild(): array
190
    {
191 1
        return $this->wrappersChild;
192
    }
193
194
    /**
195
     * @return IHtmlElement[]
196
     */
197 1
    public function wrappersChildren(): array
198
    {
199 1
        return $this->wrappersChildren;
200
    }
201
202
    /**
203
     * @return IHtmlElement[]
204
     */
205 1
    public function wrappersError(): array
206
    {
207 1
        return $this->wrappersError;
208
    }
209
210
    /**
211
     * @return IHtmlElement[]
212
     */
213 3
    public function wrappersErrors(): array
214
    {
215 3
        return $this->wrappersErrors;
216
    }
217
218 4
    public function resetWrappers(): void
219
    {
220 4
        $this->wrappers = [];
221 4
        $this->wrappersLabel = [];
222 4
        $this->wrappersInput = [];
223 4
        $this->wrappersChild = [];
224 4
        $this->wrappersChildren = [];
225 4
        $this->wrappersError = [];
226 4
        $this->wrappersErrors = [];
227 4
    }
228
}
229