Skip to content

Commit

Permalink
Change translator interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Stadly committed Nov 30, 2018
1 parent f5ad5d0 commit b10e825
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Policy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
26 changes: 16 additions & 10 deletions src/Rule/CharacterClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]
);
}

Expand All @@ -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(),
]
);
}

Expand All @@ -154,7 +160,7 @@ public function getMessage(): string
[
'%min%' => $this->getMin(),
'%max%' => $this->getMax(),
'%characters%' => $this->getCharacters()
'%characters%' => $this->getCharacters(),
]
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Rule/Digit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
);
}

Expand All @@ -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()]
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Rule/HaveIBeenPwned.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
);
}

Expand All @@ -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()]
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Rule/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
);
}

Expand All @@ -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()]
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Rule/LowerCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
);
}

Expand All @@ -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()]
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Rule/UpperCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
);
}

Expand All @@ -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()]
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b10e825

Please sign in to comment.