Completed
Push — master ( ed2b3e...68d467 )
by Mikael
02:34
created

FormElementCheckbox::getHTML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
ccs 0
cts 8
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Form element
7
 */
8
class FormElementCheckbox 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
    public function __construct($name, $attributes = [])
18
    {
19
        parent::__construct($name, $attributes);
20
21
        $this['type'] = 'checkbox';
22
        
23
        $this['checked'] = isset($attributes['checked'])
24
            ? $attributes['checked']
25
            : false;
26
            
27
        $this['value'] = isset($attributes['value'])
28
            ? $attributes['value']
29
            : $name;
30
            
31
        $this->UseNameAsDefaultLabel(null);
32
    }
33
34
35
36
    /**
37
     * Get the value of the form element.
38
     *
39
     * @return array the checked values of the form element.
40
     */
41
    public function value()
42
    {
43
        return $this['checked'];
44
    }
45
46
47
48
    /**
49
     * Get HTML code for a element.
50
     *
51
     * @return string HTML code for the element.
52
     */
53
    public function getHTML()
54
    {
55
        $details = $this->getHTMLDetails();
56
        extract($details);
57
58
        return <<<EOD
59
<p>
60
<input id='$id'{$type}{$class}{$name}{$value}{$autofocus}{$required}{$readonly}{$checked}{$title} />
61
<label for='$id'>$label</label>
62
{$messages}
63
</p>
64
<p class='cf-desc'>{$description}</p>
65
EOD;
66
    }
67
}
68