vendor/easycorp/easyadmin-bundle/src/Orm/EntityUpdater.php line 35

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Orm;
  3. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Orm\EntityUpdaterInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  5. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  6. use Symfony\Component\Validator\ConstraintViolationList;
  7. use Symfony\Component\Validator\Validator\ValidatorInterface;
  8. /**
  9.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  10.  */
  11. final class EntityUpdater implements EntityUpdaterInterface
  12. {
  13.     private PropertyAccessorInterface $propertyAccessor;
  14.     private ValidatorInterface $validator;
  15.     public function __construct(PropertyAccessorInterface $propertyAccessorValidatorInterface $validator)
  16.     {
  17.         $this->propertyAccessor $propertyAccessor;
  18.         $this->validator $validator;
  19.     }
  20.     public function updateProperty(EntityDto $entityDtostring $propertyName$value): void
  21.     {
  22.         if (!$this->propertyAccessor->isWritable($entityDto->getInstance(), $propertyName)) {
  23.             throw new \RuntimeException(sprintf('The "%s" property of the "%s" entity is not writable.'$propertyName$entityDto->getName()));
  24.         }
  25.         $entityInstance $entityDto->getInstance();
  26.         $this->propertyAccessor->setValue($entityInstance$propertyName$value);
  27.         /** @var ConstraintViolationList $violations */
  28.         $violations $this->validator->validate($entityInstance);
  29.         if (\count($violations)) {
  30.             throw new \RuntimeException((string) $violations);
  31.         }
  32.         $entityDto->setInstance($entityInstance);
  33.     }
  34. }