Completed
Push — master ( e5332e...a89400 )
by Adrian
02:09
created

Element::prepareInputFilter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
namespace Sirius\Input;
3
4
use Sirius\Input\Traits\HasAttributesTrait;
5
use Sirius\Input\Traits\HasDataTrait;
6
7
/**
8
 *
9
 * @method Element getLabel() Get the label text
10
 * @method Element setLabel($label) Set label text
11
 * @method Element getLabelAttributes() Get the attributes for the label
12
 * @method Element setLabelAttributes(array $attributes) Set label attributes
13
 * @method Element setLabelAttribute($attr, $value = null) Set/Unset label attribute
14
 * @method Element addLabelClass($class) Add a CSS class to the label
15
 * @method Element removeLabelClass($class) Removes a CSS class from the label
16
 * @method Element toggleLabelClass($class) Toggles a class on the label
17
 * @method Element getHint() Get the hint text
18
 * @method Element setHint($label) Set hint text
19
 * @method Element getHintAttributes() Get the attributes for the hint
20
 * @method Element setHintAttributes(array $attributes) Set hint attributes
21
 * @method Element setHintAttribute($attr, $value = null) Set/Unset hint attribute
22
 * @method Element addHintClass($class) Add a CSS class to the hint
23
 * @method Element removeHintClass($class) Removes a CSS class from the hint
24
 * @method Element toggleHintClass($class) Toggles a class on the hint
25
 * @method Element getContainerAttributes() Get the attributes for the container
26
 * @method Element setContainerAttributes(array $attributes) Set container attributes
27
 * @method Element setContainerAttribute($attr, $value = null) Set/Unset container attribute
28
 * @method Element addContainerClass($class) Add a CSS class to the container
29
 * @method Element removeContainerClass($class) Removes a CSS class from the container
30
 * @method Element toggleContainerClass($class) Toggles a class on the container
31
 * @method Element getValidationRules() Get list of validation rules
32
 * @method Element setValidationRules(array $rules) Set list of validation rules
33
 * @method Element getFilters() Get list of data filters
34
 * @method Element setFilters(array $filters) Set list of filters
35
 * @method Element getUploadContainer() Get the upload container for the element
36
 * @method Element setUploadContainer($container) Set the upload container for the element
37
 * @method Element getUploadOptions() Get the upload options for the container
38
 * @method Element setUploadOptions(array $options) Set the upload options for the container
39
 * @method Element getUploadRules() Get the upload validation rules
40
 * @method Element setUploadRules(array $rules) Set the upload validation rules
41
 * @method Element setChoices(array $choices) Set the list of choices for SELECT type elements
42
 * @method Element getChoices() Get the list of choices for SELECT type elements
43
 * @method Element getFirstChoice() Get the list of choices for SELECT type elements
44
 */
45
abstract class Element extends \ArrayObject
46
{
47
    use HasAttributesTrait;
48
    use HasDataTrait;
49
50
    /**
51
     * Name of the field (identifier of the element in the form's child list)
52
     *
53
     * @var string
54
     */
55
    protected $name;
56
57
    /**
58
     *
59
     * @param string $name
60
     *            Name of the form element that will make it identifiable
61
     * @param array $specs
62
     *            Specification for the element (attributes, parents, etc)
63
     */
64 51
    public function __construct($name, $specs = array())
65
    {
66 51
        $specs = array_merge($this->getDefaultSpecs(), $specs);
67 51
        foreach ($specs as $key => $value) {
68 42
            $this->set($key, $value);
69 51
        }
70 51
        $this->name = $name;
71 51
    }
72
73 44
    protected function inflectUnderscoreToClass($var)
74 1
    {
75 44
        $var = str_replace('_', ' ', $var);
76 44
        $var = ucwords($var);
77
78 44
        return str_replace(' ', '', $var);
79
    }
80
81 2
    public function get($key)
82
    {
83 2
        $method = 'get' . $this->inflectUnderscoreToClass($key);
84 2
        if (method_exists($this, $method)) {
85 1
            return $this->{$method}();
86
        }
87
88 1
        return isset($this[$key]) ? $this[$key] : null;
89
    }
90
91 44
    public function set($key, $value)
92
    {
93 44
        $method = 'set' . $this->inflectUnderscoreToClass($key);
94 44
        if (method_exists($this, $method)) {
95 43
            return $this->{$method}($value);
96
        }
97 2
        $this[$key] = $value;
98
99 2
        return $this;
100
    }
101
102
103
    /**
104
     * Returns the default element specifications
105
     * The default specs are merged with the constructor specs
106
     *
107
     * @return array
108
     */
109 9
    protected function getDefaultSpecs()
110
    {
111 9
        return array();
112
    }
113
114
    /**
115
     * Retrieve the name of the form's element as registered within the form
116
     *
117
     * @return string
118
     */
119 14
    public function getName()
120
    {
121 14
        return $this->name;
122
    }
123
124
    /**
125
     * Sets the group this element belongs to
126
     *
127
     * @param null|string $group
128
     *
129
     * @return $this
130
     */
131 1
    public function setGroup($group = null)
132
    {
133 1
        $this[Specs::GROUP] = (string) $group;
134
135 1
        return $this;
136
    }
137
138
    /**
139
     * Retrieve the group this element belongs to
140
     *
141
     * @return null|string
142
     */
143 9
    public function getGroup()
144
    {
145 9
        return isset($this[Specs::GROUP]) ? $this[Specs::GROUP] : null;
146
    }
147
148
    /**
149
     * Sets the widget for this element
150
     *
151
     * @param null|string $widget
152
     *
153
     * @return $this
154
     */
155 42
    public function setWidget($widget = null)
156
    {
157 42
        $this[Specs::WIDGET] = (string) $widget;
158
159 42
        return $this;
160
    }
161
162
    /**
163
     * Retrieve the widget type for this element
164
     *
165
     * @return string
166
     */
167 1
    public function getWidget()
168
    {
169 1
        return isset($this[Specs::WIDGET]) ? $this[Specs::WIDGET] : null;
170
    }
171
172
    /**
173
     * Sets the display priority for this element
174
     *
175
     * @param integer $priority
176
     *
177
     * @return $this
178
     */
179 8
    public function setPosition($priority = 0)
180
    {
181 8
        $this[Specs::POSITION] = (int) $priority;
182
183 8
        return $this;
184
    }
185
186
    /**
187
     * Retrieve display position for this element
188
     *
189
     * @return integer
190
     */
191 1
    public function getPosition()
192
    {
193 1
        return isset($this[Specs::POSITION]) ? $this[Specs::POSITION] : 0;
194
    }
195
196
197
    /**
198
     * Prepares the input filter to receive data and be rendered
199
     * It attaches the filters, validation rules, upload handler for the element
200
     *
201
     * @param InputFilter $inputFilter
202
     */
203 9
    public function prepareInputFilter(InputFilter $inputFilter)
204
    {
205 9
        $preparableMethods = array('prepareValidator', 'prepareFiltrator', 'prepareUploadHandlers');
206 9
        foreach ($preparableMethods as $method) {
207 9
            if (method_exists($this, $method)) {
208 9
                call_user_func(array($this, $method), $inputFilter);
209 9
            }
210 9
        }
211 9
    }
212
213
}
214