Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.23% covered (success)
96.23%
51 / 53
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SelectMultiple
96.23% covered (success)
96.23%
51 / 53
66.67% covered (warning)
66.67%
2 / 3
28
0.00% covered (danger)
0.00%
0 / 1
 __construct
93.10% covered (success)
93.10%
27 / 29
0.00% covered (danger)
0.00%
0 / 1
13.06
 setValue
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
9
 resetValue
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Form\Element;
15
16/**
17 * Form select multiple element class
18 *
19 * @category   Pop
20 * @package    Pop\Form
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.6.0
25 */
26
27class SelectMultiple extends AbstractSelect
28{
29
30    /**
31     * Constructor
32     *
33     * Instantiate the select form element object
34     *
35     * @param  string       $name
36     * @param  string|array $values
37     * @param  string|array $selected
38     * @param  string       $xmlFile
39     * @param  string       $indent
40     */
41    public function __construct($name, $values, $selected = null, $xmlFile = null, $indent = null)
42    {
43        parent::__construct('select');
44
45        $this->setName($name);
46        $this->setAttributes([
47            'name'     => $name . '[]',
48            'id'       => $name,
49            'multiple' => 'multiple'
50        ]);
51
52        if (null !== $indent) {
53            $this->setIndent($indent);
54        }
55
56        $values = self::parseValues($values, $xmlFile);
57
58        // Create the child option elements.
59        foreach ($values as $k => $v) {
60            if (is_array($v)) {
61                $optGroup = new Select\Optgroup();
62                if (null !== $indent) {
63                    $optGroup->setIndent($indent);
64                }
65                $optGroup->setAttribute('label', $k);
66                foreach ($v as $ky => $vl) {
67                    $option = new Select\Option($ky, $vl);
68                    if (null !== $indent) {
69                        $option->setIndent($indent);
70                    }
71
72                    // Determine if the current option element is selected.
73                    if (is_array($this->selected) && in_array($ky, $this->selected, true)) {
74                        $option->select();
75                    }
76                    $optGroup->addChild($option);
77                }
78                $this->addChild($optGroup);
79            } else {
80                $option = new Select\Option($k, $v);
81                if (null !== $indent) {
82                    $option->setIndent($indent);
83                }
84
85                // Determine if the current option element is selected.
86                if (is_array($this->selected) && in_array($k, $this->selected, true)) {
87                    $option->select();
88                }
89                $this->addChild($option);
90            }
91        }
92
93        if (null !== $selected) {
94            $this->setValue($selected);
95        } else {
96            $this->selected = [];
97        }
98    }
99
100    /**
101     * Set the selected value of the select form element
102     *
103     * @param  mixed $value
104     * @return SelectMultiple
105     */
106    public function setValue($value)
107    {
108        $this->selected = (!is_array($value)) ? [$value] : $value;
109
110        if ($this->hasChildren()) {
111            foreach ($this->childNodes as $child) {
112                if ($child instanceof Select\Option) {
113                    if (in_array($child->getValue(), $this->selected)) {
114                        $child->select();
115                    } else {
116                        $child->deselect();
117                    }
118                } else if ($child instanceof Select\Optgroup) {
119                    $options = $child->getOptions();
120                    foreach ($options as $option) {
121                        if (in_array($option->getValue(), $this->selected)) {
122                            $option->select();
123                        } else {
124                            $option->deselect();
125                        }
126                    }
127                }
128            }
129        }
130
131        return $this;
132    }
133
134    /**
135     * Reset the value of the form element
136     *
137     * @return SelectMultiple
138     */
139    public function resetValue()
140    {
141        $this->selected = [];
142
143        if ($this->hasChildren()) {
144            foreach ($this->childNodes as $child) {
145                if ($child instanceof Select\Option) {
146                    $child->deselect();
147                } else if ($child instanceof Select\Optgroup) {
148                    $options = $child->getOptions();
149                    foreach ($options as $option) {
150                        $option->deselect();
151                    }
152                }
153            }
154        }
155
156        return $this;
157    }
158
159}