1 | <?php |
||
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 | 2874 | public function __construct($pattern, $subject) |
|
38 | |||
39 | 2874 | protected function doMatch($type = 'find') |
|
40 | { |
||
41 | 2874 | $final_pattern = '(?:' . $this->pattern . ')'; |
|
42 | switch ($type) { |
||
43 | 2874 | case 'matches': |
|
44 | 2869 | $final_pattern = '^' . $final_pattern . '$'; |
|
45 | 2869 | break; |
|
46 | 2832 | case 'lookingAt': |
|
47 | 2827 | $final_pattern = '^' . $final_pattern; |
|
48 | 2827 | break; |
|
49 | 2813 | case 'find': |
|
50 | 2813 | default: |
|
51 | // no changes |
||
52 | 2813 | break; |
|
53 | } |
||
54 | 2874 | $final_pattern = '/' . $final_pattern . '/x'; |
|
55 | 2874 | return (preg_match($final_pattern, $this->subject, $this->groups, PREG_OFFSET_CAPTURE) == 1) ? true : false; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return bool |
||
60 | */ |
||
61 | 2869 | public function matches() |
|
65 | |||
66 | /** |
||
67 | * @return bool |
||
68 | */ |
||
69 | 2827 | public function lookingAt() |
|
73 | |||
74 | /** |
||
75 | * @return bool |
||
76 | */ |
||
77 | 2813 | public function find() |
|
81 | |||
82 | /** |
||
83 | * @return int |
||
84 | */ |
||
85 | 76 | public function groupCount() |
|
93 | |||
94 | /** |
||
95 | * @param int $group |
||
96 | * @return string |
||
97 | */ |
||
98 | 34 | public function group($group = null) |
|
99 | { |
||
100 | 34 | if (!isset($group) || $group === null) { |
|
101 | $group = 0; |
||
102 | } |
||
103 | 34 | return (isset($this->groups[$group][0])) ? $this->groups[$group][0] : null; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param int|null $group |
||
108 | * @return int |
||
109 | */ |
||
110 | 76 | public function end($group = null) |
|
111 | { |
||
112 | 76 | if (!isset($group) || $group === null) { |
|
113 | 76 | $group = 0; |
|
114 | 76 | } |
|
115 | 76 | if (!isset($this->groups[$group])) { |
|
116 | return null; |
||
117 | } |
||
118 | 76 | 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 | 3 | } |
|
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 | 31 | public function replaceFirst($replacement) |
|
141 | |||
142 | /** |
||
143 | * @param string $replacement |
||
144 | * @return string |
||
145 | */ |
||
146 | 43 | public function replaceAll($replacement) |
|
150 | |||
151 | /** |
||
152 | * @param string $input |
||
153 | * @return Matcher |
||
154 | */ |
||
155 | 5 | public function reset($input = "") |
|
161 | } |
||
162 |