From f9530d78e3c4c72ddd199b6e182e78320f4a9b10 Mon Sep 17 00:00:00 2001 From: Fabricio Date: Sat, 31 Jul 2021 21:08:50 +0200 Subject: [PATCH] added assert validation support --- composer.json | 6 +++++- src/Command/UserRegisterCommand.php | 13 +++++++++++-- src/Services/Ask.php | 22 +++++++++++++++++++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 9fa6930..fb949e1 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,15 @@ ], "require": { "php": "^7.2|^8.0", + "doctrine/annotations": "^1.13", + "doctrine/orm": "^2.9", "symfony/config": "^4.4|^5", + "symfony/console": "^5.3", "symfony/dependency-injection": "^4.4|^5", "symfony/http-kernel": "^4.4|^5", "symfony/security-bundle": "^4.4|^5", - "symfony/serializer": "^4.4|^5" + "symfony/serializer": "^4.4|^5", + "symfony/validator": "^5.3" }, "require-dev": { "symfony/phpunit-bridge": "^4.4|^5" diff --git a/src/Command/UserRegisterCommand.php b/src/Command/UserRegisterCommand.php index 4209f58..5e8d229 100644 --- a/src/Command/UserRegisterCommand.php +++ b/src/Command/UserRegisterCommand.php @@ -14,6 +14,7 @@ use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; +use Symfony\Component\Validator\Validator\ValidatorInterface; class UserRegisterCommand extends Command { @@ -28,6 +29,10 @@ class UserRegisterCommand extends Command private $io; /** @var EntityManagerInterface $em */ private $em; + /** + * @var ValidatorInterface + */ + private $validator; /** * UserRegisterCommand constructor. @@ -40,13 +45,16 @@ public function __construct( string $userClassName, UserPasswordEncoderInterface $passwordEncoder, Reader $reader, - EntityManagerInterface $em) + EntityManagerInterface $em, + ValidatorInterface $validator + ) { $this->userClassName = $userClassName; parent::__construct(); $this->passwordEncoder = $passwordEncoder; $this->reader = $reader; $this->em = $em; + $this->validator = $validator; } protected function configure() @@ -77,7 +85,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->userClassName, $this->reader, $this->io, - $this->passwordEncoder + $this->passwordEncoder, + $this->validator ); foreach ($userClassReflection->getProperties() as $property) { $data[$property->getName()] = $ask->ask($property->getName()); diff --git a/src/Services/Ask.php b/src/Services/Ask.php index 95f8836..76b8414 100644 --- a/src/Services/Ask.php +++ b/src/Services/Ask.php @@ -8,6 +8,12 @@ use Fabricio872\RegisterCommand\Services\Questions\QuestionInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Constraints\AbstractComparison; +use Symfony\Component\Validator\Constraints\All; +use Symfony\Component\Validator\Constraints\Email; +use Symfony\Component\Validator\ConstraintViolation; +use Symfony\Component\Validator\Validator\ValidatorInterface; class Ask { @@ -21,18 +27,24 @@ class Ask private $passwordEncoder; /** @var string $userIdentifier */ private $userIdentifier = ' '; + /** + * @var ValidatorInterface + */ + private $validator; public function __construct( string $userClassName, Reader $reader, SymfonyStyle $io, - UserPasswordEncoderInterface $passwordEncoder + UserPasswordEncoderInterface $passwordEncoder, + ValidatorInterface $validator ) { $this->userClassName = $userClassName; $this->reader = $reader; $this->io = $io; $this->passwordEncoder = $passwordEncoder; + $this->validator = $validator; } /** @@ -51,6 +63,7 @@ public function getUserIdentifier(): string public function ask(string $propertyName) { $userReflection = new \ReflectionClass($this->userClassName); +// dump($this->reader->getPropertyAnnotation($userReflection->getProperty($propertyName), Constraint::class)); /** @var ?RegisterCommand $annotation */ $annotation = $this->reader->getPropertyAnnotation($userReflection->getProperty($propertyName), RegisterCommand::class); @@ -69,6 +82,13 @@ public function ask(string $propertyName) } $answer = $question->getAnswer(); + if ($this->reader->getPropertyAnnotation($userReflection->getProperty($propertyName), Constraint::class)) { + /** @var ConstraintViolation $violation */ + foreach ($this->validator->validate($answer, $this->reader->getPropertyAnnotation($userReflection->getProperty($propertyName), Constraint::class)) as $violation) { + $this->io->error($violation->getMessage()); + $answer = $question->getAnswer(); + } + } if ($annotation->userIdentifier) { $this->userIdentifier = ' ' . $answer . ' ';