Completed
Push — master ( 16f142...8b38a2 )
by Mikael
03:34
created

FormElementSelectMultiple   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
ccs 0
cts 22
cp 0
wmc 6
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A value() 0 4 1
A getHTML() 0 27 4
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Form element
7
 */
8
class FormElementSelectMultiple extends FormElementSelect
9
{
10
    /**
11
     * Constructor
12
     *
13
     * @param string $name       of the element.
14
     * @param array  $attributes to set to the element. Default is an
15
     *                           empty array.
16
     */
17
    public function __construct($name, $attributes = [])
18
    {
19
        parent::__construct($name, $attributes);
20
        $this['type'] = 'select-multiple';
21
    }
22
23
24
25
    /**
26
     * Get the value of the form element.
27
     *
28
     * @return array the checked values of the form element.
29
     */
30
    public function value()
31
    {
32
        return $this['checked'];
33
    }
34
35
36
37
    /**
38
     * Get HTML code for a element.
39
     *
40
     * @return string HTML code for the element.
41
     *
42
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
43
     */
44
    public function getHTML()
45
    {
46
        $details = $this->getHTMLDetails();
47
        extract($details);
48
        
49
        $name = " name='{$this['name']}[]'";
50
        $options = null;
51
        $selectedValues = is_array($this['checked']) ? $this['checked'] : [];
52
        
53
        foreach ($this['options'] as $optValue => $optText) {
54
            $selected = in_array($optValue, $selectedValues)
55
                ? " selected"
56
                : null;
57
            $options .= "<option value='{$optValue}'{$selected}>{$optText}</option>\n";
58
        }
59
60
        return <<<EOD
61
<p>
62
<label for='$id'>$label</label>$brAfterLabel
63
<select id='$id'{$size}{$class}{$name}{$autofocus}{$required}{$readonly}{$title} multiple>
64
{$options}
65
</select>
66
{$messages}
67
</p>
68
<p class='cf-desc'>{$description}</p>
69
EOD;
70
    }
71
}
72