Completed
Push — master ( f4804e...294476 )
by Mikael
03:28
created

FormElementSelect::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 2
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Form element
7
 */
8
class FormElementSelect extends FormElement
9
{
10
11
    /**
12
     * Constructor
13
     *
14
     * @param string $name       of the element.
15
     * @param array  $attributes to set to the element. Default is an empty array.
16
     *
17
     * @throws Exception if missing <options>
18
     */
19 8
    public function __construct($name, $attributes = [])
20
    {
21 8
        parent::__construct($name, $attributes);
22 8
        $this['type'] = 'select';
23 8
        $this->UseNameAsDefaultLabel();
24
        
25 8
        if (!is_array($this['options'])) {
26 2
            throw new Exception("Select needs options, did you forget to specify them when creating the element?");
27
        }
28 6
    }
29
30
31
32
    /**
33
     * Get HTML code for a element.
34
     *
35
     * @return string HTML code for the element.
36
     *
37
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
38
     */
39 2
    public function getHTML()
40
    {
41 2
        $details = $this->getHTMLDetails();
42 2
        extract($details);
43
        
44 2
        $options = null;
45 2
        foreach ($this['options'] as $optValue => $optText) {
46 1
            $options .= "<option value='{$optValue}'"
47 1
                . (($this['value'] == $optValue)
48
                    ? " selected"
49 1
                    : null)
50 1
                . ">{$optText}</option>\n";
51
        }
52
53
        return <<<EOD
54
<p>
55 2
<label for='$id'>$label</label>$brAfterLabel
56 2
<select id='$id'{$class}{$name}{$autofocus}{$required}{$readonly}{$checked}{$title}>
57 2
{$options}
58
</select>
59 2
{$messages}
60
</p>
61 2
<p class='cf-desc'>{$description}</p>
62
EOD;
63
    }
64
}
65