Passed
Pull Request — master (#155)
by Wilmer
11:04
created

WithoutModelAttribute::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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
15
    /**
16
     * The prefix to the automatically generated widget IDs.
17
     *
18
     * @param string $value
19
     *
20
     * @return static
21
     */
22 3
    public function autoIdPrefix(string $value): self
23
    {
24 3
        $new = clone $this;
25 3
        $new->autoIdPrefix = $value;
26 3
        return $new;
27
    }
28
29
    /**
30
     * The HTML attributes. The following special options are recognized.
31
     *
32
     * @param array $value
33
     *
34
     * @return static
35
     *
36
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
37
     */
38 18
    public function attributes(array $value): self
39
    {
40 18
        $new = clone $this;
41 18
        $new->attributes = $value;
42 18
        return $new;
43
    }
44
45
    /**
46
     * Set the ID of the widget.
47
     *
48
     * @param string $id
49
     *
50
     * @return static
51
     *
52
     * @link https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute
53
     */
54 3
    public function id(string $id): self
55
    {
56 3
        $new = clone $this;
57 3
        $new->id = $id;
58 3
        return $new;
59
    }
60
61
    /**
62
     * Generates a unique ID for the attribute.
63
     *
64
     * @return string
65
     */
66 27
    protected function getId(): string
67
    {
68 27
        return $this->id = $this->id !== '' ? $this->id : Html::generateId($this->autoIdPrefix);
69
    }
70
71 27
    protected function getName(): string
72
    {
73 27
        $name = $this->getId();
74
75 27
        if (isset($this->attributes['name']) && is_string($this->attributes['name'])) {
76 2
            $name = $this->attributes['name'];
77
        }
78
79 27
        return $name;
80
    }
81
}
82