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

FormElementCheckboxMultiple   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
ccs 0
cts 28
cp 0
wmc 7
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A value() 0 4 1
A getHTML() 0 34 4
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Form element
7
 */
8
class FormElementCheckboxMultiple 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
        $this['type'] = 'checkbox-multiple';
21
        
22
        if (!isset($this['values'])) {
23
            throw new Exception("Missing values for the checkbox.");
24
        }
25
    }
26
27
28
29
    /**
30
     * Get the value of the form element.
31
     *
32
     * @return array the checked values of the form element.
33
     */
34
    public function value()
35
    {
36
        return $this['checked'];
37
    }
38
39
40
41
    /**
42
     * Get HTML code for a element.
43
     *
44
     * @return string HTML code for the element.
45
     *
46
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
47
     */
48
    public function getHTML()
49
    {
50
        $details = $this->getHTMLDetails();
51
        extract($details);
52
53
        $type = "type='checkbox'";
54
        $name = " name='{$this['name']}[]'";
55
        $ret = null;
56
        $checkedValues = is_array($this['checked']) ? $this['checked'] : [];
57
58
        foreach ($this['values'] as $val) {
59
            $id .= $val;
60
            $item = $onlyValue  = htmlentities($val, ENT_QUOTES, $this->characterEncoding);
61
            $value = " value='{$onlyValue}'";
62
            $checked = in_array($val, $checkedValues)
63
                ? " checked='checked'"
64
                : null;
65
66
            $ret .= <<<EOD
67
<p>
68
<input id='$id'{$type}{$class}{$name}{$value}{$autofocus}{$readonly}{$checked}{$title} />
69
<label for='$id'>$item</label>
70
{$messages}
71
</p>
72
EOD;
73
        }
74
        return <<<EOD
75
<div>
76
<p>{$label}</p>
77
{$ret}
78
<p class='cf-desc'>{$description}</p>
79
</div>
80
EOD;
81
    }
82
}
83