Completed
Push — 7.7.4-changes ( 130305...3e87b4 )
by Joshua
10:55
created

Matcher::doMatch()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 8.8571
ccs 12
cts 12
cp 1
cc 5
eloc 14
nc 8
nop 1
crap 5
1
<?php
2
3
namespace libphonenumber;
4
5
/**
6
 * Matcher for various regex matching
7
 *
8
 * Note that this is NOT the same as google's java PhoneNumberMatcher class.
9
 * This class is a minimal port of java's built-in matcher class, whereas PhoneNumberMatcher
10
 * is designed to recognize phone numbers embedded in any text.
11
 */
12
class Matcher
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $pattern;
18
19
    /**
20
     * @var string
21
     */
22
    protected $subject;
23
24
    /**
25
     * @var array
26
     */
27
    protected $groups = array();
28
29
    /**
30
     * @param string $pattern
31
     * @param string $subject
32
     */
33 2838
    public function __construct($pattern, $subject)
34
    {
35 2838
        $this->pattern = str_replace('/', '\/', $pattern);
36 2838
        $this->subject = $subject;
37 2838
    }
38
39 2838
    protected function doMatch($type = 'find')
40
    {
41 2838
        $final_pattern = '(?:' . $this->pattern . ')';
42
        switch ($type) {
43 2838
            case 'matches':
44 2831
                $final_pattern = '^' . $final_pattern . '$';
45 2831
                break;
46 2796
            case 'lookingAt':
47 2791
                $final_pattern = '^' . $final_pattern;
48 2791
                break;
49 2778
            case 'find':
50
            default:
51
                // no changes
52 2778
                break;
53
        }
54 2838
        $final_pattern = '/' . $final_pattern . '/x';
55 2838
        return (preg_match($final_pattern, $this->subject, $this->groups, PREG_OFFSET_CAPTURE) == 1) ? true : false;
56
    }
57
58
    /**
59
     * @return bool
60
     */
61 2831
    public function matches()
62
    {
63 2831
        return $this->doMatch('matches');
64
    }
65
66
    /**
67
     * @return bool
68
     */
69 2791
    public function lookingAt()
70
    {
71 2791
        return $this->doMatch('lookingAt');
72
    }
73
74
    /**
75
     * @return bool
76
     */
77 2778
    public function find()
78
    {
79 2778
        return $this->doMatch('find');
80
    }
81
82
    /**
83
     * @return int
84
     */
85 71
    public function groupCount()
86
    {
87 71
        if (empty($this->groups)) {
88
            return null;
89
        } else {
90 71
            return count($this->groups) - 1;
91
        }
92
    }
93
94
    /**
95
     * @param int $group
96
     * @return string
97
     */
98 33
    public function group($group = null)
99
    {
100 33
        if (!isset($group) || $group === null) {
101
            $group = 0;
102
        }
103 33
        return (isset($this->groups[$group][0])) ? $this->groups[$group][0] : null;
104
    }
105
106
    /**
107
     * @param int|null $group
108
     * @return int
109
     */
110 71
    public function end($group = null)
111
    {
112 71
        if (!isset($group) || $group === null) {
113 71
            $group = 0;
114
        }
115 71
        if (!isset($this->groups[$group])) {
116
            return null;
117
        }
118 71
        return $this->groups[$group][1] + strlen($this->groups[$group][0]);
119
    }
120
121 3
    public function start($group = null)
122
    {
123 3
        if (isset($group) || $group === null) {
124 3
            $group = 0;
125
        }
126 3
        if (!isset($this->groups[$group])) {
127
            return null;
128
        }
129
130 3
        return $this->groups[$group][1];
131
    }
132
133
    /**
134
     * @param string $replacement
135
     * @return string
136
     */
137 30
    public function replaceFirst($replacement)
138
    {
139 30
        return preg_replace('/' . $this->pattern . '/x', $replacement, $this->subject, 1);
140
    }
141
142
    /**
143
     * @param string $replacement
144
     * @return string
145
     */
146 42
    public function replaceAll($replacement)
147
    {
148 42
        return preg_replace('/' . $this->pattern . '/x', $replacement, $this->subject);
149
    }
150
151
    /**
152
     * @param string $input
153
     * @return Matcher
154
     */
155 4
    public function reset($input = "")
156
    {
157 4
        $this->subject = $input;
158
159 4
        return $this;
160
    }
161
}
162