1717 */
1818namespace Mcustiel \PhpSimpleRegex ;
1919
20+ use Mcustiel \PhpSimpleRegex \PregFunctions \GrepFunctions ;
21+ use Mcustiel \PhpSimpleRegex \PregFunctions \MatchFunctions ;
22+ use Mcustiel \PhpSimpleRegex \PregFunctions \ReplaceFunctions ;
23+ use Mcustiel \PhpSimpleRegex \PregFunctions \SplitFunctions ;
24+
2025/**
2126 * Facade class that wraps preg_* functions and returns the result of calling this function in an
2227 * object oriented way when necessary.
2530 */
2631class Executor
2732{
28- /**
29- * Searches for all matches for the given pattern.
30- *
31- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
32- * $pattern
33- * @param string
34- * $subject
35- * @param number
36- * $offset
37- *
38- * @throws \RuntimeException
39- * @throws \InvalidArgumentException
40- *
41- * @return \Mcustiel\PhpSimpleRegex\MatchResult
42- */
43- public function getAllMatches ($ pattern , $ subject , $ offset = 0 )
44- {
45- $ matches = array ();
46- $ result = @preg_match_all (
47- $ this ->getPatternByType ($ pattern ),
48- $ subject ,
49- $ matches ,
50- PREG_SET_ORDER | PREG_OFFSET_CAPTURE ,
51- $ offset
52- );
53-
54- $ this ->checkResultIsOkOrThrowException ($ result , $ pattern );
55-
56- return new MatchResult ($ matches );
57- }
58-
59- /**
60- * Searches for matches for the given pattern and returns the first match.
61- *
62- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
63- * $pattern
64- * @param string
65- * $subject
66- * @param number
67- * $offset
68- *
69- * @throws \RuntimeException
70- * @throws \InvalidArgumentException
71- *
72- * @return \Mcustiel\PhpSimpleRegex\Match|NULL
73- */
74- public function getOneMatch ($ pattern , $ subject , $ offset = 0 )
75- {
76- $ matches = array ();
77- $ pattern = $ this ->getPatternByType ($ pattern );
78- $ result = @preg_match ($ pattern , $ subject , $ matches , PREG_OFFSET_CAPTURE , $ offset );
79- $ this ->checkResultIsOkOrThrowException ($ result , $ pattern );
80- return $ result ? new Match ($ matches ) : null ;
81- }
82-
83- /**
84- * Checks weather the string matches the given pattern.
85- *
86- * @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
87- * $pattern
88- * @param string
89- * $subject
90- * @param number
91- * $offset
92- *
93- * @throws \RuntimeException
94- * @throws \InvalidArgumentException
95- *
96- * @return boolean
97- */
98- public function match ($ pattern , $ subject , $ offset = 0 )
99- {
100- $ matches = [];
101- $ result = @preg_match (
102- $ this ->getPatternByType ($ pattern ),
103- $ subject ,
104- $ matches ,
105- 0 ,
106- $ offset
107- );
108- $ this ->checkResultIsOkOrThrowException ($ result , $ pattern );
109- return (boolean ) $ result ;
110- }
111-
112- /**
113- * Replaces all occurrences of $pattern with $replacement in $subject and returns the result and number
114- * of replacements done.
115- * See @link http://php.net/manual/en/function.preg-replace.php
116- *
117- * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
118- * $pattern
119- * @param string
120- * $replacement
121- * @param string|array
122- * $subject
123- * @param number
124- * $limit
125- *
126- * @throws \RuntimeException
127- * @throws \InvalidArgumentException
128- *
129- * @return \Mcustiel\PhpSimpleRegex\ReplaceResult
130- */
131- public function replaceAndCount ($ pattern , $ replacement , $ subject , $ limit = -1 )
132- {
133- $ count = 0 ;
134- $ replaced = @preg_replace (
135- $ this ->getPatternForReplace ($ pattern ),
136- $ replacement ,
137- $ subject ,
138- $ limit ,
139- $ count
140- );
141-
142- if ($ replaced === null ) {
143- throw new \RuntimeException (
144- 'An error occurred replacing the pattern ' . var_export ($ pattern , true )
145- );
146- }
147-
148- return new ReplaceResult ($ replaced , $ count );
149- }
150-
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-
185- /**
186- * Replaces all occurrences of $pattern with $replacement in $subject and returns the replaced subject.
187- *
188- * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
189- * $pattern
190- * @param string
191- * $replacement
192- * @param string|array
193- * $subject
194- * @param number
195- * $limit
196- *
197- * @throws \RuntimeException
198- * @throws \InvalidArgumentException
199- *
200- * @return string|array
201- */
202- public function replace ($ pattern , $ replacement , $ subject , $ limit = -1 )
203- {
204- $ replaced = @preg_replace (
205- $ this ->getPatternForReplace ($ pattern ),
206- $ replacement ,
207- $ subject ,
208- $ limit
209- );
210- if ($ replaced === null ) {
211- throw new \RuntimeException (
212- 'An error occurred replacing the pattern ' . var_export ($ pattern , true )
213- );
214- }
215-
216- return $ replaced ;
217- }
218-
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-
247- /**
248- * Replaces all occurrences of $pattern using $callback function in $subject
249- * and returns the replaced subject.
250- *
251- * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
252- * $pattern
253- * @param callable
254- * $callback
255- * @param string|array
256- * $subject
257- * @param number
258- * $limit
259- *
260- * @throws \RuntimeException
261- * @throws \InvalidArgumentException
262- *
263- * @return string|array
264- */
265- public function replaceCallback ($ pattern , callable $ callback , $ subject , $ limit = -1 )
266- {
267- $ replaced = @preg_replace_callback (
268- $ this ->getPatternForReplace ($ pattern ),
269- $ callback ,
270- $ subject ,
271- $ limit
272- );
273- if ($ replaced === null ) {
274- throw new \RuntimeException (
275- 'An error occurred replacing the pattern ' . var_export ($ pattern , true )
276- );
277- }
278-
279- return $ replaced ;
280- }
281-
282- /**
283- * Replaces all occurrences of $pattern using $callback function in $subject
284- * and returns the replaced subject and the number of replacements done.
285- *
286- * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
287- * $pattern
288- * @param callable
289- * $callback
290- * @param string|array
291- * $subject
292- * @param number
293- * $limit
294- * @throws \RuntimeException
295- * @throws \InvalidArgumentException
296- *
297- * @return \Mcustiel\PhpSimpleRegex\ReplaceResult
298- */
299- public function replaceCallbackAndCount ($ pattern , callable $ callback , $ subject , $ limit = -1 )
300- {
301- $ count = 0 ;
302- $ result = preg_replace_callback (
303- $ this ->getPatternForReplace ($ pattern ),
304- $ callback ,
305- $ subject ,
306- $ limit ,
307- $ count
308- );
309-
310- return new ReplaceResult ($ result , $ count );
311- }
33+ use GrepFunctions, MatchFunctions, ReplaceFunctions, SplitFunctions;
31234
31335 /**
31436 * @param mixed $pattern
@@ -351,8 +73,9 @@ private function getPatternByType($pattern)
35173 }
35274
35375 /**
76+ * @param mixed $result
35477 * @param string $pattern
355- * @param boolean $result
78+ *
35679 * @throws \RuntimeException
35780 */
35881 private function checkResultIsOkOrThrowException ($ result , $ pattern )
@@ -363,4 +86,19 @@ private function checkResultIsOkOrThrowException($result, $pattern)
36386 );
36487 }
36588 }
89+
90+ /**
91+ * @param mixed $result
92+ * @param string $pattern
93+ *
94+ * @throws \RuntimeException
95+ */
96+ private function checkResultIsOkForReplaceOrThrowException ($ result , $ pattern )
97+ {
98+ if ($ result === null ) {
99+ throw new \RuntimeException (
100+ 'An error occurred replacing the pattern ' . var_export ($ pattern , true )
101+ );
102+ }
103+ }
366104}
0 commit comments