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
|
|
|
*/ |
42
|
|
|
abstract class Element extends Specs |
43
|
|
|
{ |
44
|
|
|
use HasAttributesTrait; |
45
|
|
|
use HasDataTrait; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Name of the field (identifier of the element in the form's child list) |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $name; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @param string $name |
57
|
|
|
* Name of the form element that will make it identifiable |
58
|
|
|
* @param array $specs |
59
|
|
|
* Specification for the element (attributes, parents, etc) |
60
|
|
|
*/ |
61
|
48 |
|
function __construct($name, $specs = array()) |
|
|
|
|
62
|
|
|
{ |
63
|
48 |
|
$specs = array_merge($this->getDefaultSpecs(), $specs); |
64
|
48 |
|
foreach ($specs as $key => $value) { |
65
|
40 |
|
$this->set($key, $value); |
66
|
48 |
|
} |
67
|
48 |
|
$this->name = $name; |
68
|
48 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the default element specifications |
72
|
|
|
* To be used for easily extending objects |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
8 |
|
protected function getDefaultSpecs() |
77
|
|
|
{ |
78
|
8 |
|
return array(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Retrieve the name of the form's element as registered within the form |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
14 |
|
function getName() |
|
|
|
|
87
|
|
|
{ |
88
|
14 |
|
return $this->name; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sets the group this element belongs to |
93
|
|
|
* |
94
|
|
|
* @param null|string $group |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
1 |
|
function setGroup($group = null) |
|
|
|
|
99
|
|
|
{ |
100
|
1 |
|
$this[Element::GROUP] = (string) $group; |
101
|
|
|
|
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieve the group this element belongs to |
107
|
|
|
* |
108
|
|
|
* @return null|string |
109
|
|
|
*/ |
110
|
9 |
|
function getGroup() |
|
|
|
|
111
|
|
|
{ |
112
|
9 |
|
return isset($this[Element::GROUP]) ? $this[Element::GROUP] : null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sets the widget for this element |
117
|
|
|
* |
118
|
|
|
* @param null|string $widget |
119
|
|
|
* |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
40 |
|
function setWidget($widget = null) |
|
|
|
|
123
|
|
|
{ |
124
|
40 |
|
$this[Element::WIDGET] = (string) $widget; |
125
|
|
|
|
126
|
40 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieve the widget type for this element |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
function getWidget() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
return isset($this[Element::WIDGET]) ? $this[Element::WIDGET] : null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Sets the display priority for this element |
141
|
|
|
* |
142
|
|
|
* @param integer $priority |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
7 |
|
function setPosition($priority = 0) |
|
|
|
|
147
|
|
|
{ |
148
|
7 |
|
$this[Element::POSITION] = (int) $priority; |
149
|
|
|
|
150
|
7 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Retrieve display position for this element |
155
|
|
|
* |
156
|
|
|
* @return integer |
157
|
|
|
*/ |
158
|
|
|
function getPosition() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
return isset($this[Element::POSITION]) ? $this[Element::POSITION] : 0; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Prepares the input filter to receive data and be rendered |
166
|
|
|
* It attaches the filters, validation rules, upload handler for the element |
167
|
|
|
* |
168
|
|
|
* @param InputFilter $inputFilter |
169
|
|
|
*/ |
170
|
9 |
|
function prepareInputFilter(InputFilter $inputFilter) |
|
|
|
|
171
|
|
|
{ |
172
|
9 |
|
$preparableMethods = array('prepareValidator', 'prepareFiltrator', 'prepareUploadHandlers'); |
173
|
9 |
|
foreach ($preparableMethods as $method) { |
174
|
9 |
|
if (method_exists($this, $method)) { |
175
|
9 |
|
call_user_func(array( $this, $method), $inputFilter); |
176
|
9 |
|
} |
177
|
9 |
|
} |
178
|
9 |
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.