vendor/symfony/form/Extension/Core/DataMapper/DataMapper.php line 63

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\DataAccessorInterface;
  12. use Symfony\Component\Form\DataMapperInterface;
  13. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Form\Extension\Core\DataAccessor\CallbackAccessor;
  15. use Symfony\Component\Form\Extension\Core\DataAccessor\ChainAccessor;
  16. use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
  17. /**
  18.  * Maps arrays/objects to/from forms using data accessors.
  19.  *
  20.  * @author Bernhard Schussek <bschussek@gmail.com>
  21.  */
  22. class DataMapper implements DataMapperInterface
  23. {
  24.     private DataAccessorInterface $dataAccessor;
  25.     public function __construct(DataAccessorInterface $dataAccessor null)
  26.     {
  27.         $this->dataAccessor $dataAccessor ?? new ChainAccessor([
  28.             new CallbackAccessor(),
  29.             new PropertyPathAccessor(),
  30.         ]);
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function mapDataToForms(mixed $data\Traversable $forms): void
  36.     {
  37.         $empty null === $data || [] === $data;
  38.         if (!$empty && !\is_array($data) && !\is_object($data)) {
  39.             throw new UnexpectedTypeException($data'object, array or empty');
  40.         }
  41.         foreach ($forms as $form) {
  42.             $config $form->getConfig();
  43.             if (!$empty && $config->getMapped() && $this->dataAccessor->isReadable($data$form)) {
  44.                 $form->setData($this->dataAccessor->getValue($data$form));
  45.             } else {
  46.                 $form->setData($config->getData());
  47.             }
  48.         }
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function mapFormsToData(\Traversable $formsmixed &$data): void
  54.     {
  55.         if (null === $data) {
  56.             return;
  57.         }
  58.         if (!\is_array($data) && !\is_object($data)) {
  59.             throw new UnexpectedTypeException($data'object, array or empty');
  60.         }
  61.         foreach ($forms as $form) {
  62.             $config $form->getConfig();
  63.             // Write-back is disabled if the form is not synchronized (transformation failed),
  64.             // if the form was not submitted and if the form is disabled (modification not allowed)
  65.             if ($config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled() && $this->dataAccessor->isWritable($data$form)) {
  66.                 $this->dataAccessor->setValue($data$form->getData(), $form);
  67.             }
  68.         }
  69.     }
  70. }