MultiSelect   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getValue() 0 10 3
A setValue() 0 13 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\Html\Attribute;
9
use AbterPhp\Framework\Html\Helper\Attributes;
10
11
class MultiSelect extends Select
12
{
13
    protected const ERROR_NO_CONTENT = 'MultiSelect can not contain nodes';
14
15
    /**
16
     * MultiSelect constructor.
17
     *
18
     * @param string                        $inputId
19
     * @param string                        $name
20
     * @param string[]                      $intents
21
     * @param array<string, Attribute>|null $attributes
22
     * @param string|null                   $tag
23
     */
24
    public function __construct(
25
        string $inputId,
26
        string $name,
27
        array $intents = [],
28
        ?array $attributes = null,
29
        ?string $tag = null
30
    ) {
31
        $attributes ??= [];
32
        $attributes = Attributes::addItem($attributes, Html5::ATTR_MULTIPLE);
33
34
        parent::__construct($inputId, $name, $intents, $attributes, $tag);
35
    }
36
37
    /**
38
     * @suppress PhanParamSignatureMismatch
39
     *
40
     * @return string[]
41
     */
42
    public function getValue()
43
    {
44
        $values = [];
45
        foreach ($this->content as $option) {
46
            if ($option->hasAttribute(Html5::ATTR_SELECTED)) {
47
                $values[] = $option->getValue();
48
            }
49
        }
50
51
        return $values;
52
    }
53
54
    /**
55
     * @param string|string[] $value
56
     *
57
     * @return $this
58
     */
59
    public function setValue($value): self
60
    {
61
        if (!is_array($value)) {
62
            throw new \InvalidArgumentException();
63
        }
64
65
        foreach ($value as $v) {
66
            if (!is_string($v)) {
67
                throw new \InvalidArgumentException();
68
            }
69
        }
70
71
        return $this->setValueInner($value);
72
    }
73
}
74