Skip to content

Commit

Permalink
feat: add Translator to SymfonyValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
martenb committed May 21, 2024
1 parent e78b02d commit edea9a4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"nette/http": "^3.2.3",
"psr/log": "^2.0.0 || ^3.0.0",
"symfony/console": "^6.4.0 || ^7.0.0",
"symfony/translation": "^6.4.0 | ^7.0.0",
"symfony/validator": "^6.4.0 || ^7.0.0",
"symfony/yaml": "^6.4.0 || ^7.0.0",
"tracy/tracy": "^2.10.5"
Expand Down
12 changes: 12 additions & 0 deletions src/Core/Mapping/Validator/SymfonyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Contracts\Translation\TranslatorInterface;

class SymfonyValidator implements IEntityValidator
{
Expand All @@ -16,6 +17,8 @@ class SymfonyValidator implements IEntityValidator

private ?ConstraintValidatorFactoryInterface $constraintValidatorFactory = null;

private ?TranslatorInterface $translator = null;

public function __construct(?Reader $reader = null)
{
$this->reader = $reader;
Expand All @@ -27,6 +30,11 @@ public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterfac
$this->constraintValidatorFactory = $constraintValidatorFactory;
}

public function setTranslator(TranslatorInterface $translator): void
{
$this->translator = $translator;
}

/**
* @throws ValidationException
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
Expand All @@ -44,6 +52,10 @@ public function validate(object $entity): void
$validatorBuilder->setConstraintValidatorFactory($this->constraintValidatorFactory);
}

if ($this->translator !== null) {
$validatorBuilder->setTranslator($this->translator);
}

$validator = $validatorBuilder->getValidator();

/** @var ConstraintViolationListInterface $violations */
Expand Down
1 change: 1 addition & 0 deletions tests/Cases/Core/DI/ApiExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Toolkit::test(function (): void {
)
setup:
- setConstraintValidatorFactory(Symfony\Component\Validator\ConstraintValidatorFactory())
- setTranslator(Symfony\Component\Translation\Translator(en))
api:
plugins:
Apitte\Core\DI\Plugin\CoreMappingPlugin:
Expand Down

0 comments on commit edea9a4

Please sign in to comment.