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

FormElementSelect::getHTML()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 0
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
crap 3.0026
rs 8.8571
c 0
b 0
f 0
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