Rules
1 month ago
CachedWordInflector.php
1 month ago
GenericLanguageInflectorFactory.php
1 month ago
Inflector.php
1 month ago
InflectorFactory.php
1 month ago
Language.php
1 month ago
LanguageInflectorFactory.php
1 month ago
NoopWordInflector.php
1 month ago
RulesetInflector.php
1 month ago
WordInflector.php
1 month ago
RulesetInflector.php
47 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace IAWPSCOPED\Doctrine\Inflector; |
| 5 | |
| 6 | use IAWPSCOPED\Doctrine\Inflector\Rules\Ruleset; |
| 7 | use function array_merge; |
| 8 | /** |
| 9 | * Inflects based on multiple rulesets. |
| 10 | * |
| 11 | * Rules: |
| 12 | * - If the word matches any uninflected word pattern, it is not inflected |
| 13 | * - The first ruleset that returns a different value for an irregular word wins |
| 14 | * - The first ruleset that returns a different value for a regular word wins |
| 15 | * - If none of the above match, the word is left as-is |
| 16 | * @internal |
| 17 | */ |
| 18 | class RulesetInflector implements WordInflector |
| 19 | { |
| 20 | /** @var Ruleset[] */ |
| 21 | private $rulesets; |
| 22 | public function __construct(Ruleset $ruleset, Ruleset ...$rulesets) |
| 23 | { |
| 24 | $this->rulesets = array_merge([$ruleset], $rulesets); |
| 25 | } |
| 26 | public function inflect(string $word) : string |
| 27 | { |
| 28 | if ($word === '') { |
| 29 | return ''; |
| 30 | } |
| 31 | foreach ($this->rulesets as $ruleset) { |
| 32 | if ($ruleset->getUninflected()->matches($word)) { |
| 33 | return $word; |
| 34 | } |
| 35 | $inflected = $ruleset->getIrregular()->inflect($word); |
| 36 | if ($inflected !== $word) { |
| 37 | return $inflected; |
| 38 | } |
| 39 | $inflected = $ruleset->getRegular()->inflect($word); |
| 40 | if ($inflected !== $word) { |
| 41 | return $inflected; |
| 42 | } |
| 43 | } |
| 44 | return $word; |
| 45 | } |
| 46 | } |
| 47 |