This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
Collection.php
533 lines (463 loc) · 14.2 KB
/
Collection.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?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\Element;
use Traversable;
use Zend\Form\Element;
use Zend\Form\ElementInterface;
use Zend\Form\Exception;
use Zend\Form\Fieldset;
use Zend\Form\FieldsetInterface;
use Zend\Form\FieldsetPrepareAwareInterface;
use Zend\Form\FormInterface;
use Zend\Stdlib\ArrayUtils;
class Collection extends Fieldset implements FieldsetPrepareAwareInterface
{
/**
* Default template placeholder
*/
const DEFAULT_TEMPLATE_PLACEHOLDER = '__index__';
/**
* Element used in the collection
*
* @var ElementInterface
*/
protected $targetElement;
/**
* Initial count of target element
*
* @var int
*/
protected $count = 1;
/**
* Are new elements allowed to be added dynamically ?
*
* @var bool
*/
protected $allowAdd = true;
/**
* Are existing elements allowed to be removed dynamically ?
*
* @var bool
*/
protected $allowRemove = true;
/**
* Is the template generated ?
*
* @var bool
*/
protected $shouldCreateTemplate = false;
/**
* Placeholder used in template content for making your life easier with JavaScript
*
* @var string
*/
protected $templatePlaceholder = self::DEFAULT_TEMPLATE_PLACEHOLDER;
/**
* Element used as a template
*
* @var ElementInterface|FieldsetInterface
*/
protected $templateElement;
/**
* Accepted options for Collection:
* - target_element: an array or element used in the collection
* - count: number of times the element is added initially
* - allow_add: if set to true, elements can be added to the form dynamically (using JavaScript)
* - allow_remove: if set to true, elements can be removed to the form
* - should_create_template: if set to true, a template is generated (inside a <span>)
* - template_placeholder: placeholder used in the data template
*
* @param array|Traversable $options
* @return Collection
*/
public function setOptions($options)
{
parent::setOptions($options);
if (isset($options['target_element'])) {
$this->setTargetElement($options['target_element']);
}
if (isset($options['count'])) {
$this->setCount($options['count']);
}
if (isset($options['allow_add'])) {
$this->setAllowAdd($options['allow_add']);
}
if (isset($options['allow_remove'])) {
$this->setAllowRemove($options['allow_remove']);
}
if (isset($options['should_create_template'])) {
$this->setShouldCreateTemplate($options['should_create_template']);
}
if (isset($options['template_placeholder'])) {
$this->setTemplatePlaceholder($options['template_placeholder']);
}
return $this;
}
/**
* Checks if the object can be set in this fieldset
*
* @param object $object
* @return bool
*/
public function allowObjectBinding($object)
{
return true;
}
/**
* Set the object used by the hydrator
* In this case the "object" is a collection of objects
*
* @param array|Traversable $object
* @return Fieldset|FieldsetInterface
* @throws Exception\InvalidArgumentException
*/
public function setObject($object)
{
if (!is_array($object) && !$object instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable object argument; received "%s"',
__METHOD__,
(is_object($object) ? get_class($object) : gettype($object))
));
}
$this->object = $object;
$this->count = count($object);
return $this;
}
/**
* Populate values
*
* @param array|Traversable $data
* @throws \Zend\Form\Exception\InvalidArgumentException
* @throws \Zend\Form\Exception\DomainException
* @return void
*/
public function populateValues($data)
{
if (!is_array($data) && !$data instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable set of data; received "%s"',
__METHOD__,
(is_object($data) ? get_class($data) : gettype($data))
));
}
// Can't do anything with empty data
if (empty($data)) {
return;
}
if (count($data) < $this->getCount()) {
if (!$this->allowRemove) {
throw new Exception\DomainException(sprintf(
'There are fewer elements than specified in the collection (%s). Either set the allow_remove option ' .
'to true, or re-submit the form.',
get_class($this)
)
);
}
// If there are less data and that allowRemove is true, we remove elements that are not presents
$this->setCount(count($data));
foreach ($this->byName as $name => $elementOrFieldset) {
if (isset($data[$name])) {
continue;
}
$this->remove($name);
}
}
if ($this->targetElement instanceof FieldsetInterface) {
foreach ($this->byName as $name => $fieldset) {
if (isset($data[$name])) {
$fieldset->populateValues($data[$name]);
unset($data[$name]);
}
}
} else {
foreach ($this->byName as $name => $element) {
$element->setAttribute('value', $data[$name]);
unset($data[$name]);
}
}
// If there are still data, this means that elements or fieldsets were dynamically added. If allowed by the user, add them
if (!empty($data) && $this->allowAdd) {
foreach ($data as $key => $value) {
$elementOrFieldset = $this->createNewTargetElementInstance();
$elementOrFieldset->setName($key);
if ($elementOrFieldset instanceof FieldsetInterface) {
$elementOrFieldset->populateValues($value);
} else {
$elementOrFieldset->setAttribute('value', $value);
}
$this->add($elementOrFieldset);
}
} elseif (!empty($data) && !$this->allowAdd) {
throw new Exception\DomainException(sprintf(
'There are more elements than specified in the collection (%s). Either set the allow_add option ' .
'to true, or re-submit the form.',
get_class($this)
)
);
}
}
/**
* Checks if this fieldset can bind data
*
* @return bool
*/
public function allowValueBinding()
{
return true;
}
/**
* Bind values to the object
*
* @param array $values
* @return array|mixed|void
*/
public function bindValues(array $values = array())
{
$collection = array();
foreach ($values as $name => $value) {
$element = $this->get($name);
if ($element instanceof FieldsetInterface) {
$collection[] = $element->bindValues($value);
} else {
$collection[] = $value;
}
}
return $collection;
}
/**
* Set the initial count of target element
*
* @param $count
* @return Collection
*/
public function setCount($count)
{
$this->count = $count > 0 ? $count : 0;
return $this;
}
/**
* Get the initial count of target element
*
* @return int
*/
public function getCount()
{
return $this->count;
}
/**
* Set the target element
*
* @param ElementInterface|array|Traversable $elementOrFieldset
* @return Collection
* @throws \Zend\Form\Exception\InvalidArgumentException
*/
public function setTargetElement($elementOrFieldset)
{
if (is_array($elementOrFieldset)
|| ($elementOrFieldset instanceof Traversable && !$elementOrFieldset instanceof ElementInterface)
) {
$factory = $this->getFormFactory();
$elementOrFieldset = $factory->create($elementOrFieldset);
}
if (!$elementOrFieldset instanceof ElementInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'%s requires that $elementOrFieldset be an object implementing %s; received "%s"',
__METHOD__,
__NAMESPACE__ . '\ElementInterface',
(is_object($elementOrFieldset) ? get_class($elementOrFieldset) : gettype($elementOrFieldset))
));
}
$this->targetElement = $elementOrFieldset;
return $this;
}
/**
* Get target element
*
* @return ElementInterface|null
*/
public function getTargetElement()
{
return $this->targetElement;
}
/**
* Get allow add
*
* @param bool $allowAdd
* @return Collection
*/
public function setAllowAdd($allowAdd)
{
$this->allowAdd = (bool) $allowAdd;
return $this;
}
/**
* Get allow add
*
* @return bool
*/
public function allowAdd()
{
return $this->allowAdd;
}
/**
* @param bool $allowRemove
* @return Collection
*/
public function setAllowRemove($allowRemove)
{
$this->allowRemove = (bool) $allowRemove;
return $this;
}
/**
* @return bool
*/
public function allowRemove()
{
return $this->allowRemove;
}
/**
* If set to true, a template prototype is automatically added to the form to ease the creation of dynamic elements through JavaScript
*
* @param bool $shouldCreateTemplate
* @return Collection
*/
public function setShouldCreateTemplate($shouldCreateTemplate)
{
$this->shouldCreateTemplate = (bool) $shouldCreateTemplate;
return $this;
}
/**
* Get if the collection should create a template
*
* @return bool
*/
public function shouldCreateTemplate()
{
return $this->shouldCreateTemplate;
}
/**
* Set the placeholder used in the template generated to help create new elements in JavaScript
*
* @param string $templatePlaceholder
* @return Collection
*/
public function setTemplatePlaceholder($templatePlaceholder)
{
if (is_string($templatePlaceholder)) {
$this->templatePlaceholder = $templatePlaceholder;
}
return $this;
}
/**
* Get the template placeholder
*
* @return string
*/
public function getTemplatePlaceholder()
{
return $this->templatePlaceholder;
}
/**
* Get a template element used for rendering purposes only
*
* @return null|ElementInterface|FieldsetInterface
*/
public function getTemplateElement()
{
if ($this->templateElement === null) {
$this->templateElement = $this->createTemplateElement();
}
return $this->templateElement;
}
/**
* Prepare the collection by adding a dummy template element if the user want one
*
* @param FormInterface $form
* @return mixed|void
*/
public function prepareElement(FormInterface $form)
{
// Create a template that will also be prepared
if ($this->shouldCreateTemplate) {
$templateElement = $this->getTemplateElement();
$this->add($templateElement);
}
parent::prepareElement($form);
// The template element has been prepared, but we don't want it to be rendered nor validated, so remove it from the list
if ($this->shouldCreateTemplate) {
$this->remove($this->templatePlaceholder);
}
}
/**
* @return array
*/
public function extract()
{
if ($this->object instanceof Traversable) {
$this->object = ArrayUtils::iteratorToArray($this->object);
}
if (!is_array($this->object)) {
return array();
}
$values = array();
foreach ($this->object as $key => $value) {
if ($this->hydrator) {
$values[$key] = $this->hydrator->extract($value);
} elseif ($value instanceof $this->targetElement->object) {
// @see https://github.com/zendframework/zf2/pull/2848
$targetElement = clone $this->targetElement;
$targetElement->object = $value;
$values[$key] = $targetElement->extract();
}
}
return $values;
}
/**
* If both count and targetElement are set, add them to the fieldset
*
* @return void
*/
public function prepareFieldset()
{
if ($this->targetElement !== null) {
for ($i = 0; $i != $this->count; ++$i) {
$elementOrFieldset = $this->createNewTargetElementInstance();
$elementOrFieldset->setName($i);
$this->add($elementOrFieldset);
}
}
}
/**
* Create a new instance of the target element
*
* @return ElementInterface
*/
protected function createNewTargetElementInstance()
{
return clone $this->targetElement;
}
/**
* Create a dummy template element
*
* @return null|ElementInterface|FieldsetInterface
*/
protected function createTemplateElement()
{
if (!$this->shouldCreateTemplate) {
return null;
}
if ($this->templateElement) {
return $this->templateElement;
}
$elementOrFieldset = $this->createNewTargetElementInstance();
$elementOrFieldset->setName($this->templatePlaceholder);
return $elementOrFieldset;
}
}