Debian Codegraph @

lib/3rdParty/composer/doctrine/inflector/src/RulesetInflector.php

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