/
MergeCollectionListener.php
341 lines (291 loc) · 12.4 KB
/
MergeCollectionListener.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Util\FormUtil;
use Symfony\Component\Form\Util\PropertyPath;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class MergeCollectionListener implements EventSubscriberInterface
{
/**
* Strategy for merging the new collection into the old collection
*
* @var integer
*/
const MERGE_NORMAL = 1;
/**
* Strategy for calling add/remove methods on the parent data for all
* new/removed elements in the new collection
*
* @var integer
*/
const MERGE_INTO_PARENT = 2;
/**
* Whether elements may be added to the collection
* @var Boolean
*/
private $allowAdd;
/**
* Whether elements may be removed from the collection
* @var Boolean
*/
private $allowDelete;
/**
* Whether to search for and use adder and remover methods
* @var Boolean
*/
private $mergeStrategy;
/**
* The name of the adder method to look for
* @var string
*/
private $addMethod;
/**
* The name of the remover method to look for
* @var string
*/
private $removeMethod;
/**
* A copy of the data before starting binding for this form
* @var mixed
*/
private $dataSnapshot;
/**
* Creates a new listener.
*
* @param Boolean $allowAdd Whether values might be added to the
* collection.
* @param Boolean $allowDelete Whether values might be removed from the
* collection.
* @param integer $mergeStrategy Which strategy to use for merging the
* bound collection with the original
* collection. Might be any combination of
* MERGE_NORMAL and MERGE_INTO_PARENT.
* MERGE_INTO_PARENT has precedence over
* MERGE_NORMAL if an adder/remover method
* is found. The default strategy is to use
* both strategies.
* @param string $addMethod The name of the adder method to use. If
* not given, the listener tries to discover
* the method automatically.
* @param string $removeMethod The name of the remover method to use. If
* not given, the listener tries to discover
* the method automatically.
*
* @throws FormException If the given strategy is invalid.
*/
public function __construct($allowAdd = false, $allowDelete = false, $mergeStrategy = null, $addMethod = null, $removeMethod = null)
{
if ($mergeStrategy && !($mergeStrategy & (self::MERGE_NORMAL | self::MERGE_INTO_PARENT))) {
throw new FormException('The merge strategy needs to be at least MERGE_NORMAL or MERGE_INTO_PARENT');
}
$this->allowAdd = $allowAdd;
$this->allowDelete = $allowDelete;
$this->mergeStrategy = $mergeStrategy ?: self::MERGE_NORMAL | self::MERGE_INTO_PARENT;
$this->addMethod = $addMethod;
$this->removeMethod = $removeMethod;
}
static public function getSubscribedEvents()
{
return array(
FormEvents::PRE_BIND => 'preBind',
FormEvents::BIND_NORM_DATA => 'onBindNormData',
);
}
public function preBind(DataEvent $event)
{
// Get a snapshot of the current state of the normalized data
// to compare against later
$this->dataSnapshot = $event->getForm()->getNormData();
if (is_object($this->dataSnapshot)) {
// Make sure the snapshot remains stable and doesn't change
$this->dataSnapshot = clone $this->dataSnapshot;
}
if (null !== $this->dataSnapshot && !is_array($this->dataSnapshot) && !($this->dataSnapshot instanceof \Traversable && $this->dataSnapshot instanceof \ArrayAccess)) {
throw new UnexpectedTypeException($this->dataSnapshot, 'array or (\Traversable and \ArrayAccess)');
}
}
public function onBindNormData(FilterDataEvent $event)
{
$originalData = $event->getForm()->getNormData();
// If we are not allowed to change anything, return immediately
if (!$this->allowAdd && !$this->allowDelete) {
// Don't set to the snapshot as then we are switching from the
// original object to its copy, which might break things
$event->setData($originalData);
return;
}
$form = $event->getForm();
$data = $event->getData();
$childPropertyPath = null;
$parentData = null;
$addMethod = null;
$removeMethod = null;
$propertyPath = null;
$plural = null;
if ($form->hasParent() && $form->getAttribute('property_path')) {
$propertyPath = new PropertyPath($form->getAttribute('property_path'));
$childPropertyPath = $propertyPath;
$parentData = $form->getParent()->getClientData();
$lastElement = $propertyPath->getElement($propertyPath->getLength() - 1);
// If the property path contains more than one element, the parent
// data is the object at the parent property path
if ($propertyPath->getLength() > 1) {
$parentData = $propertyPath->getParent()->getValue($parentData);
// Property path relative to $parentData
$childPropertyPath = new PropertyPath($lastElement);
}
// The plural form is the last element of the property path
$plural = ucfirst($lastElement);
}
if (null === $data) {
$data = array();
}
if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) {
throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)');
}
if (null !== $originalData && !is_array($originalData) && !($originalData instanceof \Traversable && $originalData instanceof \ArrayAccess)) {
throw new UnexpectedTypeException($originalData, 'array or (\Traversable and \ArrayAccess)');
}
// Check if the parent has matching methods to add/remove items
if (($this->mergeStrategy & self::MERGE_INTO_PARENT) && is_object($parentData)) {
$reflClass = new \ReflectionClass($parentData);
$addMethodNeeded = $this->allowAdd && !$this->addMethod;
$removeMethodNeeded = $this->allowDelete && !$this->removeMethod;
// Any of the two methods is required, but not yet known
if ($addMethodNeeded || $removeMethodNeeded) {
$singulars = (array) FormUtil::singularify($plural);
foreach ($singulars as $singular) {
// Try to find adder, but don't override preconfigured one
if ($addMethodNeeded) {
$addMethod = 'add' . $singular;
// False alert
if (!$this->isAccessible($reflClass, $addMethod, 1)) {
$addMethod = null;
}
}
// Try to find remover, but don't override preconfigured one
if ($removeMethodNeeded) {
$removeMethod = 'remove' . $singular;
// False alert
if (!$this->isAccessible($reflClass, $removeMethod, 1)) {
$removeMethod = null;
}
}
// Found all that we need. Abort search.
if ((!$addMethodNeeded || $addMethod) && (!$removeMethodNeeded || $removeMethod)) {
break;
}
// False alert
$addMethod = null;
$removeMethod = null;
}
}
// Set preconfigured adder
if ($this->allowAdd && $this->addMethod) {
$addMethod = $this->addMethod;
if (!$this->isAccessible($reflClass, $addMethod, 1)) {
throw new FormException(sprintf(
'The public method "%s" could not be found on class %s',
$addMethod,
$reflClass->getName()
));
}
}
// Set preconfigured remover
if ($this->allowDelete && $this->removeMethod) {
$removeMethod = $this->removeMethod;
if (!$this->isAccessible($reflClass, $removeMethod, 1)) {
throw new FormException(sprintf(
'The public method "%s" could not be found on class %s',
$removeMethod,
$reflClass->getName()
));
}
}
}
// Calculate delta between $data and the snapshot created in PRE_BIND
$itemsToDelete = array();
$itemsToAdd = is_object($data) ? clone $data : $data;
if ($this->dataSnapshot) {
foreach ($this->dataSnapshot as $originalItem) {
foreach ($data as $key => $item) {
if ($item === $originalItem) {
// Item found, next original item
unset($itemsToAdd[$key]);
continue 2;
}
}
// Item not found, remember for deletion
foreach ($originalData as $key => $item) {
if ($item === $originalItem) {
$itemsToDelete[$key] = $item;
continue 2;
}
}
}
}
if ($addMethod || $removeMethod) {
// If methods to add and to remove exist, call them now, if allowed
if ($removeMethod) {
foreach ($itemsToDelete as $item) {
$parentData->$removeMethod($item);
}
}
if ($addMethod) {
foreach ($itemsToAdd as $item) {
$parentData->$addMethod($item);
}
}
$event->setData($childPropertyPath->getValue($parentData));
} elseif ($this->mergeStrategy & self::MERGE_NORMAL) {
if (!$originalData) {
// No original data was set. Set it if allowed
if ($this->allowAdd) {
$originalData = $data;
}
} else {
// Original data is an array-like structure
// Add and remove items in the original variable
if ($this->allowDelete) {
foreach ($itemsToDelete as $key => $item) {
unset($originalData[$key]);
}
}
if ($this->allowAdd) {
foreach ($itemsToAdd as $key => $item) {
if (!isset($originalData[$key])) {
$originalData[$key] = $item;
} else {
$originalData[] = $item;
}
}
}
}
$event->setData($originalData);
}
}
private function isAccessible(\ReflectionClass $reflClass, $methodName, $numberOfRequiredParameters) {
if ($reflClass->hasMethod($methodName)) {
$method = $reflClass->getMethod($methodName);
if ($method->isPublic() && $method->getNumberOfRequiredParameters() === $numberOfRequiredParameters) {
return true;
}
}
return false;
}
}