Skip to content

Commit

Permalink
Merge pull request #1145 from schmittjoh/sf5
Browse files Browse the repository at this point in the history
Symfony 5.0 compatibility
  • Loading branch information
goetas authored Dec 9, 2019
2 parents 87c992d + 1eeb406 commit 05fea5a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
1 change: 0 additions & 1 deletion .travis/install_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 15 additions & 2 deletions src/Handler/FormErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 05fea5a

Please sign in to comment.