Test Failed
Push — master ( 194522...eda1b6 )
by Mikael
01:43
created

FormElementSelect   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
B getHTML() 0 26 3
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
    public function __construct($name, $attributes = [])
20
    {
21
        parent::__construct($name, $attributes);
22
        $this['type'] = 'select';
23
        $this->UseNameAsDefaultLabel();
24
        
25
        if (!is_array($this['options'])) {
26
            throw new Exception("Select needs options, did you forget to specify them when creating the element?");
27
        }
28
    }
29
30
31
32
    /**
33
     * Get HTML code for a element.
34
     *
35
     * @return string HTML code for the element.
36
     */
37
    public function getHTML()
38
    {
39
        $details = $this->getHTMLDetails();
40
        extract($details);
41
        
42
        $options = null;
43
        foreach ($this['options'] as $optValue => $optText) {
44
            $options .= "<option value='{$optValue}'"
45
                . (($this['value'] == $optValue)
46
                    ? " selected"
47
                    : null)
48
                . ">{$optText}</option>\n";
49
        }
50
51
        return <<<EOD
52
<p>
53
<label for='$id'>$label</label>
54
<br/>
55
<select id='$id'{$class}{$name}{$autofocus}{$required}{$readonly}{$checked}{$title}>
56
{$options}
57
</select>
58
{$messages}
59
</p>
60
<p class='cf-desc'>{$description}</p>
61
EOD;
62
    }
63
}
64