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

Irregular   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getRegex() 0 7 2
A getIrregular() 0 3 1
A inflect() 0 7 2
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