Passed
Pull Request — master (#97)
by Jonathan
02:24
created

Substitutions::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector\Rules;
6
7
use Doctrine\Inflector\WordInflector;
8
use function mb_substr;
9
use function strtolower;
10
11
class Substitutions implements WordInflector
12
{
13
    /** @var Substitution[] */
14
    private $substitutions;
15
16 524
    public function __construct(Substitution ...$substitutions)
17
    {
18 524
        foreach ($substitutions as $substitution) {
19 524
            $this->substitutions[$substitution->getFrom()->getWord()] = $substitution;
20
        }
21 524
    }
22
23 523
    public function getFlippedSubstitutions() : Substitutions
24
    {
25 523
        $substitutions = [];
26
27 523
        foreach ($this->substitutions as $substitution) {
28 523
            $substitutions[] = new Substitution(
29 523
                $substitution->getTo(),
30 523
                $substitution->getFrom()
31
            );
32
        }
33
34 523
        return new Substitutions(...$substitutions);
35
    }
36
37
38 265
    public function inflect(string $word) : string
39
    {
40 265
        $lowerWord = strtolower($word);
41
42 265
        if (isset($this->substitutions[$lowerWord])) {
43 104
            return mb_substr($word, 0, 1) . mb_substr($this->substitutions[$lowerWord]->getTo()->getWord(), 1);
44
        }
45
46 161
        return $word;
47
    }
48
}
49