|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\UX\LiveComponent; |
| 13 | + |
| 14 | +use Psr\Container\ContainerInterface; |
| 15 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 16 | +use Symfony\Component\Validator\ConstraintViolation; |
| 17 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 18 | +use Symfony\Contracts\Service\ServiceSubscriberInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Ryan Weaver <[email protected]> |
| 22 | + * |
| 23 | + * @experimental |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +class ComponentValidator implements ComponentValidatorInterface, ServiceSubscriberInterface |
| 27 | +{ |
| 28 | + /** @var ContainerInterface */ |
| 29 | + private $container; |
| 30 | + |
| 31 | + public function __construct(ContainerInterface $container) |
| 32 | + { |
| 33 | + $this->container = $container; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return ConstraintViolation[][] |
| 38 | + */ |
| 39 | + public function validate(object $component): array |
| 40 | + { |
| 41 | + $errors = $this->getValidator()->validate($component); |
| 42 | + |
| 43 | + $validationErrors = []; |
| 44 | + foreach ($errors as $error) { |
| 45 | + /** @var ConstraintViolation $error */ |
| 46 | + $property = $error->getPropertyPath(); |
| 47 | + if (!isset($validationErrors[$property])) { |
| 48 | + $validationErrors[$property] = []; |
| 49 | + } |
| 50 | + |
| 51 | + $validationErrors[$property][] = $error; |
| 52 | + } |
| 53 | + |
| 54 | + return $validationErrors; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Validates a single field. |
| 59 | + * |
| 60 | + * If a property path - like post.title - is passed, this will |
| 61 | + * validate the *entire* "post" property. It will then loop |
| 62 | + * over all the errors and collect only those for "post.title". |
| 63 | + * |
| 64 | + * @return ConstraintViolation[] |
| 65 | + */ |
| 66 | + public function validateField(object $component, string $propertyPath): array |
| 67 | + { |
| 68 | + $propertyParts = explode('.', $propertyPath); |
| 69 | + $propertyName = $propertyParts[0]; |
| 70 | + |
| 71 | + /** @var $errors */ |
| 72 | + $errors = $this->getValidator()->validateProperty($component, $propertyName); |
| 73 | + |
| 74 | + $errorsForPath = []; |
| 75 | + foreach ($errors as $error) { |
| 76 | + /** @var ConstraintViolation $error */ |
| 77 | + if ($error->getPropertyPath() === $propertyPath) { |
| 78 | + $errorsForPath[] = $error; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return $errorsForPath; |
| 83 | + } |
| 84 | + |
| 85 | + private function getValidator(): ValidatorInterface |
| 86 | + { |
| 87 | + return $this->container->get('validator'); |
| 88 | + } |
| 89 | + |
| 90 | + private function getPropertyAccessor(): PropertyAccessorInterface |
| 91 | + { |
| 92 | + return $this->container->get('property_accessor'); |
| 93 | + } |
| 94 | + |
| 95 | + public static function getSubscribedServices(): array |
| 96 | + { |
| 97 | + return [ |
| 98 | + 'validator' => ValidatorInterface::class, |
| 99 | + 'property_accessor' => PropertyAccessorInterface::class, |
| 100 | + ]; |
| 101 | + } |
| 102 | +} |
0 commit comments