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

Patterns::getRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector\Rules;
6
7
use function array_map;
8
use function implode;
9
use function preg_match;
10
11
class Patterns
12
{
13
    /** @var Pattern[] */
14
    private $patterns;
15
16
    /** @var string|null */
17
    private $regex;
18
19 530
    public function __construct(Pattern ...$patterns)
20
    {
21 530
        $this->patterns = $patterns;
22 530
    }
23
24 512
    public function isUninflected(string $word) : bool
25
    {
26 512
        return preg_match($this->getRegex(), $word, $regs) === 1;
27
    }
28
29 512
    private function getRegex() : string
30
    {
31 512
        if ($this->regex === null) {
32
            $patterns = array_map(function (Pattern $word) : string {
33 512
                return $word->getPattern();
34 512
            }, $this->patterns);
35
36 512
            $this->regex = '/^(?:' . implode('|', $patterns) . ')$/i';
37
        }
38
39 512
        return $this->regex;
40
    }
41
}
42