[Form] Used direct method access in MergeCollectionListener instead of Reflection to avoid problems when using class hierarchies

This commit is contained in:
Bernhard Schussek 2012-02-02 12:50:16 +01:00
parent d208f4e0de
commit b39377402b

View File

@ -80,8 +80,8 @@ class MergeCollectionListener implements EventSubscriberInterface
$form = $event->getForm(); $form = $event->getForm();
$data = $event->getData(); $data = $event->getData();
$parentData = $form->hasParent() ? $form->getParent()->getData() : null; $parentData = $form->hasParent() ? $form->getParent()->getData() : null;
$adder = null; $adderName = null;
$remover = null; $removerName = null;
if (null === $data) { if (null === $data) {
$data = array(); $data = array();
@ -102,36 +102,34 @@ class MergeCollectionListener implements EventSubscriberInterface
$reflClass = new \ReflectionClass($parentData); $reflClass = new \ReflectionClass($parentData);
foreach ($singulars as $singular) { foreach ($singulars as $singular) {
$adderName = $this->adderPrefix . $singular; $maybeAdderName = $this->adderPrefix . $singular;
$removerName = $this->removerPrefix . $singular; $maybeRemoverName = $this->removerPrefix . $singular;
if ($this->allowAdd && $reflClass->hasMethod($adderName)) { if ($this->allowAdd && $reflClass->hasMethod($maybeAdderName)) {
$adder = $reflClass->getMethod($adderName); $adder = $reflClass->getMethod($maybeAdderName);
if (!$adder->isPublic() || $adder->getNumberOfRequiredParameters() !== 1) { if ($adder->isPublic() && $adder->getNumberOfRequiredParameters() === 1) {
// False alert $adderName = $maybeAdderName;
$adder = null;
} }
} }
if ($this->allowDelete && $reflClass->hasMethod($removerName)) { if ($this->allowDelete && $reflClass->hasMethod($maybeRemoverName)) {
$remover = $reflClass->getMethod($removerName); $remover = $reflClass->getMethod($maybeRemoverName);
if (!$remover->isPublic() || $remover->getNumberOfRequiredParameters() !== 1) { if ($remover->isPublic() && $remover->getNumberOfRequiredParameters() === 1) {
// False alert $removerName = $maybeRemoverName;
$remover = null;
} }
} }
// When we want to both add and delete, we look for an adder and // When we want to both add and delete, we look for an adder and
// remover with the same name // remover with the same name
if (!($this->allowAdd && !$adder) && !($this->allowDelete && !$remover)) { if (!($this->allowAdd && !$adderName) && !($this->allowDelete && !$removerName)) {
break; break;
} }
// False alert // False alert
$adder = null; $adderName = null;
$remover = null; $removerName = null;
} }
} }
@ -155,17 +153,17 @@ class MergeCollectionListener implements EventSubscriberInterface
} }
} }
if ($adder || $remover) { if ($adderName || $removerName) {
// If methods to add and to remove exist, call them now, if allowed // If methods to add and to remove exist, call them now, if allowed
if ($remover) { if ($removerName) {
foreach ($itemsToDelete as $item) { foreach ($itemsToDelete as $item) {
$remover->invoke($parentData, $item); $parentData->$removerName($item);
} }
} }
if ($adder) { if ($adderName) {
foreach ($itemsToAdd as $item) { foreach ($itemsToAdd as $item) {
$adder->invoke($parentData, $item); $parentData->$adderName($item);
} }
} }
} elseif (!$originalData) { } elseif (!$originalData) {