Completed
Pull Request — 2.x (#48)
by
unknown
02:06
created

Checkbox::prep()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Html\Helper\Input;
10
11
/**
12
 *
13
 * An HTML checkbox input.
14
 *
15
 * @package Aura.Html
16
 *
17
 */
18
class Checkbox extends AbstractChecked
19
{
20
21
    /**
22
     *
23
     * Individual attributes for each option (only for multi checkbox)
24
     *
25
     * @var array
26
     *
27
     */
28
    protected $options_attribs = array();
29
30
    /**
31
     *
32
     * Prepares the properties on this helper.
33
     *
34
     * @param array $spec The specification array.
35
     *
36
     */
37 12
    protected function prep(array $spec)
38
    {
39 12
        if (isset($spec['options_attribs'])) {
40 1
            $this->options_attribs = $spec['options_attribs'];
41 1
            unset($spec['options_attribs']);
42 1
        }
43
44 12
        parent::prep($spec);
45 12
    }
46
47
    /**
48
     *
49
     * Returns the HTML for the input.
50
     *
51
     * @return string
52
     *
53
     */
54 12
    public function __toString()
55
    {
56 12
        $this->attribs['type'] = 'checkbox';
57
58 12
        if ($this->options) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
59 3
            return $this->multiple();
60
        }
61
62
        // Get unchecked element first. This unsets value_unchecked
63 12
        $unchecked = $this->htmlUnchecked();
64
65
        // Get the input
66 12
        $input = $this->htmlChecked();
67
68
        // Unchecked (hidden) element must reside outside the label
69 12
        $html  = $unchecked . $this->htmlLabel($input);
70
71 12
        return $this->indent(0, $html);
72
    }
73
74
    /**
75
     *
76
     * Returns the HTML for the "unchecked" part of the input.
77
     *
78
     * @return string
79
     *
80
     */
81 12
    protected function htmlUnchecked()
82
    {
83 12
        if (! isset($this->attribs['value_unchecked'])) {
84 10
            return;
85
        }
86
87 2
        $unchecked = $this->attribs['value_unchecked'];
88 2
        unset($this->attribs['value_unchecked']);
89
90
        $attribs = array(
91 2
            'type' => 'hidden',
92 2
            'value' => $unchecked,
93 2
            'name' => $this->name
94 2
        );
95
96 2
        return $this->void('input', $attribs);
97
    }
98
99 3
    protected function multiple()
100
    {
101 3
        $html = '';
102 3
        $checkbox = clone($this);
103
104 3
        $this->attribs['name'] .= '[]';
105
106 3
        foreach ($this->options as $value => $label) {
107 3
            $this->attribs['value'] = $value;
108 3
            $this->attribs['label'] = $label;
109
110 3
            $option_attribs = isset($this->options_attribs[$value]) ? $this->options_attribs[$value] : array();
111
112 3
            $html .= $checkbox(array(
113 3
                'name'    => $this->attribs['name'],
114 3
                'value'   => $this->value,
115 3
                'attribs' => $this->attribs + $option_attribs
116 3
            ));
117 3
        }
118 3
        return $html;
119
    }
120
}
121