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

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\Exception\AccessException;
  14. use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
  15. use Symfony\Component\PropertyAccess\PropertyAccess;
  16. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  17. /**
  18.  * Maps arrays/objects to/from forms using property paths.
  19.  *
  20.  * @author Bernhard Schussek <bschussek@gmail.com>
  21.  */
  22. class PropertyPathMapper implements DataMapperInterface
  23. {
  24.     private $propertyAccessor;
  25.     public function __construct(PropertyAccessorInterface $propertyAccessor null)
  26.     {
  27.         $this->propertyAccessor $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function mapDataToForms($data$forms)
  33.     {
  34.         $empty null === $data || [] === $data;
  35.         if (!$empty && !\is_array($data) && !\is_object($data)) {
  36.             throw new UnexpectedTypeException($data'object, array or empty');
  37.         }
  38.         foreach ($forms as $form) {
  39.             $propertyPath $form->getPropertyPath();
  40.             $config $form->getConfig();
  41.             if (!$empty && null !== $propertyPath && $config->getMapped()) {
  42.                 $form->setData($this->getPropertyValue($data$propertyPath));
  43.             } else {
  44.                 $form->setData($config->getData());
  45.             }
  46.         }
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function mapFormsToData($forms, &$data)
  52.     {
  53.         if (null === $data) {
  54.             return;
  55.         }
  56.         if (!\is_array($data) && !\is_object($data)) {
  57.             throw new UnexpectedTypeException($data'object, array or empty');
  58.         }
  59.         foreach ($forms as $form) {
  60.             $propertyPath $form->getPropertyPath();
  61.             $config $form->getConfig();
  62.             // Write-back is disabled if the form is not synchronized (transformation failed),
  63.             // if the form was not submitted and if the form is disabled (modification not allowed)
  64.             if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) {
  65.                 $propertyValue $form->getData();
  66.                 // If the field is of type DateTimeInterface and the data is the same skip the update to
  67.                 // keep the original object hash
  68.                 if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data$propertyPath)) {
  69.                     continue;
  70.                 }
  71.                 // If the data is identical to the value in $data, we are
  72.                 // dealing with a reference
  73.                 if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->getPropertyValue($data$propertyPath)) {
  74.                     $this->propertyAccessor->setValue($data$propertyPath$propertyValue);
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     private function getPropertyValue($data$propertyPath)
  80.     {
  81.         try {
  82.             return $this->propertyAccessor->getValue($data$propertyPath);
  83.         } catch (AccessException $e) {
  84.             if (!$e instanceof UninitializedPropertyException
  85.                 // For versions without UninitializedPropertyException check the exception message
  86.                 && (class_exists(UninitializedPropertyException::class) || !str_contains($e->getMessage(), 'You should initialize it'))
  87.             ) {
  88.                 throw $e;
  89.             }
  90.             return null;
  91.         }
  92.     }
  93. }