@@ -17,19 +17,24 @@ class Executor
1717 * @param number
1818 * $offset
1919 *
20+ * @throws \RuntimeException
21+ * @throws \InvalidArgumentException
22+ *
2023 * @return \Mcustiel\PhpSimpleRegex\MatchResult
2124 */
2225 public function getAllMatches ($ pattern , $ subject , $ offset = 0 )
2326 {
2427 $ matches = array ();
25- preg_match_all (
28+ $ result = @ preg_match_all (
2629 $ this ->getPatternByType ($ pattern ),
2730 $ subject ,
2831 $ matches ,
2932 PREG_SET_ORDER | PREG_OFFSET_CAPTURE ,
3033 $ offset
3134 );
3235
36+ $ this ->checkResultIsOkOrThrowException ($ result , $ pattern );
37+
3338 return new MatchResult ($ matches );
3439 }
3540
@@ -41,16 +46,18 @@ public function getAllMatches($pattern, $subject, $offset = 0)
4146 * @param number
4247 * $offset
4348 *
49+ * @throws \RuntimeException
50+ * @throws \InvalidArgumentException
51+ *
4452 * @return \Mcustiel\PhpSimpleRegex\MatchResult|NULL
4553 */
4654 public function getOneMatch ($ pattern , $ subject , $ offset = 0 )
4755 {
4856 $ matches = array ();
4957 $ pattern = $ this ->getPatternByType ($ pattern );
50- if (preg_match ($ pattern , $ subject , $ matches , PREG_SET_ORDER | PREG_OFFSET_CAPTURE , $ offset )) {
51- return (new MatchResult ($ matches ))->getMatchAt (0 );
52- }
53- return null ;
58+ $ result = @preg_match ($ pattern , $ subject , $ matches , PREG_OFFSET_CAPTURE , $ offset );
59+ $ this ->checkResultIsOkOrThrowException ($ result , $ pattern );
60+ return $ result ? new Match ($ matches ) : null ;
5461 }
5562
5663 /**
@@ -61,17 +68,23 @@ public function getOneMatch($pattern, $subject, $offset = 0)
6168 * @param number
6269 * $offset
6370 *
71+ * @throws \RuntimeException
72+ * @throws \InvalidArgumentException
73+ *
6474 * @return boolean
6575 */
6676 public function match ($ pattern , $ subject , $ offset = 0 )
6777 {
68- return (bool ) preg_match (
78+ $ matches = [];
79+ $ result = @preg_match (
6980 $ this ->getPatternByType ($ pattern ),
7081 $ subject ,
71- null ,
72- PREG_PATTERN_ORDER ,
82+ $ matches ,
83+ 0 ,
7384 $ offset
7485 );
86+ $ this ->checkResultIsOkOrThrowException ($ result , $ pattern );
87+ return (boolean ) $ result ;
7588 }
7689
7790 /**
@@ -84,6 +97,9 @@ public function match($pattern, $subject, $offset = 0)
8497 * @param number
8598 * $limit
8699 *
100+ * @throws \RuntimeException
101+ * @throws \InvalidArgumentException
102+ *
87103 * @return \Mcustiel\PhpSimpleRegex\ReplaceResult
88104 */
89105 public function replaceAndCount ($ pattern , $ replacement , $ subject , $ limit = -1 )
@@ -104,6 +120,9 @@ public function replaceAndCount($pattern, $replacement, $subject, $limit = -1)
104120 * @param number
105121 * $limit
106122 *
123+ * @throws \RuntimeException
124+ * @throws \InvalidArgumentException
125+ *
107126 * @return string
108127 */
109128 public function replace ($ pattern , $ replacement , $ subject , $ limit = -1 )
@@ -121,6 +140,9 @@ public function replace($pattern, $replacement, $subject, $limit = -1)
121140 * @param number
122141 * $limit
123142 *
143+ * @throws \RuntimeException
144+ * @throws \InvalidArgumentException
145+ *
124146 * @return string
125147 */
126148 public function replaceCallback ($ pattern , callable $ callback , $ subject , $ limit = -1 )
@@ -137,6 +159,8 @@ public function replaceCallback($pattern, callable $callback, $subject, $limit =
137159 * $subject
138160 * @param number
139161 * $limit
162+ * @throws \RuntimeException
163+ * @throws \InvalidArgumentException
140164 *
141165 * @return \Mcustiel\PhpSimpleRegex\ReplaceResult
142166 */
@@ -151,7 +175,7 @@ public function replaceCallbackAndCount($pattern, callable $callback, $subject,
151175 /**
152176 * @param mixed $pattern
153177 * @throws \InvalidArgumentException
154- * @return unknown
178+ * @return string
155179 */
156180 private function getPatternByType ($ pattern )
157181 {
@@ -171,4 +195,16 @@ private function getPatternByType($pattern)
171195 . ' or an instance of SelvinOrtiz\Utils\Flux\Flux '
172196 );
173197 }
198+
199+ /**
200+ * @param string $pattern
201+ * @param boolean $result
202+ * @throws \RuntimeException
203+ */
204+ private function checkResultIsOkOrThrowException ($ result , $ pattern )
205+ {
206+ if ($ result === false ) {
207+ throw new \RuntimeException ('An error occurred executing the pattern ' . $ pattern );
208+ }
209+ }
174210}
0 commit comments