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

FormElementSelect   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
ccs 0
cts 22
cp 0
wmc 5
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getHTML() 0 25 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
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
38
     */
39
    public function getHTML()
40
    {
41
        $details = $this->getHTMLDetails();
42
        extract($details);
43
        
44
        $options = null;
45
        foreach ($this['options'] as $optValue => $optText) {
46
            $options .= "<option value='{$optValue}'"
47
                . (($this['value'] == $optValue)
48
                    ? " selected=\"selected\""
49
                    : null)
50
                . ">{$optText}</option>\n";
51
        }
52
53
        return <<<EOD
54
<p>
55
<label for='$id'>$label</label>$brAfterLabel
56
<select id='$id'{$class}{$name}{$autofocus}{$required}{$readonly}{$checked}{$title}>
57
{$options}
58
</select>
59
{$messages}
60
</p>
61
<p class='cf-desc'>{$description}</p>
62
EOD;
63
    }
64
}
65