Passed
Pull Request — master (#97)
by Jonathan
09:17
created

Pattern::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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