Completed
Pull Request — master (#139)
by Andreas
04:45
created

RulesetInflector::inflect()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 29
ccs 15
cts 15
cp 1
rs 8.4444
cc 8
nc 16
nop 1
crap 8
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 1109
    public function __construct(Ruleset $ruleset, Ruleset ...$rulesets)
25
    {
26 1109
        $this->rulesets = array_merge([$ruleset], $rulesets);
27 1109
    }
28
29 1570
    public function inflect(string $word) : string
30
    {
31 1570
        if ($word === '') {
32 4
            return '';
33
        }
34
35 1566
        foreach ($this->rulesets as $ruleset) {
36 1566
            if ($ruleset->getUninflected()->matches($word)) {
37 626
                return $word;
38
            }
39
        }
40
41 943
        foreach ($this->rulesets as $ruleset) {
42 943
            $inflected = $ruleset->getIrregular()->inflect($word);
43
44 943
            if ($inflected !== $word) {
45 299
                return $inflected;
46
            }
47
        }
48
49 647
        foreach ($this->rulesets as $ruleset) {
50 647
            $inflected = $ruleset->getRegular()->inflect($word);
51
52 647
            if ($inflected !== $word) {
53 634
                return $inflected;
54
            }
55
        }
56
57 13
        return $word;
58
    }
59
}
60