| 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 |
* @internal |
| 13 |
*/ |
| 14 |
class Matcher |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $pattern; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $subject; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $groups = array(); |
| 30 |
|
| 31 |
private $searchIndex = 0; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param string $pattern |
| 35 |
* @param string $subject |
| 36 |
*/ |
| 37 |
public function __construct($pattern, $subject) |
| 38 |
{ |
| 39 |
$this->pattern = str_replace('/', '\/', $pattern); |
| 40 |
$this->subject = $subject; |
| 41 |
} |
| 42 |
|
| 43 |
protected function doMatch($type = 'find', $offset = 0) |
| 44 |
{ |
| 45 |
$final_pattern = '(?:' . $this->pattern . ')'; |
| 46 |
switch ($type) { |
| 47 |
case 'matches': |
| 48 |
$final_pattern = '^' . $final_pattern . '$'; |
| 49 |
break; |
| 50 |
case 'lookingAt': |
| 51 |
$final_pattern = '^' . $final_pattern; |
| 52 |
break; |
| 53 |
case 'find': |
| 54 |
default: |
| 55 |
// no changes |
| 56 |
break; |
| 57 |
} |
| 58 |
$final_pattern = '/' . $final_pattern . '/ui'; |
| 59 |
|
| 60 |
$search = mb_substr($this->subject, $offset); |
| 61 |
|
| 62 |
$result = preg_match($final_pattern, $search, $groups, PREG_OFFSET_CAPTURE); |
| 63 |
|
| 64 |
if ($result === 1) { |
| 65 |
// Expand $groups into $this->groups, but being multi-byte aware |
| 66 |
|
| 67 |
$positions = array(); |
| 68 |
|
| 69 |
foreach ($groups as $group) { |
| 70 |
$positions[] = array( |
| 71 |
$group[0], |
| 72 |
$offset + mb_strlen(mb_strcut($search, 0, $group[1])) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
$this->groups = $positions; |
| 77 |
} |
| 78 |
|
| 79 |
return ($result === 1); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @return bool |
| 84 |
*/ |
| 85 |
public function matches() |
| 86 |
{ |
| 87 |
return $this->doMatch('matches'); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
public function lookingAt() |
| 94 |
{ |
| 95 |
return $this->doMatch('lookingAt'); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
public function find($offset = null) |
| 102 |
{ |
| 103 |
if ($offset === null) { |
| 104 |
$offset = $this->searchIndex; |
| 105 |
} |
| 106 |
|
| 107 |
// Increment search index for the next time we call this |
| 108 |
$this->searchIndex++; |
| 109 |
return $this->doMatch('find', $offset); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @return int |
| 114 |
*/ |
| 115 |
public function groupCount() |
| 116 |
{ |
| 117 |
if (empty($this->groups)) { |
| 118 |
return null; |
| 119 |
} |
| 120 |
|
| 121 |
return count($this->groups) - 1; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @param int $group |
| 126 |
* @return string |
| 127 |
*/ |
| 128 |
public function group($group = null) |
| 129 |
{ |
| 130 |
if ($group === null) { |
| 131 |
$group = 0; |
| 132 |
} |
| 133 |
return isset($this->groups[$group][0]) ? $this->groups[$group][0] : null; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @param int|null $group |
| 138 |
* @return int |
| 139 |
*/ |
| 140 |
public function end($group = null) |
| 141 |
{ |
| 142 |
if ($group === null) { |
| 143 |
$group = 0; |
| 144 |
} |
| 145 |
if (!isset($this->groups[$group])) { |
| 146 |
return null; |
| 147 |
} |
| 148 |
return $this->groups[$group][1] + mb_strlen($this->groups[$group][0]); |
| 149 |
} |
| 150 |
|
| 151 |
public function start($group = null) |
| 152 |
{ |
| 153 |
if ($group === null) { |
| 154 |
$group = 0; |
| 155 |
} |
| 156 |
if (!isset($this->groups[$group])) { |
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
return $this->groups[$group][1]; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @param string $replacement |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
public function replaceFirst($replacement) |
| 168 |
{ |
| 169 |
return preg_replace('/' . $this->pattern . '/x', $replacement, $this->subject, 1); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @param string $replacement |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
public function replaceAll($replacement) |
| 177 |
{ |
| 178 |
return preg_replace('/' . $this->pattern . '/x', $replacement, $this->subject); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @param string $input |
| 183 |
* @return Matcher |
| 184 |
*/ |
| 185 |
public function reset($input = '') |
| 186 |
{ |
| 187 |
$this->subject = $input; |
| 188 |
|
| 189 |
return $this; |
| 190 |
} |
| 191 |
} |
| 192 |
|