forked from zendframework/zendframework
/
ElementAnnotationsListener.php
377 lines (338 loc) · 11.7 KB
/
ElementAnnotationsListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Form\Annotation;
use Zend\EventManager\EventManagerInterface;
use Zend\Stdlib\ArrayObject;
/**
* Default listeners for element annotations
*
* Defines and attaches a set of default listeners for element annotations
* (which are defined on object properties). These include:
*
* - AllowEmpty
* - Attributes
* - ErrorMessage
* - Filter
* - Flags
* - Input
* - Hydrator
* - Object
* - Required
* - Type
* - Validator
*
* See the individual annotation classes for more details. The handlers registered
* work with the annotation values, as well as the element and input specification
* passed in the event object.
*/
class ElementAnnotationsListener extends AbstractAnnotationsListener
{
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach('configureElement', array($this, 'handleAllowEmptyAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleAttributesAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleComposedObjectAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleErrorMessageAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleFilterAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleFlagsAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleHydratorAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleInputAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleObjectAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleOptionsAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleRequiredAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleTypeAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleValidatorAnnotation'));
$this->listeners[] = $events->attach('discoverName', array($this, 'handleNameAnnotation'));
$this->listeners[] = $events->attach('discoverName', array($this, 'discoverFallbackName'));
$this->listeners[] = $events->attach('checkForExclude', array($this, 'handleExcludeAnnotation'));
}
/**
* Handle the AllowEmpty annotation
*
* Sets the allow_empty flag on the input specification array.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleAllowEmptyAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof AllowEmpty) {
return;
}
$inputSpec = $e->getParam('inputSpec');
$inputSpec['allow_empty'] = true;
}
/**
* Handle the Attributes annotation
*
* Sets the attributes array of the element specification.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleAttributesAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Attributes) {
return;
}
$elementSpec = $e->getParam('elementSpec');
if (isset($elementSpec['spec']['attributes'])) {
$elementSpec['spec']['attributes'] = array_merge($elementSpec['spec']['attributes'], $annotation->getAttributes());
return;
}
$elementSpec['spec']['attributes'] = $annotation->getAttributes();
}
/**
* Allow creating fieldsets from composed entity properties
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleComposedObjectAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof ComposedObject) {
return;
}
$class = $annotation->getComposedObject();
$annotationManager = $e->getTarget();
$specification = $annotationManager->getFormSpecification($class);
$name = $e->getParam('name');
$elementSpec = $e->getParam('elementSpec');
$filterSpec = $e->getParam('filterSpec');
// Compose input filter into parent input filter
$inputFilter = $specification['input_filter'];
if (!isset($inputFilter['type'])) {
$inputFilter['type'] = 'Zend\InputFilter\InputFilter';
}
$e->setParam('inputSpec', $inputFilter);
unset($specification['input_filter']);
// Compose specification as a fieldset into parent form/fieldset
if (!isset($specification['type'])) {
$specification['type'] = 'Zend\Form\Fieldset';
}
if ($annotation->isCollection()) {
$elementSpec['spec']['type'] = 'Zend\Form\Element\Collection';
$elementSpec['spec']['name'] = $name;
$elementSpec['spec']['options'] = new ArrayObject($annotation->getOptions());
$elementSpec['spec']['options']['target_element'] = $specification;
if (isset($specification['hydrator'])) {
$elementSpec['spec']['hydrator'] = $specification['hydrator'];
}
} else {
$elementSpec['spec'] = $specification;
$elementSpec['spec']['name'] = $name;
}
}
/**
* Handle the ErrorMessage annotation
*
* Sets the error_message of the input specification.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleErrorMessageAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof ErrorMessage) {
return;
}
$inputSpec = $e->getParam('inputSpec');
$inputSpec['error_message'] = $annotation->getMessage();
}
/**
* Determine if the element has been marked to exclude from the definition
*
* @param \Zend\EventManager\EventInterface $e
* @return bool
*/
public function handleExcludeAnnotation($e)
{
$annotations = $e->getParam('annotations');
if ($annotations->hasAnnotation('Zend\Form\Annotation\Exclude')) {
return true;
}
return false;
}
/**
* Handle the Filter annotation
*
* Adds a filter to the filter chain specification for the input.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleFilterAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Filter) {
return;
}
$inputSpec = $e->getParam('inputSpec');
if (!isset($inputSpec['filters'])) {
$inputSpec['filters'] = array();
}
$inputSpec['filters'][] = $annotation->getFilter();
}
/**
* Handle the Flags annotation
*
* Sets the element flags in the specification (used typically for setting
* priority).
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleFlagsAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Flags) {
return;
}
$elementSpec = $e->getParam('elementSpec');
$elementSpec['flags'] = $annotation->getFlags();
}
/**
* Handle the Hydrator annotation
*
* Sets the hydrator class to use in the fieldset specification.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleHydratorAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Hydrator) {
return;
}
$elementSpec = $e->getParam('elementSpec');
$elementSpec['spec']['hydrator'] = $annotation->getHydrator();
}
/**
* Handle the Input annotation
*
* Sets the filter specification for the current element to the specified
* input class name.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleInputAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Input) {
return;
}
$inputSpec = $e->getParam('inputSpec');
$inputSpec['type'] = $annotation->getInput();
}
/**
* Handle the Object annotation
*
* Sets the object to bind to the form or fieldset
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleObjectAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Object) {
return;
}
$elementSpec = $e->getParam('elementSpec');
$elementSpec['spec']['object'] = $annotation->getObject();
}
/**
* Handle the Options annotation
*
* Sets the element options in the specification.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleOptionsAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Options) {
return;
}
$elementSpec = $e->getParam('elementSpec');
$elementSpec['spec']['options'] = $annotation->getOptions();
}
/**
* Handle the Required annotation
*
* Sets the required flag on the input based on the annotation value.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleRequiredAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Required) {
return;
}
$required = (bool) $annotation->getRequired();
$inputSpec = $e->getParam('inputSpec');
$inputSpec['required'] = $required;
if ($required) {
$elementSpec = $e->getParam('elementSpec');
if (!isset($elementSpec['spec']['attributes'])) {
$elementSpec['spec']['attributes'] = array();
}
$elementSpec['spec']['attributes']['required'] = 'required';
}
}
/**
* Handle the Type annotation
*
* Sets the element class type to use in the element specification.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleTypeAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Type) {
return;
}
$elementSpec = $e->getParam('elementSpec');
$elementSpec['spec']['type'] = $annotation->getType();
}
/**
* Handle the Validator annotation
*
* Adds a validator to the validator chain of the input specification.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleValidatorAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Validator) {
return;
}
$inputSpec = $e->getParam('inputSpec');
if (!isset($inputSpec['validators'])) {
$inputSpec['validators'] = array();
}
$inputSpec['validators'][] = $annotation->getValidator();
}
}