Completed
Push — master ( 25a367...4e273e )
by Adrian
03:50
created

Element::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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