Passed
Pull Request — master (#192)
by Alexander
02:31
created

Hidden   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 27.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 54
ccs 5
cts 18
cp 0.2778
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generateInput() 0 12 4
A form() 0 5 1
A ariaDescribedBy() 0 5 1
A ariaLabel() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\AbstractField;
9
use Yiisoft\Html\Html;
10
11
use function is_string;
12
13
/**
14
 * The input element with a type attribute whose value is "hidden" represents a value that is not intended to be
15
 * examined or manipulated by the user.
16
 *
17
 * @link https://html.spec.whatwg.org/multipage/input.html#hidden-state-(type=hidden)
18
 */
19
final class Hidden extends AbstractField
20
{
21
    protected bool $useContainer = false;
22
    protected string $template = '{input}';
23
24
    /**
25
     * Identifies the element (or elements) that describes the object.
26
     *
27
     * @link https://w3c.github.io/aria/#aria-describedby
28
     */
29
    public function ariaDescribedBy(string $value): self
30
    {
31
        $new = clone $this;
32
        $new->inputTagAttributes['aria-describedby'] = $value;
33
        return $new;
34
    }
35
36
    /**
37
     * Defines a string value that labels the current element.
38
     *
39
     * @link https://w3c.github.io/aria/#aria-label
40
     */
41
    public function ariaLabel(string $value): self
42
    {
43
        $new = clone $this;
44
        $new->inputTagAttributes['aria-label'] = $value;
45
        return $new;
46
    }
47
48
    /**
49
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
50
     * attribute of a form element in the same document.
51
     *
52
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
53
     */
54
    public function form(string $value): self
55
    {
56
        $new = clone $this;
57
        $new->inputTagAttributes['form'] = $value;
58
        return $new;
59
    }
60
61 1
    protected function generateInput(): string
62
    {
63 1
        $value = $this->getAttributeValue();
64
65 1
        if (!is_string($value) && !is_numeric($value) && $value !== null) {
66
            throw new InvalidArgumentException('Hidden widget requires a string, numeric or null value.');
67
        }
68
69 1
        $tagAttributes = $this->getInputTagAttributes();
70
71
        /** @psalm-suppress MixedArgumentTypeCoercion */
72 1
        return Html::hiddenInput($this->getInputName(), $value, $tagAttributes)->render();
73
    }
74
}
75