From b10e82519cf711309cb4a54af5976cee85cd27ad Mon Sep 17 00:00:00 2001 From: Stadly Date: Fri, 30 Nov 2018 11:49:39 +0100 Subject: [PATCH] Change translator interface --- CHANGELOG.md | 1 + composer.json | 2 +- src/Policy.php | 2 +- src/Rule/CharacterClass.php | 26 ++++++++++++++++---------- src/Rule/Digit.php | 12 ++++++------ src/Rule/HaveIBeenPwned.php | 12 ++++++------ src/Rule/Length.php | 12 ++++++------ src/Rule/LowerCase.php | 12 ++++++------ src/Rule/UpperCase.php | 12 ++++++------ tests/PolicyTest.php | 2 +- 10 files changed, 50 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc2e3d5..f23dd16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ### Changed - Specify the maximum number of appearances in breaches before the minimum in Have I Been Pwned? - Renamed Dictionary methods `getMin` and `getMax` to `getMinWordLength` and `getMaxWordLength`. +- Translators must implement `Symfony\Contracts\Translation\TranslatorInterface` instead of `Symfony\Component\Translation\TranslatorInterface`, since the latter is deprecated. ### Fixed - Nothing diff --git a/composer.json b/composer.json index 4be2bcb..9c99b03 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "php-http/httplug": "^2.0", "psr/http-client": "^1.0", "psr/http-factory": "^1.0", - "symfony/translation": "^4.0", + "symfony/translation": "^4.2", "vanderlee/php-stable-sort-functions": "^2.0.4" }, "autoload": { diff --git a/src/Policy.php b/src/Policy.php index 0b3e797..48bc08c 100644 --- a/src/Policy.php +++ b/src/Policy.php @@ -8,7 +8,7 @@ use Stadly\PasswordPolice\Rule\RuleInterface; use Stadly\PasswordPolice\Rule\TestException; use Symfony\Component\Translation\Translator; -use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; final class Policy { diff --git a/src/Rule/CharacterClass.php b/src/Rule/CharacterClass.php index d53aab5..c08a5d6 100644 --- a/src/Rule/CharacterClass.php +++ b/src/Rule/CharacterClass.php @@ -116,11 +116,13 @@ public function getMessage(): string $translator = Policy::getTranslator(); if ($this->getMax() === null) { - return $translator->transChoice( + return $translator->trans( 'There must be at least one character matching %characters%.|'. 'There must be at least %count% characters matching %characters%.', - $this->getMin(), - ['%characters%' => $this->getCharacters()] + [ + '%count%' => $this->getMin(), + '%characters%' => $this->getCharacters(), + ] ); } @@ -132,20 +134,24 @@ public function getMessage(): string } if ($this->getMin() === 0) { - return $translator->transChoice( + return $translator->trans( 'There must be at most one character matching %characters%.|'. 'There must be at most %count% characters matching %characters%.', - $this->getMax(), - ['%characters%' => $this->getCharacters()] + [ + '%count%' => $this->getMax(), + '%characters%' => $this->getCharacters(), + ] ); } if ($this->getMin() === $this->getMax()) { - return $translator->transChoice( + return $translator->trans( 'There must be exactly one character matching %characters%.|'. 'There must be exactly %count% characters matching %characters%.', - $this->getMin(), - ['%characters%' => $this->getCharacters()] + [ + '%count%' => $this->getMin(), + '%characters%' => $this->getCharacters(), + ] ); } @@ -154,7 +160,7 @@ public function getMessage(): string [ '%min%' => $this->getMin(), '%max%' => $this->getMax(), - '%characters%' => $this->getCharacters() + '%characters%' => $this->getCharacters(), ] ); } diff --git a/src/Rule/Digit.php b/src/Rule/Digit.php index d8dda75..9c1a977 100644 --- a/src/Rule/Digit.php +++ b/src/Rule/Digit.php @@ -25,10 +25,10 @@ public function getMessage(): string $translator = Policy::getTranslator(); if ($this->getMax() === null) { - return $translator->transChoice( + return $translator->trans( 'There must be at least one digit.|'. 'There must be at least %count% digits.', - $this->getMin() + ['%count%' => $this->getMin()] ); } @@ -39,18 +39,18 @@ public function getMessage(): string } if ($this->getMin() === 0) { - return $translator->transChoice( + return $translator->trans( 'There must be at most one digit.|'. 'There must be at most %count% digits.', - $this->getMax() + ['%count%' => $this->getMax()] ); } if ($this->getMin() === $this->getMax()) { - return $translator->transChoice( + return $translator->trans( 'There must be exactly one digit.|'. 'There must be exactly %count% digits.', - $this->getMin() + ['%count%' => $this->getMin()] ); } diff --git a/src/Rule/HaveIBeenPwned.php b/src/Rule/HaveIBeenPwned.php index 9fb5e26..ca7e94e 100644 --- a/src/Rule/HaveIBeenPwned.php +++ b/src/Rule/HaveIBeenPwned.php @@ -148,10 +148,10 @@ public function getMessage(): string $translator = Policy::getTranslator(); if ($this->getMax() === null) { - return $translator->transChoice( + return $translator->trans( 'Must appear at least once in breaches.|'. 'Must appear at least %count% times in breaches.', - $this->getMin() + ['%count%' => $this->getMin()] ); } @@ -162,18 +162,18 @@ public function getMessage(): string } if ($this->getMin() === 0) { - return $translator->transChoice( + return $translator->trans( 'Must appear at most once in breaches.|'. 'Must appear at most %count% times in breaches.', - $this->getMax() + ['%count%' => $this->getMax()] ); } if ($this->getMin() === $this->getMax()) { - return $translator->transChoice( + return $translator->trans( 'Must appear exactly once in breaches.|'. 'Must appear exactly %count% times in breaches.', - $this->getMin() + ['%count%' => $this->getMin()] ); } diff --git a/src/Rule/Length.php b/src/Rule/Length.php index 8c886d7..d870fa6 100644 --- a/src/Rule/Length.php +++ b/src/Rule/Length.php @@ -98,10 +98,10 @@ public function getMessage(): string $translator = Policy::getTranslator(); if ($this->getMax() === null) { - return $translator->transChoice( + return $translator->trans( 'There must be at least one character.|'. 'There must be at least %count% characters.', - $this->getMin() + ['%count%' => $this->getMin()] ); } @@ -112,18 +112,18 @@ public function getMessage(): string } if ($this->getMin() === 0) { - return $translator->transChoice( + return $translator->trans( 'There must be at most one character.|'. 'There must be at most %count% characters.', - $this->getMax() + ['%count%' => $this->getMax()] ); } if ($this->getMin() === $this->getMax()) { - return $translator->transChoice( + return $translator->trans( 'There must be exactly one character.|'. 'There must be exactly %count% characters.', - $this->getMin() + ['%count%' => $this->getMin()] ); } diff --git a/src/Rule/LowerCase.php b/src/Rule/LowerCase.php index 5ce0e7d..ee9e386 100644 --- a/src/Rule/LowerCase.php +++ b/src/Rule/LowerCase.php @@ -98,10 +98,10 @@ public function getMessage(): string $translator = Policy::getTranslator(); if ($this->getMax() === null) { - return $translator->transChoice( + return $translator->trans( 'There must be at least one lower case character.|'. 'There must be at least %count% lower case characters.', - $this->getMin() + ['%count%' => $this->getMin()] ); } @@ -112,18 +112,18 @@ public function getMessage(): string } if ($this->getMin() === 0) { - return $translator->transChoice( + return $translator->trans( 'There must be at most one lower case character.|'. 'There must be at most %count% lower case characters.', - $this->getMax() + ['%count%' => $this->getMax()] ); } if ($this->getMin() === $this->getMax()) { - return $translator->transChoice( + return $translator->trans( 'There must be exactly one lower case character.|'. 'There must be exactly %count% lower case characters.', - $this->getMin() + ['%count%' => $this->getMin()] ); } diff --git a/src/Rule/UpperCase.php b/src/Rule/UpperCase.php index 7992711..abaa580 100644 --- a/src/Rule/UpperCase.php +++ b/src/Rule/UpperCase.php @@ -98,10 +98,10 @@ public function getMessage(): string $translator = Policy::getTranslator(); if ($this->getMax() === null) { - return $translator->transChoice( + return $translator->trans( 'There must be at least one upper case character.|'. 'There must be at least %count% upper case characters.', - $this->getMin() + ['%count%' => $this->getMin()] ); } @@ -112,18 +112,18 @@ public function getMessage(): string } if ($this->getMin() === 0) { - return $translator->transChoice( + return $translator->trans( 'There must be at most one upper case character.|'. 'There must be at most %count% upper case characters.', - $this->getMax() + ['%count%' => $this->getMax()] ); } if ($this->getMin() === $this->getMax()) { - return $translator->transChoice( + return $translator->trans( 'There must be exactly one upper case character.|'. 'There must be exactly %count% upper case characters.', - $this->getMin() + ['%count%' => $this->getMin()] ); } diff --git a/tests/PolicyTest.php b/tests/PolicyTest.php index aae0be7..7334983 100644 --- a/tests/PolicyTest.php +++ b/tests/PolicyTest.php @@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase; use Stadly\PasswordPolice\Rule\RuleException; use Stadly\PasswordPolice\Rule\RuleInterface; -use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; /** * @coversDefaultClass \Stadly\PasswordPolice\Policy