|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/form package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Form\Element; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Form\ElementInterface; |
|
13
|
|
|
use Slick\Form\Renderer\Div; |
|
14
|
|
|
use Slick\Form\Renderer\RendererInterface; |
|
15
|
|
|
use Slick\Form\ValueAwareInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Abstract Element: base element interface implementations |
|
19
|
|
|
* |
|
20
|
|
|
* @package Slick\Form\Element |
|
21
|
|
|
* @author Filipe Silva <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractElement implements ElementInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $settings = [ |
|
30
|
|
|
'multiple' => false |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add attribute manipulation methods |
|
35
|
|
|
*/ |
|
36
|
|
|
use AttributesAwareMethods; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add render capabilities to element |
|
40
|
|
|
*/ |
|
41
|
|
|
use RenderAwareMethods; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string|mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $value; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var null|string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $name = null; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string Renderer class |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $rendererClass = Div::class; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var RendererInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $renderer; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var bool |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $rendering = false; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets the element value |
|
70
|
|
|
* |
|
71
|
|
|
* This value here is just a string and it can be the content that |
|
72
|
|
|
* goes inside <label> tags for example. |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
32 |
|
public function getValue() |
|
77
|
|
|
{ |
|
78
|
32 |
|
return $this->value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Sets the value or content of the element |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed $value |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
80 |
|
public function setValue($value) |
|
89
|
|
|
{ |
|
90
|
80 |
|
$this->value = $value; |
|
91
|
80 |
|
if ($value instanceof ValueAwareInterface) { |
|
92
|
|
|
$this->value = $value->getFormValue(); |
|
93
|
|
|
} |
|
94
|
80 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns element name |
|
99
|
|
|
* |
|
100
|
|
|
* @return null|string |
|
101
|
|
|
*/ |
|
102
|
36 |
|
public function getName() |
|
103
|
|
|
{ |
|
104
|
36 |
|
return $this->name; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Sets element name |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* |
|
112
|
|
|
* @return $this|self|AbstractElement |
|
113
|
|
|
*/ |
|
114
|
34 |
|
public function setName($name) |
|
115
|
|
|
{ |
|
116
|
34 |
|
$this->name = $name; |
|
117
|
34 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Gets the HTML renderer for this element |
|
122
|
|
|
* |
|
123
|
|
|
* @return RendererInterface |
|
124
|
|
|
*/ |
|
125
|
20 |
|
protected function getRenderer() |
|
126
|
|
|
{ |
|
127
|
20 |
|
if (null === $this->renderer) { |
|
128
|
20 |
|
$this->setRenderer(new $this->rendererClass()); |
|
129
|
20 |
|
} |
|
130
|
20 |
|
return $this->renderer; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Sets internal renderer |
|
135
|
|
|
* |
|
136
|
|
|
* @param RendererInterface $renderer |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this|self|AbstractElement |
|
139
|
|
|
*/ |
|
140
|
20 |
|
protected function setRenderer(RendererInterface $renderer) |
|
141
|
|
|
{ |
|
142
|
20 |
|
$this->renderer = $renderer; |
|
143
|
20 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set other input settings |
|
148
|
|
|
* |
|
149
|
|
|
* @param array $settings |
|
150
|
|
|
* @return self|AbstractElement |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setSettings(array $settings) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->settings = array_merge($this->settings, $settings); |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Set rendering state |
|
160
|
|
|
* |
|
161
|
|
|
* @param bool $state |
|
162
|
|
|
* |
|
163
|
|
|
* @return self|$this|AbstractElement |
|
164
|
|
|
*/ |
|
165
|
20 |
|
public function setRendering($state) |
|
166
|
|
|
{ |
|
167
|
20 |
|
$this->rendering = $state; |
|
168
|
20 |
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Check if input is being rendered |
|
173
|
|
|
* |
|
174
|
|
|
* @return boolean |
|
175
|
|
|
*/ |
|
176
|
|
|
public function isRendering() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->rendering; |
|
179
|
|
|
} |
|
180
|
|
|
} |