Completed
Pull Request — master (#90)
by Jonathan
02:59
created

Irregular::inflect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector\Rules;
6
7
use function array_keys;
8
use function implode;
9
use function preg_match;
10
use function strtolower;
11
use function substr;
12
13
class Irregular
14
{
15
    /** @var string[] */
16
    private $irregular;
17
18
    /** @var string|null */
19
    private $regex;
20
21
    /**
22
     * @param string[] $irregular
23
     */
24 526
    public function __construct(array $irregular = [])
25
    {
26 526
        $this->irregular = $irregular;
27 526
    }
28
29
    /**
30
     * @return string[]
31
     */
32 526
    public function getIrregular() : array
33
    {
34 526
        return $this->irregular;
35
    }
36
37 510
    public function inflect(string $word) : ?string
38
    {
39 510
        if (preg_match('/(.*)\\b(' . $this->getRegex() . ')$/i', $word, $regs)) {
40 93
            return $regs[1] . $word[0] . substr($this->irregular[strtolower($regs[2])], 1);
41
        }
42
43 420
        return null;
44
    }
45
46 510
    private function getRegex() : string
47
    {
48 510
        if ($this->regex === null) {
49 510
            $this->regex = '(?:' . implode('|', array_keys($this->irregular)) . ')';
50
        }
51
52 510
        return $this->regex;
53
    }
54
}
55