Select   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 116
rs 10
c 1
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 9 3
A setContent() 0 7 2
A __construct() 0 12 1
A getName() 0 3 1
A setValue() 0 7 2
A __toString() 0 5 1
A setValueInner() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Element;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Form\Component\Option;
9
use AbterPhp\Framework\Html\Attribute;
10
use AbterPhp\Framework\Html\Helper\Attributes;
11
use AbterPhp\Framework\Html\Helper\Tag as TagHelper;
12
use AbterPhp\Framework\Html\IStringer;
13
use AbterPhp\Framework\Html\Node;
14
use AbterPhp\Framework\Html\Tag;
15
use LogicException;
16
17
class Select extends Input
18
{
19
    protected const ERROR_NO_CONTENT = 'Select can not contain nodes';
20
21
    protected const SEPARATOR = "\n";
22
23
    protected const DEFAULT_TAG  = Html5::TAG_SELECT;
24
    protected const CONTENT_TYPE = Option::class;
25
26
    protected const PROTECTED_KEYS = [Html5::ATTR_ID, Html5::ATTR_NAME];
27
28
    /** @var Option[] */
29
    protected array $content = [];
30
31
    /**
32
     * Select constructor.
33
     *
34
     * @param string                        $inputId
35
     * @param string                        $name
36
     * @param string[]                      $intents
37
     * @param array<string, Attribute>|null $attributes
38
     * @param string|null                   $tag
39
     */
40
    public function __construct(
41
        string $inputId,
42
        string $name,
43
        array $intents = [],
44
        ?array $attributes = null,
45
        ?string $tag = null
46
    ) {
47
        $attributes ??= [];
48
        $attributes = Attributes::addItem($attributes, Html5::ATTR_ID, $inputId);
49
        $attributes = Attributes::addItem($attributes, Html5::ATTR_NAME, $name);
50
51
        Tag::__construct(null, $intents, $attributes, $tag);
52
    }
53
54
    /**
55
     * @param array<string|IStringer>|string|IStringer|null $content
56
     *
57
     * @return $this
58
     */
59
    public function setContent($content): self
60
    {
61
        if (null === $content) {
62
            return $this;
63
        }
64
65
        throw new LogicException(static::ERROR_NO_CONTENT);
66
    }
67
68
    /**
69
     * @param string|string[] $value
70
     *
71
     * @return $this
72
     */
73
    public function setValue($value): self
74
    {
75
        if (!is_string($value)) {
76
            throw new \InvalidArgumentException();
77
        }
78
79
        return $this->setValueInner([$value]);
80
    }
81
82
    /**
83
     * @param string[] $values
84
     *
85
     * @return $this
86
     */
87
    protected function setValueInner(array $values): self
88
    {
89
        foreach ($this->content as $option) {
90
            $value = $option->getValue();
91
            if (in_array($value, $values, true)) {
92
                $option->setAttribute(new Attribute(Html5::ATTR_SELECTED));
93
            } elseif ($option->hasAttribute(Html5::ATTR_SELECTED)) {
94
                $option->removeAttribute(Html5::ATTR_SELECTED);
95
            }
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * @suppress PhanParamSignatureMismatch
103
     *
104
     * @return string|null
105
     */
106
    public function getValue()
107
    {
108
        foreach ($this->content as $option) {
109
            if ($option->hasAttribute(Html5::ATTR_SELECTED)) {
110
                return $option->getValue();
111
            }
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getName(): string
121
    {
122
        return $this->attributes[Html5::ATTR_NAME]->getValue();
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function __toString(): string
129
    {
130
        $content = Node::__toString();
131
132
        return TagHelper::toString($this->tag, $content, $this->getAttributes());
133
    }
134
}
135