1 | <?php |
||
17 | class ParallelRegex |
||
18 | { |
||
19 | /** @var string[] patterns to match */ |
||
20 | protected $patterns; |
||
21 | /** @var string[] labels for above patterns */ |
||
22 | protected $labels; |
||
23 | /** @var string the compound regex matching all patterns */ |
||
24 | protected $regex; |
||
25 | /** @var bool case sensitive matching? */ |
||
26 | protected $case; |
||
27 | |||
28 | /** |
||
29 | * Constructor. Starts with no patterns. |
||
30 | * |
||
31 | * @param boolean $case True for case sensitive, false |
||
32 | * for insensitive. |
||
33 | */ |
||
34 | public function __construct($case) |
||
41 | |||
42 | /** |
||
43 | * Adds a pattern with an optional label. |
||
44 | * |
||
45 | * @param mixed $pattern Perl style regex. Must be UTF-8 |
||
46 | * encoded. If its a string, the (, ) |
||
47 | * lose their meaning unless they |
||
48 | * form part of a lookahead or |
||
49 | * lookbehind assertation. |
||
50 | * @param bool|string $label Label of regex to be returned |
||
51 | * on a match. Label must be ASCII |
||
52 | */ |
||
53 | public function addPattern($pattern, $label = true) |
||
60 | |||
61 | /** |
||
62 | * Attempts to match all patterns at once against a string. |
||
63 | * |
||
64 | * @param string $subject String to match against. |
||
65 | * @param string $match First matched portion of |
||
66 | * subject. |
||
67 | * @return bool|string False if no match found, label if label exists, true if not |
||
68 | */ |
||
69 | public function apply($subject, &$match) |
||
70 | { |
||
71 | if (count($this->patterns) == 0) { |
||
72 | return false; |
||
73 | } |
||
74 | if (! preg_match($this->getCompoundedRegex(), $subject, $matches)) { |
||
75 | $match = ""; |
||
76 | return false; |
||
77 | } |
||
78 | |||
79 | $match = $matches[0]; |
||
80 | $size = count($matches); |
||
81 | // FIXME this could be made faster by storing the labels as keys in a hashmap |
||
82 | for ($i = 1; $i < $size; $i++) { |
||
83 | if ($matches[$i] && isset($this->labels[$i - 1])) { |
||
84 | return $this->labels[$i - 1]; |
||
85 | } |
||
86 | } |
||
87 | return true; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Attempts to split the string against all patterns at once |
||
92 | * |
||
93 | * @param string $subject String to match against. |
||
94 | * @param array $split The split result: array containing, pre-match, match & post-match strings |
||
95 | * @return boolean True on success. |
||
96 | * |
||
97 | * @author Christopher Smith <[email protected]> |
||
98 | */ |
||
99 | public function split($subject, &$split) |
||
134 | |||
135 | /** |
||
136 | * Compounds the patterns into a single |
||
137 | * regular expression separated with the |
||
138 | * "or" operator. Caches the regex. |
||
139 | * Will automatically escape (, ) and / tokens. |
||
140 | * |
||
141 | * @return null|string |
||
142 | */ |
||
143 | protected function getCompoundedRegex() |
||
194 | |||
195 | /** |
||
196 | * Accessor for perl regex mode flags to use. |
||
197 | * @return string Perl regex flags. |
||
198 | */ |
||
199 | protected function getPerlMatchingFlags() |
||
203 | } |
||
204 |