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

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