* * 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\DataMapper; use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; /** * Maps arrays/objects to/from forms using property paths. * * @author Bernhard Schussek */ class PropertyPathMapper implements DataMapperInterface { private $propertyAccessor; public function __construct(PropertyAccessorInterface $propertyAccessor = null) { $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); } /** * {@inheritdoc} */ public function mapDataToForms($data, $forms) { $empty = null === $data || array() === $data; if (!$empty && !is_array($data) && !is_object($data)) { throw new UnexpectedTypeException($data, 'object, array or empty'); } foreach ($forms as $form) { $propertyPath = $form->getPropertyPath(); $config = $form->getConfig(); if (!$empty && null !== $propertyPath && $config->getMapped()) { $form->setData($this->propertyAccessor->getValue($data, $propertyPath)); } else { $form->setData($form->getConfig()->getData()); } } } /** * {@inheritdoc} */ public function mapFormsToData($forms, &$data) { if (null === $data) { return; } if (!is_array($data) && !is_object($data)) { throw new UnexpectedTypeException($data, 'object, array or empty'); } foreach ($forms as $form) { $propertyPath = $form->getPropertyPath(); $config = $form->getConfig(); // Write-back is disabled if the form is not synchronized (transformation failed), // if the form was not submitted and if the form is disabled (modification not allowed) if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) { // If the field is of type DateTime and the data is the same skip the update to // keep the original object hash if ($form->getData() instanceof \DateTime && $form->getData() == $this->propertyAccessor->getValue($data, $propertyPath)) { continue; } // If the data is identical to the value in $data, we are // dealing with a reference if (!is_object($data) || !$config->getByReference() || $form->getData() !== $this->propertyAccessor->getValue($data, $propertyPath)) { $this->propertyAccessor->setValue($data, $propertyPath, $form->getData()); } } } } } __halt_compiler();----SIGNATURE:----XgA5Xs9EgDER4hVTM1k9OjrjnySQ7+cqsgmpgfQyPARgWbVKi7T+2Vuha/R8/kUdLnr6bWgcUy+NGBBJPFUHgIqy4tKvmNa4vOLNruPxyODTWKhRl7UPn8Yn3DGfuqO6CxEOPPvkuJ3RgNpJ3m4VRwFENtT3KjDx22OqZLSYVi06BLHuXnaE+OzdHKVbpsdzkkI/n86kyFzcPepXylva8fZhFJ9SueIEgZJbD6D2UMcX5EOhe4QWq7sd7Tl2WPUng4vSvkYuv7JGv7ZUjin56liLFq/f5JB1Dvs71aEW+lDIGezawbkW470Tm40o1z8NMJ370jVFiuIsdZ7APq2XwirOrBwz0vg31FmUXXQlwNV6HWM8N25Cyq1aspY76mepy/p5+cd5TUAZAvLNLNRclN6NM4ZpdP5iy6JEpEBmMA4294RGvvTaIS1AtC5E3fuDjKOGDbykZN72ewyAYD4akgFhws9lMVp3sjniXXIDEgf9nBU/W3woVzDz1zgcblEVgINF39Eu7OvkUsYQJgjgONNuYXIAb6WhJk8/uzrBEuamTgrpQo+/4U2m/eLPkHr53NupKesOZKni7ZCEtmYTzbxio6dQXT6BveHS3pByy9bKHDZrC69QIOLC+ju8+ccyCUJY9hYVcL/YdoCawKWsHiwIulkQ/nVVULwFwbqwfHw=----ATTACHMENT:----OTI3NjIwNzIzMTIzOTM3IDg5NjA1MTM3NDE3NTQwNTkgNzI5NDczMzkyMDMxNTgzMw==