RulesetInflector::inflect()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

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