Pattern::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace GettextParser;
3
4
class Pattern
5
{
6
    /**
7
     * regular expression
8
     * @var string
9
     */
10
    private $pattern = '';
11
12
    /**
13
     * @var bool
14
     */
15
    private $isPlural = false;
16
17
    /**
18
     * @param $pattern
19
     * @param bool $isPlural
20
     */
21
    public function __construct($pattern, $isPlural = false)
22
    {
23
        $this->pattern = $pattern;
24
        $this->isPlural = $isPlural;
25
    }
26
27
    /**
28
     * @param $data
29
     * @return array|bool
30
     */
31
    public function match($data)
32
    {
33
        $result = preg_match_all($this->getPattern(), $data, $matches, PREG_SET_ORDER);
34
        if ($result === false || $result === 0) {
35
            return false;
36
        }
37
38
        $results = array();
39
        foreach ($matches as $match) {
40
            $results[] = $this->processMatch($match);
41
        }
42
43
        return $results;
44
    }
45
46
    /**
47
     * @param array $match
48
     *
49
     * @return array|string
50
     */
51
    protected function processMatch($match)
52
    {
53
        if ($this->isPlural()) {
54
            $startPos = 1;
55
            $length = count($match) - $startPos - 1;
56
57
            return array_slice($match, $startPos, $length);
58
        }
59
60
        return $match[1];
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getPattern()
67
    {
68
        return $this->pattern;
69
    }
70
71
    /**
72
     * @return boolean
73
     */
74
    public function isPlural()
75
    {
76
        return $this->isPlural;
77
    }
78
}
79