Completed
Push — master ( 595a55...3ece2b )
by Sebastian
02:08
created

Select::multiple()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 0
1
<?php
2
3
namespace Spatie\Html\Elements;
4
5
use Illuminate\Support\Str;
6
use Spatie\Html\Selectable;
7
use Spatie\Html\BaseElement;
8
use Illuminate\Support\Collection;
9
10
class Select extends BaseElement
11
{
12
    /** @var string */
13
    protected $tag = 'select';
14
15
    /** @var array */
16
    protected $options = [];
17
18
    /** @var string|iterable */
19
    protected $value = '';
20
21
    /**
22
     * @return static
23
     */
24
    public function multiple()
25
    {
26
        $element = clone $this;
27
28
        $element = $element->attribute('multiple');
29
30
        $name = $element->getAttribute('name');
31
32
        if ($name && ! Str::endsWith($name, '[]')) {
33
            $element = $element->name($name.'[]');
34
        }
35
36
        $element->applyValueToOptions();
37
38
        return $element;
39
    }
40
41
    /**
42
     * @param string $name
43
     *
44
     * @return static
45
     */
46
    public function name(?string $name)
47
    {
48
        return $this->attribute('name', $name);
49
    }
50
51
    /**
52
     * @param iterable $options
53
     *
54
     * @return static
55
     */
56
    public function options(iterable $options)
57
    {
58
        return $this->addChildren($options, function ($text, $value) {
59
            return Option::create()
60
                ->value($value)
61
                ->text($text)
62
                ->selectedIf($value === $this->value);
63
        });
64
    }
65
66
    public function placeholder(string $text)
67
    {
68
        return $this->prependChild(
69
            Option::create()
70
                ->value('')
71
                ->text($text)
72
                ->selectedIf(! $this->hasSelection())
73
        );
74
    }
75
76
    /**
77
     * @param string|iterable $value
78
     *
79
     * @return static
80
     */
81
    public function value($value = null)
82
    {
83
        $element = clone $this;
84
85
        $element->value = $value;
86
87
        $element->applyValueToOptions();
88
89
        return $element;
90
    }
91
92
    protected function hasSelection(): bool
93
    {
94
        return $this->children->contains->hasAttribute('selected');
95
    }
96
97
    protected function applyValueToOptions()
98
    {
99
        $value = Collection::make($this->value);
100
101
        if (! $this->hasAttribute('multiple')) {
102
            $value = $value->take(1);
103
        }
104
105
        $this->children = $this->children->map(function ($child) use ($value) {
106
            if ($child instanceof Selectable) {
107
                return $child->selectedIf($value->contains($child->getAttribute('value')));
108
            }
109
110
            return $child;
111
        });
112
    }
113
}
114