Test Failed
Pull Request — master (#159)
by Sergei
02:23
created

AbstractField::setInputIdAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use InvalidArgumentException;
8
use Stringable;
9
use Yiisoft\Form\FormModelInterface;
10
use Yiisoft\Form\Helper\HtmlForm;
11
use Yiisoft\Html\Tag\Base\Tag;
12
use Yiisoft\Html\Tag\Base\TagContentInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Html\Tag\Base\TagContentInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Html\Tag\Div;
14
use Yiisoft\Html\Tag\Label;
15
use Yiisoft\Widget\Widget;
16
17
abstract class AbstractField extends Widget
18
{
19
    private ?FormModelInterface $formModel = null;
20
    private string $attribute = '';
21
22
    private ?TagContentInterface $containerTag = null;
23
    private bool $withoutContainer = false;
24
25
    private string $template = "{label}\n{input}\n{hint}\n{error}";
26
27
    private ?string $inputId = null;
28
    private bool $setInputIdAttribute = true;
29
30
    private ?Label $labelTag = null;
31
    private ?string $labelContent = null;
32
    private bool $setLabelForAttribute = true;
33
34
    private ?TagContentInterface $hintTag = null;
35
    private ?string $hintContent = null;
36
37
    /**
38
     * @return static
39
     */
40
    final public function attribute(FormModelInterface $formModel, string $attribute): self
41
    {
42
        $new = clone $this;
43
        $new->formModel = $formModel;
44
        $new->attribute = $attribute;
45
        return $new;
46
    }
47
48
    final public function containerTag(?TagContentInterface $tag): self
49
    {
50
        $new = clone $this;
51
        $new->containerTag = $tag;
52
        return $new;
53
    }
54
55
    final public function withoutContainer(): self
56
    {
57
        $new = clone $this;
58
        $new->withoutContainer = true;
59
        return $new;
60
    }
61
62
    /**
63
     * Set layout template for render a field.
64
     *
65
     * @param string $template
66
     *
67
     * @return static
68
     */
69
    final public function template(string $template): self
70
    {
71
        $new = clone $this;
72
        $new->template = $template;
73
        return $new;
74
    }
75
76
    /**
77
     * @return static
78
     */
79
    final public function id(?string $id): self
80
    {
81
        $new = clone $this;
82
        $new->inputId = $id;
83
        return $new;
84
    }
85
86
    /**
87
     * @return static
88
     */
89
    final public function setInputIdAttribute(bool $value): self
90
    {
91
        $new = clone $this;
92
        $new->setInputIdAttribute = $value;
93
        return $new;
94
    }
95
96
    /**
97
     * @return static
98
     */
99
    final public function labelTag(?Label $tag): self
100
    {
101
        $new = clone $this;
102
        $new->labelTag = $tag;
103
        return $new;
104
    }
105
106
    /**
107
     * @return static
108
     */
109
    final public function label(?string $content): self
110
    {
111
        $new = clone $this;
112
        $new->labelContent = $content;
113
        return $new;
114
    }
115
116
    /**
117
     * @return static
118
     */
119
    final public function setLabelForAttribute(bool $value): self
120
    {
121
        $new = clone $this;
122
        $new->setLabelForAttribute = $value;
123
        return $new;
124
    }
125
126
    final public function hintTag(?TagContentInterface $tag): self
127
    {
128
        $new = clone $this;
129
        $new->hintTag = $tag;
130
        return $new;
131
    }
132
133
    final public function hint(?string $content): self
134
    {
135
        $new = clone $this;
136
        $new->hintContent = $content;
137
        return $new;
138
    }
139
140
    final protected function getFormModel(): FormModelInterface
141
    {
142
        if ($this->formModel === null) {
143
            throw new InvalidArgumentException('Form model is not set.');
144
        }
145
146
        return $this->formModel;
147
    }
148
149
    final protected function getInputName(): string
150
    {
151
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
152
    }
153
154
    final protected function getInputId(): ?string
155
    {
156
        if (!$this->setInputIdAttribute) {
157
            return null;
158
        }
159
160
        return $this->inputId ?? HtmlForm::getInputId($this->getFormModel(), $this->attribute);
161
    }
162
163
    /**
164
     * @return bool|float|int|iterable|object|string|Stringable|null
165
     */
166
    final protected function getAttributeValue()
167
    {
168
        return HtmlForm::getAttributeValue($this->getFormModel(), $this->attribute);
169
    }
170
171
    final protected function getAttributeLabel(): string
172
    {
173
        return HtmlForm::getAttributeLabel($this->getFormModel(), $this->attribute);
174
    }
175
176
    final protected function getAttributeHint(): string
177
    {
178
        return HtmlForm::getAttributeHint($this->getFormModel(), $this->attribute);
179
    }
180
181
    final protected function getAttributePlaceholder(): ?string
182
    {
183
        return $this->getFormModel()->getAttributePlaceholder($this->attribute);
184
    }
185
186
    final protected function prepareIdInInputTag(Tag $tag): Tag
187
    {
188
        if (
189
            $this->setInputIdAttribute
190
            && $tag->getAttribute('id') === null
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on Yiisoft\Html\Tag\Base\Tag. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
            && $tag->/** @scrutinizer ignore-call */ getAttribute('id') === null

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
        ) {
192
            $id = $this->getInputId();
193
            if ($id !== null) {
194
                $tag = $tag->id($id);
195
            }
196
        }
197
198
        return $tag;
199
    }
200
201
    final protected function run(): string
202
    {
203
        if ($this->withoutContainer) {
204
            return $this->generateContent();
205
        }
206
207
        $containerTag = $this->containerTag ?? Div::tag();
208
209
        return $containerTag->open()
210
            . PHP_EOL
211
            . $this->generateContent()
212
            . PHP_EOL
213
            . $containerTag->close();
214
    }
215
216
    abstract protected function generateInput(): string;
217
218
    private function generateContent(): string
219
    {
220
        $parts = [
221
            '{label}' => $this->generateLabel(),
222
            '{input}' => $this->generateInput(),
223
            '{hint}' => $this->generateHint(),
224
            '{error}' => $this->generateError(),
225
        ];
226
227
        return preg_replace('/^\h*\v+/m', '', trim(strtr($this->template, $parts)));
228
    }
229
230
    private function generateLabel(): string
231
    {
232
        $tag = $this->labelTag ?? Label::tag();
233
234
        if ($this->labelContent !== null) {
235
            $tag = $tag->content($this->labelContent);
236
        } elseif ($tag->getContent() === '') {
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on Yiisoft\Html\Tag\Label. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
        } elseif ($tag->/** @scrutinizer ignore-call */ getContent() === '') {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
237
            $tag = $tag->content($this->getAttributeLabel());
238
        }
239
240
        if (
241
            $this->setLabelForAttribute
242
            && $tag->getAttribute('for') === null
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on Yiisoft\Html\Tag\Label. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

242
            && $tag->/** @scrutinizer ignore-call */ getAttribute('for') === null

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
243
        ) {
244
            $id = $this->getInputId();
245
            if ($id !== null) {
246
                $tag = $tag->forId($id);
247
            }
248
        }
249
250
        return $tag->render();
251
    }
252
253
    private function generateHint(): string
254
    {
255
        $tag = $this->hintTag ?? Div::tag();
256
257
        if ($this->hintContent !== null) {
258
            $tag = $tag->content($this->hintContent);
259
        } elseif ($tag->getContent() === '') {
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on Yiisoft\Html\Tag\Div. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

259
        } elseif ($tag->/** @scrutinizer ignore-call */ getContent() === '') {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
260
            $tag = $tag->content($this->getAttributeHint());
261
        }
262
263
        return $tag->render();
264
    }
265
266
    private function generateError(): string
267
    {
268
        return '';
269
    }
270
}
271