| Total Complexity | 9 |
| Total Lines | 49 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | class Irregular implements WordInflector |
||
| 16 | { |
||
| 17 | /** @var Substitution[] */ |
||
| 18 | private $substitutions; |
||
| 19 | |||
| 20 | /** @var string|null */ |
||
| 21 | private $regex; |
||
| 22 | |||
| 23 | 528 | public function __construct(Substitution ...$substitutions) |
|
| 27 | } |
||
| 28 | 528 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @return Substitution[] |
||
| 32 | */ |
||
| 33 | 526 | public function getFlippedSubstitutions() : iterable |
|
| 34 | { |
||
| 35 | 526 | foreach ($this->substitutions as $substitution) { |
|
| 36 | 526 | yield new Substitution($substitution->getTo(), $substitution->getFrom()); |
|
| 37 | } |
||
| 38 | 526 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * @return Substitution[] |
||
| 42 | */ |
||
| 43 | 526 | public function getSubstitutions() : iterable |
|
| 44 | { |
||
| 45 | 526 | yield from array_values($this->substitutions); |
|
| 46 | 526 | } |
|
| 47 | |||
| 48 | 267 | public function inflect(string $word) : string |
|
| 49 | { |
||
| 50 | 267 | if (preg_match('/(.*)\\b(' . $this->getRegex() . ')$/i', $word, $regs) > 0) { |
|
| 51 | 94 | return $regs[1] . $word[0] . substr($this->substitutions[strtolower($regs[2])]->getTo()->getWord(), 1); |
|
| 52 | } |
||
| 53 | |||
| 54 | 176 | return $word; |
|
| 55 | } |
||
| 56 | |||
| 57 | 267 | private function getRegex() : string |
|
| 64 | } |
||
| 65 | } |
||
| 66 |