From f6f7d56265003cf9484051035f5647bf7b9ef977 Mon Sep 17 00:00:00 2001 From: Martin Brettschneider Date: Tue, 21 May 2024 14:14:44 +0200 Subject: [PATCH] feat: add Translator to SymfonyValidator --- .docs/mapping.md | 6 +++++- composer.json | 1 + .../Mapping/Validator/SymfonyValidator.php | 20 +++++++++++++++++++ tests/Cases/Core/DI/ApiExtension.phpt | 2 ++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.docs/mapping.md b/.docs/mapping.md index 3532653..db5ae2e 100644 --- a/.docs/mapping.md +++ b/.docs/mapping.md @@ -236,7 +236,9 @@ final class UserFilter extends BasicEntity ``` You can override `ConstraintValidatorFactory` on `SymfonyValidator`. If you want to use [custom validation contstraints](https://symfony.com/doc/current/validation/custom_constraint.html) with support of Nette DI, -you should also install [contributte/validator](https://github.com/contributte/validator). Take a look at example. +you should also install [contributte/validator](https://github.com/contributte/validator). +If you want to use translated constraint messages, you can use [contributte/translation](https://github.com/contributte/translation). +Take a look at example. ```yaml services: @@ -244,6 +246,8 @@ services: factory: Apitte\Core\Mapping\Validator\SymfonyValidator setup: - setConstraintValidatorFactory(Contributte\Validator\ContainerConstraintValidatorFactory()) + - setTranslator(@Contributte\Translation\Translator) + - setTranslationDomain('validators') api: plugins: Apitte\Core\DI\Plugin\CoreMappingPlugin: diff --git a/composer.json b/composer.json index 656aa57..0eac1cf 100644 --- a/composer.json +++ b/composer.json @@ -40,6 +40,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" diff --git a/src/Core/Mapping/Validator/SymfonyValidator.php b/src/Core/Mapping/Validator/SymfonyValidator.php index 80e7845..8823b8c 100644 --- a/src/Core/Mapping/Validator/SymfonyValidator.php +++ b/src/Core/Mapping/Validator/SymfonyValidator.php @@ -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 { @@ -16,6 +17,10 @@ class SymfonyValidator implements IEntityValidator private ?ConstraintValidatorFactoryInterface $constraintValidatorFactory = null; + private ?TranslatorInterface $translator = null; + + private ?string $translationDomain = null; + public function __construct(?Reader $reader = null) { $this->reader = $reader; @@ -27,6 +32,16 @@ public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterfac $this->constraintValidatorFactory = $constraintValidatorFactory; } + public function setTranslator(TranslatorInterface $translator): void + { + $this->translator = $translator; + } + + public function setTranslationDomain(string $translationDomain): void + { + $this->translationDomain = $translationDomain; + } + /** * @throws ValidationException * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint @@ -44,6 +59,11 @@ public function validate(object $entity): void $validatorBuilder->setConstraintValidatorFactory($this->constraintValidatorFactory); } + if ($this->translator !== null) { + $validatorBuilder->setTranslator($this->translator); + $validatorBuilder->setTranslationDomain($this->translationDomain); + } + $validator = $validatorBuilder->getValidator(); /** @var ConstraintViolationListInterface $violations */ diff --git a/tests/Cases/Core/DI/ApiExtension.phpt b/tests/Cases/Core/DI/ApiExtension.phpt index 6dae956..f0215c9 100644 --- a/tests/Cases/Core/DI/ApiExtension.phpt +++ b/tests/Cases/Core/DI/ApiExtension.phpt @@ -107,6 +107,8 @@ Toolkit::test(function (): void { ) setup: - setConstraintValidatorFactory(Symfony\Component\Validator\ConstraintValidatorFactory()) + - setTranslator(Symfony\Component\Translation\Translator(en)) + - setTranslationDomain(validators) api: plugins: Apitte\Core\DI\Plugin\CoreMappingPlugin: