11<?php
2+ /**
3+ * This file is part of php-simple-regex.
4+ *
5+ * php-simple-regex is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU Lesser General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * (at your option) any later version.
9+ *
10+ * php-simple-regex is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with php-simple-regex. If not, see <http://www.gnu.org/licenses/>.
17+ */
218namespace Mcustiel \PhpSimpleRegex ;
319
420/**
@@ -12,7 +28,7 @@ class Executor
1228 /**
1329 * Searches for all matches for the given pattern.
1430 *
15- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
31+ * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
1632 * $pattern
1733 * @param string
1834 * $subject
@@ -43,7 +59,7 @@ public function getAllMatches($pattern, $subject, $offset = 0)
4359 /**
4460 * Searches for matches for the given pattern and returns the first match.
4561 *
46- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
62+ * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
4763 * $pattern
4864 * @param string
4965 * $subject
@@ -67,7 +83,7 @@ public function getOneMatch($pattern, $subject, $offset = 0)
6783 /**
6884 * Checks weather the string matches the given pattern.
6985 *
70- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
86+ * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
7187 * $pattern
7288 * @param string
7389 * $subject
@@ -96,8 +112,9 @@ public function match($pattern, $subject, $offset = 0)
96112 /**
97113 * Replaces all occurrences of $pattern with $replacement in $subject and returns the result and number
98114 * of replacements done.
115+ * See @link http://php.net/manual/en/function.preg-replace.php
99116 *
100- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
117+ * @param string|array| \VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
101118 * $pattern
102119 * @param string
103120 * $replacement
@@ -115,7 +132,7 @@ public function replaceAndCount($pattern, $replacement, $subject, $limit = -1)
115132 {
116133 $ count = 0 ;
117134 $ replaced = @preg_replace (
118- $ this ->getPatternByType ($ pattern ),
135+ $ this ->getPatternForReplace ($ pattern ),
119136 $ replacement ,
120137 $ subject ,
121138 $ limit ,
@@ -131,10 +148,44 @@ public function replaceAndCount($pattern, $replacement, $subject, $limit = -1)
131148 return new ReplaceResult ($ replaced , $ count );
132149 }
133150
151+ /**
152+ * Replaces all occurrences of $pattern with $replacement in $subject and returns the result and number
153+ * of replacements done. Result will return only the modified subjects.
154+ * See @link http://php.net/manual/en/function.preg-filter.php
155+ *
156+ * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
157+ * $pattern
158+ * @param string
159+ * $replacement
160+ * @param string|array
161+ * $subject
162+ * @param number
163+ * $limit
164+ *
165+ * @throws \RuntimeException
166+ * @throws \InvalidArgumentException
167+ *
168+ * @return \Mcustiel\PhpSimpleRegex\ReplaceResult
169+ */
170+ public function replaceAndCountAndOnlyGetChanged ($ pattern , $ replacement , $ subject , $ limit = -1 )
171+ {
172+ $ count = 0 ;
173+ // I must display error here, since I couldn't find a way to detect if error happened
174+ $ replaced = preg_filter (
175+ $ this ->getPatternForReplace ($ pattern ),
176+ $ replacement ,
177+ $ subject ,
178+ $ limit ,
179+ $ count
180+ );
181+
182+ return new ReplaceResult ($ replaced , $ count );
183+ }
184+
134185 /**
135186 * Replaces all occurrences of $pattern with $replacement in $subject and returns the replaced subject.
136187 *
137- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
188+ * @param string|array| \VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
138189 * $pattern
139190 * @param string
140191 * $replacement
@@ -151,7 +202,7 @@ public function replaceAndCount($pattern, $replacement, $subject, $limit = -1)
151202 public function replace ($ pattern , $ replacement , $ subject , $ limit = -1 )
152203 {
153204 $ replaced = @preg_replace (
154- $ this ->getPatternByType ($ pattern ),
205+ $ this ->getPatternForReplace ($ pattern ),
155206 $ replacement ,
156207 $ subject ,
157208 $ limit
@@ -165,11 +216,39 @@ public function replace($pattern, $replacement, $subject, $limit = -1)
165216 return $ replaced ;
166217 }
167218
219+ /**
220+ * Replaces all occurrences of $pattern with $replacement in $subject and returns the replaced subject.
221+ *
222+ * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
223+ * $pattern
224+ * @param string
225+ * $replacement
226+ * @param string|array
227+ * $subject
228+ * @param number
229+ * $limit
230+ *
231+ * @throws \RuntimeException
232+ * @throws \InvalidArgumentException
233+ *
234+ * @return string|array
235+ */
236+ public function replaceAndOnlyGetChanged ($ pattern , $ replacement , $ subject , $ limit = -1 )
237+ {
238+ // I must display error here, since I couldn't find a way to detect if error happened
239+ return preg_filter (
240+ $ this ->getPatternForReplace ($ pattern ),
241+ $ replacement ,
242+ $ subject ,
243+ $ limit
244+ );
245+ }
246+
168247 /**
169248 * Replaces all occurrences of $pattern using $callback function in $subject
170249 * and returns the replaced subject.
171250 *
172- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
251+ * @param string|array| \VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
173252 * $pattern
174253 * @param callable
175254 * $callback
@@ -186,7 +265,7 @@ public function replace($pattern, $replacement, $subject, $limit = -1)
186265 public function replaceCallback ($ pattern , callable $ callback , $ subject , $ limit = -1 )
187266 {
188267 $ replaced = @preg_replace_callback (
189- $ this ->getPatternByType ($ pattern ),
268+ $ this ->getPatternForReplace ($ pattern ),
190269 $ callback ,
191270 $ subject ,
192271 $ limit
@@ -204,7 +283,7 @@ public function replaceCallback($pattern, callable $callback, $subject, $limit =
204283 * Replaces all occurrences of $pattern using $callback function in $subject
205284 * and returns the replaced subject and the number of replacements done.
206285 *
207- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
286+ * @param string|array| \VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
208287 * $pattern
209288 * @param callable
210289 * $callback
@@ -221,7 +300,7 @@ public function replaceCallbackAndCount($pattern, callable $callback, $subject,
221300 {
222301 $ count = 0 ;
223302 $ result = preg_replace_callback (
224- $ this ->getPatternByType ($ pattern ),
303+ $ this ->getPatternForReplace ($ pattern ),
225304 $ callback ,
226305 $ subject ,
227306 $ limit ,
@@ -231,6 +310,20 @@ public function replaceCallbackAndCount($pattern, callable $callback, $subject,
231310 return new ReplaceResult ($ result , $ count );
232311 }
233312
313+ /**
314+ * @param mixed $pattern
315+ * @throws \InvalidArgumentException
316+ * @return string|array
317+ */
318+ private function getPatternForReplace ($ pattern )
319+ {
320+ if (is_array ($ pattern )) {
321+ return $ pattern ;
322+ }
323+
324+ return $ this ->getPatternByType ($ pattern );
325+ }
326+
234327 /**
235328 * @param mixed $pattern
236329 * @throws \InvalidArgumentException
@@ -244,14 +337,16 @@ private function getPatternByType($pattern)
244337 if (is_object ($ pattern )) {
245338 $ class = get_class ($ pattern );
246339 if ($ class == 'SelvinOrtiz\Utils\Flux\Flux '
247- || $ class == 'VerbalExpressions\PHPVerbalExpressions\VerbalExpressions ' ) {
340+ || $ class == 'VerbalExpressions\PHPVerbalExpressions\VerbalExpressions '
341+ || $ class == 'MarkWilson\VerbalExpression ' ) {
248342 return $ pattern ->__toString ();
249343 }
250344 }
251345 throw new \InvalidArgumentException (
252346 'Pattern must be a string, an instance of '
253347 . 'VerbalExpressions\PHPVerbalExpressions\VerbalExpressions '
254- . ' or an instance of SelvinOrtiz\Utils\Flux\Flux '
348+ . ', SelvinOrtiz\Utils\Flux\Flux '
349+ . ' or MarkWilson\VerbalExpression '
255350 );
256351 }
257352
@@ -263,7 +358,9 @@ private function getPatternByType($pattern)
263358 private function checkResultIsOkOrThrowException ($ result , $ pattern )
264359 {
265360 if ($ result === false ) {
266- throw new \RuntimeException ('An error occurred executing the pattern ' . $ pattern );
361+ throw new \RuntimeException (
362+ 'An error occurred executing the pattern ' . var_export ($ pattern , true )
363+ );
267364 }
268365 }
269366}
0 commit comments