* * 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:----A0zIET796AlYFcj30bdHH5fRcIa7xo1xvn2SS6W6AqxyMXN4hJdcl4x49EAv0QWuXZf3jGDISUVDEz+jVW0kOiQSEQkpiNN/KeoZyLG+XDeH3305gE6Xmq6Rd+o92gyGSgp2fx7jLO1IVg0qHE9IXC+sybXfKfF2m8bqmmWqMh8VMOiLhbNrderH+Eb2T9m72uOLzDUzkSjhKJ6YLgzcSSzHqlX+iIIrZzczqEcxDd/q0sl4NvnMGlTyxjfBNUjwTbDLefivsb3rWm4BpWvVeuNfUuFTuxoRSxygRo7ae12XHTjYGSdFdwOUojWFT19t0EO2rCo6VuRAOzUcKXkLcFDlkZFl6r6sS6fV9WLtBPl6KOeGLOwrUHF/X9KhtVEMP5g2l5iOJQu2oL/DYmP6x5nCozxXrZ91YOHI1V9bXUBvFKYegySiHZNQMC45Qz1BsjLEUxM4FWaOc2gUoblzYy3PD6Iq/6Ce5/VMqeAbshkGDc/2s4OFC70iBffdFWTZ8vJY6qPGIz9yLzYVyOHfVc0uWigPTV2DjlAE92+xFOFhSvAOCkUuBapXmnY1qQ7RmXwQ9+4vkQeHXcYBOx4b/n/rnUCZiio6jSSQt4/40KqcLcdIbM+LFHVehCPZ6YHNgx5+m6PdH39M+73rEaMR2Yvl/WJ7pQ774amUULuG5e4=----ATTACHMENT:----MjI3OTE1ODYzMjA3OTg4NCAyMDQyNjk4MDg1NjM1Mjg3IDI5MjY4OTM2NDQ1ODQ2Mzc=