Passed
Pull Request — master (#151)
by Wilmer
02:19
created

WithoutModelAttribute::generateId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\Attribute;
6
7
use Yiisoft\Html\Html;
8
9
trait WithoutModelAttribute
10
{
11
    private string $autoIdPrefix = '';
12
    private array $attributes = [];
13
    private string $id = '';
14
    private string $name = '';
15
    private string $value = '';
16
17
    /**
18
     * The prefix to the automatically generated widget IDs.
19
     *
20
     * @param string $value
21
     *
22
     * @return static
23
     */
24 3
    public function autoIdPrefix(string $value): self
25
    {
26 3
        $new = clone $this;
27 3
        $new->autoIdPrefix = $value;
28 3
        return $new;
29
    }
30
31
    /**
32
     * The HTML attributes. The following special options are recognized.
33
     *
34
     * @param array $value
35
     *
36
     * @return static
37
     *
38
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
39
     */
40 18
    public function attributes(array $value): self
41
    {
42 18
        $new = clone $this;
43 18
        $new->attributes = $value;
44 18
        return $new;
45
    }
46
47
    /**
48
     * Set the ID of the widget.
49
     *
50
     * @param string $id
51
     *
52
     * @return static
53
     *
54
     * @link https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute
55
     */
56 3
    public function id(string $id): self
57
    {
58 3
        $new = clone $this;
59 3
        $new->id = $id;
60 3
        return $new;
61
    }
62
63
    /**
64
     * The name part of the name/value pair associated with this element for the purposes of form submission.
65
     *
66
     * @param string The name of the widget.
0 ignored issues
show
Bug introduced by
The type Yiisoft\Form\Widget\Attribute\The 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...
67
     *
68
     * @return static
69
     *
70
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.reset.html#input.reset.attrs.name
71
     */
72 2
    public function name(string $value): self
73
    {
74 2
        $new = clone $this;
75 2
        $new->name = $value;
76 2
        return $new;
77
    }
78
79
    /**
80
     * Specifies a value for the input element.
81
     *
82
     * @param string $value
83
     *
84
     * @return static
85
     *
86
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.reset.html#input.reset.attrs.value
87
     */
88 3
    public function value(string $value): self
89
    {
90 3
        $new = clone $this;
91 3
        $new->value = $value;
92 3
        return $new;
93
    }
94
95
    /**
96
     * Generates a unique ID for the attribute.
97
     *
98
     * @return string
99
     */
100 28
    protected function generateId(): string
101
    {
102 28
        return $this->id = $this->id !== '' ? $this->id : Html::generateId($this->autoIdPrefix);
103
    }
104
105 28
    protected function getName(): string
106
    {
107 28
        return $this->name = $this->name !== '' ? $this->name : $this->generateId();
108
    }
109
}
110