vendor/symfony/form/Extension/Core/DataMapper/PropertyPathMapper.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\Core\DataMapper;
  11. use Symfony\Component\Form\DataMapperInterface;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. use Symfony\Component\PropertyAccess\PropertyAccess;
  14. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  15. /**
  16.  * Maps arrays/objects to/from forms using property paths.
  17.  *
  18.  * @author Bernhard Schussek <bschussek@gmail.com>
  19.  */
  20. class PropertyPathMapper implements DataMapperInterface
  21. {
  22.     private $propertyAccessor;
  23.     public function __construct(PropertyAccessorInterface $propertyAccessor null)
  24.     {
  25.         $this->propertyAccessor $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public function mapDataToForms($dataiterable $forms)
  31.     {
  32.         $empty null === $data || [] === $data;
  33.         if (!$empty && !\is_array($data) && !\is_object($data)) {
  34.             throw new UnexpectedTypeException($data'object, array or empty');
  35.         }
  36.         foreach ($forms as $form) {
  37.             $propertyPath $form->getPropertyPath();
  38.             $config $form->getConfig();
  39.             if (!$empty && null !== $propertyPath && $config->getMapped()) {
  40.                 $form->setData($this->propertyAccessor->getValue($data$propertyPath));
  41.             } else {
  42.                 $form->setData($config->getData());
  43.             }
  44.         }
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function mapFormsToData(iterable $forms, &$data)
  50.     {
  51.         if (null === $data) {
  52.             return;
  53.         }
  54.         if (!\is_array($data) && !\is_object($data)) {
  55.             throw new UnexpectedTypeException($data'object, array or empty');
  56.         }
  57.         foreach ($forms as $form) {
  58.             $propertyPath $form->getPropertyPath();
  59.             $config $form->getConfig();
  60.             // Write-back is disabled if the form is not synchronized (transformation failed),
  61.             // if the form was not submitted and if the form is disabled (modification not allowed)
  62.             if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) {
  63.                 $propertyValue $form->getData();
  64.                 // If the field is of type DateTimeInterface and the data is the same skip the update to
  65.                 // keep the original object hash
  66.                 if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->propertyAccessor->getValue($data$propertyPath)) {
  67.                     continue;
  68.                 }
  69.                 // If the data is identical to the value in $data, we are
  70.                 // dealing with a reference
  71.                 if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->propertyAccessor->getValue($data$propertyPath)) {
  72.                     $this->propertyAccessor->setValue($data$propertyPath$propertyValue);
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }