Skip to content

Commit

Permalink
added assert validation support
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabricio872 committed Jul 31, 2021
1 parent 03bb10b commit f9530d7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 11 additions & 2 deletions src/Command/UserRegisterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,6 +29,10 @@ class UserRegisterCommand extends Command
private $io;
/** @var EntityManagerInterface $em */
private $em;
/**
* @var ValidatorInterface
*/
private $validator;

/**
* UserRegisterCommand constructor.
Expand All @@ -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()
Expand Down Expand Up @@ -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());
Expand Down
22 changes: 21 additions & 1 deletion src/Services/Ask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

/**
Expand All @@ -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);

Expand All @@ -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 . ' ';
Expand Down

2 comments on commit f9530d7

@Fabricio872
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fabricio872
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.