diff --git a/.travis/install_test.sh b/.travis/install_test.sh index c231abc74..8670f4642 100755 --- a/.travis/install_test.sh +++ b/.travis/install_test.sh @@ -5,6 +5,5 @@ set -ex composer self-update if [[ $TRAVIS_PHP_VERSION != '7.2' ]]; then phpenv config-rm xdebug.ini || true; fi -if [[ $TRAVIS_PHP_VERSION == '7.4'* ]]; then composer require --dev --no-update "symfony/cache:^4.4@dev"; fi composer update $COMPOSER_FLAGS -n diff --git a/composer.json b/composer.json index 2a13c4056..32abdcf2d 100644 --- a/composer.json +++ b/composer.json @@ -35,13 +35,13 @@ "doctrine/phpcr-odm": "^1.3|^2.0", "ocramius/proxy-manager": "^1.0|^2.0", "psr/container": "^1.0", - "symfony/dependency-injection": "^3.0|^4.0", - "symfony/yaml": "^3.3|^4.0", - "symfony/translation": "^3.0|^4.0", - "symfony/validator": "^3.1.9|^4.0", - "symfony/form": "^3.0|^4.0", - "symfony/filesystem": "^3.0|^4.0", - "symfony/expression-language": "^3.0|^4.0", + "symfony/dependency-injection": "^3.0|^4.0|^5.0", + "symfony/yaml": "^3.3|^4.0|^5.0", + "symfony/translation": "^3.0|^4.0|^5.0", + "symfony/validator": "^3.1.9|^4.0|^5.0", + "symfony/form": "^3.0|^4.0|^5.0", + "symfony/filesystem": "^3.0|^4.0|^5.0", + "symfony/expression-language": "^3.0|^4.0|^5.0", "phpunit/phpunit": "^7.5||^8.0", "doctrine/coding-standard": "^5.0" }, diff --git a/src/Handler/FormErrorHandler.php b/src/Handler/FormErrorHandler.php index efd8eb2bc..60a40a454 100644 --- a/src/Handler/FormErrorHandler.php +++ b/src/Handler/FormErrorHandler.php @@ -10,6 +10,7 @@ use Symfony\Component\Form\Form; use Symfony\Component\Form\FormError; use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface as TranslatorContract; final class FormErrorHandler implements SubscribingHandlerInterface { @@ -46,8 +47,16 @@ public static function getSubscribingMethods() return $methods; } - public function __construct(?TranslatorInterface $translator = null, string $translationDomain = 'validators') + public function __construct(?object $translator = null, string $translationDomain = 'validators') { + if (null!== $translator && (!$translator instanceof TranslatorInterface && !$translator instanceof TranslatorContract)) { + throw new \InvalidArgumentException(sprintf( + 'The first argument passed to %s must be instance of %s or %s, %s given', + self::class, + TranslatorInterface::class, + TranslatorContract::class + )); + } $this->translator = $translator; $this->translationDomain = $translationDomain; } @@ -110,7 +119,11 @@ private function getErrorMessage(FormError $error): ?string } if (null !== $error->getMessagePluralization()) { - return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), $this->translationDomain); + if ($this->translator instanceof TranslatorContract) { + return $this->translator->trans($error->getMessageTemplate(), ['%count%' => $error->getMessagePluralization()] + $error->getMessageParameters(), $this->translationDomain); + } else { + return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), $this->translationDomain); + } } return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), $this->translationDomain); diff --git a/tests/Serializer/BaseSerializationTest.php b/tests/Serializer/BaseSerializationTest.php index 40072c6c4..dc02723a6 100644 --- a/tests/Serializer/BaseSerializationTest.php +++ b/tests/Serializer/BaseSerializationTest.php @@ -117,7 +117,6 @@ use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormFactoryBuilder; use Symfony\Component\Translation\IdentityTranslator; -use Symfony\Component\Translation\MessageSelector; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; @@ -1689,7 +1688,7 @@ protected function setUp(): void $this->handlerRegistry->registerSubscribingHandler(new ConstraintViolationHandler()); $this->handlerRegistry->registerSubscribingHandler(new StdClassHandler()); $this->handlerRegistry->registerSubscribingHandler(new DateHandler()); - $this->handlerRegistry->registerSubscribingHandler(new FormErrorHandler(new IdentityTranslator(new MessageSelector()))); + $this->handlerRegistry->registerSubscribingHandler(new FormErrorHandler(new IdentityTranslator())); $this->handlerRegistry->registerSubscribingHandler(new ArrayCollectionHandler()); $this->handlerRegistry->registerSubscribingHandler(new IteratorHandler()); $this->handlerRegistry->registerHandler(