22namespace Mcustiel \PhpSimpleRegex ;
33
44/**
5+ * Facade class that wraps preg_* functions and returns the result of calling this function in an
6+ * object oriented way when necessary.
57 *
68 * @author mcustiel
7- *
89 */
910class Executor
1011{
1112 /**
13+ * Searches for all matches for the given pattern.
1214 *
1315 * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
1416 * $pattern
@@ -39,6 +41,8 @@ public function getAllMatches($pattern, $subject, $offset = 0)
3941 }
4042
4143 /**
44+ * Searches for matches for the given pattern and returns the first match.
45+ *
4246 * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
4347 * $pattern
4448 * @param string
@@ -61,6 +65,8 @@ public function getOneMatch($pattern, $subject, $offset = 0)
6165 }
6266
6367 /**
68+ * Checks weather the string matches the given pattern.
69+ *
6470 * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
6571 * $pattern
6672 * @param string
@@ -88,11 +94,14 @@ public function match($pattern, $subject, $offset = 0)
8894 }
8995
9096 /**
97+ * Replaces all occurrences of $pattern with $replacement in $subject and returns the result and number
98+ * of replacements done.
99+ *
91100 * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
92101 * $pattern
93102 * @param string
94103 * $replacement
95- * @param string
104+ * @param string|array
96105 * $subject
97106 * @param number
98107 * $limit
@@ -105,57 +114,101 @@ public function match($pattern, $subject, $offset = 0)
105114 public function replaceAndCount ($ pattern , $ replacement , $ subject , $ limit = -1 )
106115 {
107116 $ count = 0 ;
108- $ replaced = preg_replace ($ this ->getPatternByType ($ pattern ), $ replacement , $ subject , $ limit , $ count );
117+ $ replaced = @preg_replace (
118+ $ this ->getPatternByType ($ pattern ),
119+ $ replacement ,
120+ $ subject ,
121+ $ limit ,
122+ $ count
123+ );
124+
125+ if ($ replaced === null ) {
126+ throw new \RuntimeException (
127+ 'An error occurred replacing the pattern ' . var_export ($ pattern , true )
128+ );
129+ }
109130
110131 return new ReplaceResult ($ replaced , $ count );
111132 }
112133
113134 /**
135+ * Replaces all occurrences of $pattern with $replacement in $subject and returns the replaced subject.
136+ *
114137 * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
115138 * $pattern
116139 * @param string
117140 * $replacement
118- * @param string
141+ * @param string|array
119142 * $subject
120143 * @param number
121144 * $limit
122145 *
123146 * @throws \RuntimeException
124147 * @throws \InvalidArgumentException
125148 *
126- * @return string
149+ * @return string|array
127150 */
128151 public function replace ($ pattern , $ replacement , $ subject , $ limit = -1 )
129152 {
130- return preg_replace ($ this ->getPatternByType ($ pattern ), $ replacement , $ subject , $ limit );
153+ $ replaced = @preg_replace (
154+ $ this ->getPatternByType ($ pattern ),
155+ $ replacement ,
156+ $ subject ,
157+ $ limit
158+ );
159+ if ($ replaced === null ) {
160+ throw new \RuntimeException (
161+ 'An error occurred replacing the pattern ' . var_export ($ pattern , true )
162+ );
163+ }
164+
165+ return $ replaced ;
131166 }
132167
133168 /**
169+ * Replaces all occurrences of $pattern using $callback function in $subject
170+ * and returns the replaced subject.
171+ *
134172 * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
135173 * $pattern
136174 * @param callable
137175 * $callback
138- * @param string
176+ * @param string|array
139177 * $subject
140178 * @param number
141179 * $limit
142180 *
143181 * @throws \RuntimeException
144182 * @throws \InvalidArgumentException
145183 *
146- * @return string
184+ * @return string|array
147185 */
148186 public function replaceCallback ($ pattern , callable $ callback , $ subject , $ limit = -1 )
149187 {
150- return preg_replace_callback ($ this ->getPatternByType ($ pattern ), $ callback , $ subject , $ limit );
188+ $ replaced = @preg_replace_callback (
189+ $ this ->getPatternByType ($ pattern ),
190+ $ callback ,
191+ $ subject ,
192+ $ limit
193+ );
194+ if ($ replaced === null ) {
195+ throw new \RuntimeException (
196+ 'An error occurred replacing the pattern ' . var_export ($ pattern , true )
197+ );
198+ }
199+
200+ return $ replaced ;
151201 }
152202
153203 /**
204+ * Replaces all occurrences of $pattern using $callback function in $subject
205+ * and returns the replaced subject and the number of replacements done.
206+ *
154207 * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux
155208 * $pattern
156209 * @param callable
157210 * $callback
158- * @param string
211+ * @param string|array
159212 * $subject
160213 * @param number
161214 * $limit
@@ -167,7 +220,13 @@ public function replaceCallback($pattern, callable $callback, $subject, $limit =
167220 public function replaceCallbackAndCount ($ pattern , callable $ callback , $ subject , $ limit = -1 )
168221 {
169222 $ count = 0 ;
170- $ result = preg_replace_callback ($ this ->getPatternByType ($ pattern ), $ callback , $ subject , $ limit );
223+ $ result = preg_replace_callback (
224+ $ this ->getPatternByType ($ pattern ),
225+ $ callback ,
226+ $ subject ,
227+ $ limit ,
228+ $ count
229+ );
171230
172231 return new ReplaceResult ($ result , $ count );
173232 }
0 commit comments