GenericLanguageInflectorFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector;
6
7
use Doctrine\Inflector\Rules\Ruleset;
8
use function array_unshift;
9
10
abstract class GenericLanguageInflectorFactory implements LanguageInflectorFactory
11
{
12
    /** @var Ruleset[] */
13
    private $singularRulesets = [];
14
15
    /** @var Ruleset[] */
16
    private $pluralRulesets = [];
17
18 1105
    final public function __construct()
19
    {
20 1105
        $this->singularRulesets[] = $this->getSingularRuleset();
21 1105
        $this->pluralRulesets[]   = $this->getPluralRuleset();
22 1105
    }
23
24 1098
    final public function build() : Inflector
25
    {
26 1098
        return new Inflector(
27 1098
            new CachedWordInflector(new RulesetInflector(
28 1098
                ...$this->singularRulesets
29
            )),
30 1098
            new CachedWordInflector(new RulesetInflector(
31 1098
                ...$this->pluralRulesets
32
            ))
33
        );
34
    }
35
36
    final public function withSingularRules(?Ruleset $singularRules, bool $reset = false) : LanguageInflectorFactory
37
    {
38
        if ($reset) {
39
            $this->singularRulesets = [];
40
        }
41
42
        if ($singularRules instanceof Ruleset) {
43
            array_unshift($this->singularRulesets, $singularRules);
44
        }
45
46
        return $this;
47
    }
48
49
    final public function withPluralRules(?Ruleset $pluralRules, bool $reset = false) : LanguageInflectorFactory
50
    {
51
        if ($reset) {
52
            $this->pluralRulesets = [];
53
        }
54
55
        if ($pluralRules instanceof Ruleset) {
56
            array_unshift($this->pluralRulesets, $pluralRules);
57
        }
58
59
        return $this;
60
    }
61
62
    abstract protected function getSingularRuleset() : Ruleset;
63
64
    abstract protected function getPluralRuleset() : Ruleset;
65
}
66