| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Doctrine\Inflector\Rules; |
| 6 |
|
| 7 |
use Doctrine\Inflector\WordInflector; |
| 8 |
|
| 9 |
use function strtolower; |
| 10 |
use function strtoupper; |
| 11 |
use function substr; |
| 12 |
|
| 13 |
class Substitutions implements WordInflector |
| 14 |
{ |
| 15 |
|
| 16 |
private $substitutions; |
| 17 |
|
| 18 |
public function __construct(Substitution ...$substitutions) |
| 19 |
{ |
| 20 |
foreach ($substitutions as $substitution) { |
| 21 |
$this->substitutions[$substitution->getFrom()->getWord()] = $substitution; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public function getFlippedSubstitutions(): Substitutions |
| 26 |
{ |
| 27 |
$substitutions = []; |
| 28 |
|
| 29 |
foreach ($this->substitutions as $substitution) { |
| 30 |
$substitutions[] = new Substitution( |
| 31 |
$substitution->getTo(), |
| 32 |
$substitution->getFrom() |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
return new Substitutions(...$substitutions); |
| 37 |
} |
| 38 |
|
| 39 |
public function inflect(string $word): string |
| 40 |
{ |
| 41 |
$lowerWord = strtolower($word); |
| 42 |
|
| 43 |
if (isset($this->substitutions[$lowerWord])) { |
| 44 |
$firstLetterUppercase = $lowerWord[0] !== $word[0]; |
| 45 |
|
| 46 |
$toWord = $this->substitutions[$lowerWord]->getTo()->getWord(); |
| 47 |
|
| 48 |
if ($firstLetterUppercase) { |
| 49 |
return strtoupper($toWord[0]) . substr($toWord, 1); |
| 50 |
} |
| 51 |
|
| 52 |
return $toWord; |
| 53 |
} |
| 54 |
|
| 55 |
return $word; |
| 56 |
} |
| 57 |
} |