English
1 month ago
Esperanto
1 month ago
French
1 month ago
Italian
1 month ago
NorwegianBokmal
1 month ago
Portuguese
1 month ago
Spanish
1 month ago
Turkish
1 month ago
Pattern.php
1 month ago
Patterns.php
1 month ago
Ruleset.php
1 month ago
Substitution.php
1 month ago
Substitutions.php
1 month ago
Transformation.php
1 month ago
Transformations.php
1 month ago
Word.php
1 month ago
Substitutions.php
43 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace IAWPSCOPED\Doctrine\Inflector\Rules; |
| 5 | |
| 6 | use IAWPSCOPED\Doctrine\Inflector\WordInflector; |
| 7 | use function strtolower; |
| 8 | use function strtoupper; |
| 9 | use function substr; |
| 10 | /** @internal */ |
| 11 | class Substitutions implements WordInflector |
| 12 | { |
| 13 | /** @var Substitution[] */ |
| 14 | private $substitutions; |
| 15 | public function __construct(Substitution ...$substitutions) |
| 16 | { |
| 17 | foreach ($substitutions as $substitution) { |
| 18 | $this->substitutions[$substitution->getFrom()->getWord()] = $substitution; |
| 19 | } |
| 20 | } |
| 21 | public function getFlippedSubstitutions() : Substitutions |
| 22 | { |
| 23 | $substitutions = []; |
| 24 | foreach ($this->substitutions as $substitution) { |
| 25 | $substitutions[] = new Substitution($substitution->getTo(), $substitution->getFrom()); |
| 26 | } |
| 27 | return new Substitutions(...$substitutions); |
| 28 | } |
| 29 | public function inflect(string $word) : string |
| 30 | { |
| 31 | $lowerWord = strtolower($word); |
| 32 | if (isset($this->substitutions[$lowerWord])) { |
| 33 | $firstLetterUppercase = $lowerWord[0] !== $word[0]; |
| 34 | $toWord = $this->substitutions[$lowerWord]->getTo()->getWord(); |
| 35 | if ($firstLetterUppercase) { |
| 36 | return strtoupper($toWord[0]) . substr($toWord, 1); |
| 37 | } |
| 38 | return $toWord; |
| 39 | } |
| 40 | return $word; |
| 41 | } |
| 42 | } |
| 43 |