Completed
Pull Request — 2.x (#43)
by jake
02:49 queued 50s
created

AbstractInput::prep()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1.0098

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 29
ccs 11
cts 14
cp 0.7856
rs 8.8571
cc 1
eloc 18
nc 1
nop 1
crap 1.0098
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
use Aura\Html\Helper\AbstractHelper;
12
use Aura\Html\Exception;
13
14
/**
15
 *
16
 * Abstact helper for inputs that can be checked (e.g. radio or checkbox).
17
 *
18
 * @package Aura.Html
19
 *
20
 */
21
abstract class AbstractInput extends AbstractHelper
22
{
23
    /**
24
     *
25
     * The input type.
26
     *
27
     * @var string
28
     *
29
     */
30
    protected $type;
31
32
    /**
33
     *
34
     * The input name.
35
     *
36
     * @var string
37
     *
38
     */
39
    protected $name;
40
41
    /**
42
     *
43
     * The current value of the input.
44
     *
45
     * @var mixed
46
     *
47
     */
48
    protected $value;
49
50
    /**
51
     *
52
     * HTML attributes for the input.
53
     *
54
     * @var array
55
     *
56
     */
57
    protected $attribs = array();
58
59
    /**
60
     *
61
     * Value options for the input.
62
     *
63
     * @var array
64
     *
65
     */
66
    protected $options = array();
67
68
    /**
69
     *
70
     * Given a input spec, returns the HTML for the input.
71
     *
72
     * @param array $spec The input spec.
73
     *
74
     * @return string
75
     *
76
     */
77 12
    public function __invoke(array $spec = null)
78
    {
79 12
        if ($spec !== null) {
80
            $this->prep($spec);
81
        }
82 12
        return $this;
83 12
    }
84
85
    /**
86
     *
87
     * Returns the HTML for this input.
88
     *
89
     * @return string
90
     *
91
     */
92
    abstract public function __toString();
93
94
    /**
95
     *
96
     * Prepares the properties on this helper.
97
     *
98
     * @param array $spec The specification array.
99
     *
100
     */
101 14
    protected function prep(array $spec)
102
    {
103
        // base spec inputs
104
        $base = array(
105
            'type' => null,
106
            'name' => null,
107
            'value' => null,
108
            'attribs' => array(),
109
            'options' => array(),
110 14
        );
111
112
        // make sure we have the base spec inputs
113
        $spec = array_merge($base, $spec);
114
115
        // retain as properties
116 14
        $this->type    = $spec['type'];
117 14
        $this->name    = $spec['name'];
118 14
        $this->value   = $spec['value'];
119 14
        $this->attribs = $spec['attribs'];
120 14
        $this->options = $spec['options'];
121
122
        // set up base attributes
123
        $attribs = array(
124 14
            'id'   => null,
125 14
            'type' => null,
126 14
            'name' => $this->name,
127 14
        );
128
        $this->attribs = array_merge($attribs, $this->attribs);
129
    }
130
}
131