Skip to content

Commit 197a2a4

Browse files
committed
More fixes and more tests.
1 parent 322824b commit 197a2a4

File tree

5 files changed

+310
-16
lines changed

5 files changed

+310
-16
lines changed

src/Executor.php

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
namespace 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
*/
910
class 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
}

src/Match.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
namespace Mcustiel\PhpSimpleRegex;
33

44
/**
5+
* Represents a match in a collection of matches obtained of checking a string against
6+
* a regular expression.
57
*
68
* @author mcustiel
7-
*
89
*/
910
class Match
1011
{
@@ -15,6 +16,7 @@ class Match
1516
protected $element;
1617

1718
/**
19+
* Class constructor.
1820
*
1921
* @param array $responseElement
2022
*/
@@ -24,6 +26,7 @@ public function __construct(array $responseElement)
2426
}
2527

2628
/**
29+
* Returns the full match string that matches the whole pattern. Null if no match found.
2730
*
2831
* @return null|string
2932
*/
@@ -33,6 +36,9 @@ public function getFullMatch()
3336
}
3437

3538
/**
39+
* Returns the offset of the string that matches the whole pattern in the subject
40+
* string, null if no match found.
41+
*
3642
* @return null|int
3743
*/
3844
public function getOffset()
@@ -41,8 +47,10 @@ public function getOffset()
4147
}
4248

4349
/**
50+
* Returns the string matching a subpattern of a pattern, giving it's index.
4451
*
4552
* @param int $index
53+
*
4654
* @throws \OutOfBoundsException
4755
* @return string
4856
*/
@@ -54,7 +62,10 @@ public function getSubMatchAt($index)
5462
}
5563

5664
/**
65+
* Returns the offset of the string matching a subpattern of a pattern, giving it's index.
66+
*
5767
* @param int $index
68+
*
5869
* @throws \OutOfBoundsException
5970
* @return int
6071
*/
@@ -65,6 +76,8 @@ public function getSubmatchOffsetAt($index)
6576
}
6677

6778
/**
79+
* Validates the index of a subpattern.
80+
*
6881
* @param int $index
6982
* @throws \OutOfBoundsException
7083
*/

src/MatchResult.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,34 @@
22
namespace Mcustiel\PhpSimpleRegex;
33

44
/**
5+
* Represent the result of a search through a string using a regular expression.
6+
* This
7+
* is an iterable collection of matches.
58
*
69
* @author mcustiel
7-
*
810
*/
911
class MatchResult implements \Iterator
1012
{
1113
/**
14+
*
1215
* @var array
1316
*/
1417
protected $response;
1518
/**
19+
*
1620
* @var int
1721
*/
1822
private $current;
1923
/**
24+
*
2025
* @var int
2126
*/
2227
private $count;
2328

2429
/**
30+
* Class contructor.
2531
*
26-
* @param array $regex
32+
* @param array $regexResponse
2733
*/
2834
public function __construct(array $regexResponse)
2935
{
@@ -33,6 +39,7 @@ public function __construct(array $regexResponse)
3339
}
3440

3541
/**
42+
* Returns the number of matches found in the subject string for the executed regex.
3643
*
3744
* @return int
3845
*/
@@ -42,21 +49,26 @@ public function getMatchesCount()
4249
}
4350

4451
/**
52+
* Returns a match for the executed pattern using it's order in the string.
4553
*
4654
* @param int $index
55+
*
4756
* @throws \OutOfBoundsException
4857
* @return \Mcustiel\PhpSimpleRegex\Match
4958
*/
5059
public function getMatchAt($index)
5160
{
52-
if (!isset($this->response[$index])) {
61+
if (! isset($this->response[$index])) {
5362
throw new \OutOfBoundsException('Trying to access a match at an invalid index');
5463
}
5564

5665
return new Match($this->response[$index]);
5766
}
5867

68+
// \Iterable implementation
69+
5970
/**
71+
*
6072
* @return \Mcustiel\PhpSimpleRegex\Match
6173
*/
6274
public function current()

src/ReplaceResult.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22
namespace Mcustiel\PhpSimpleRegex;
33

44
/**
5+
* Represents the result of calling a preg_replace_* function and requesting the count of replacements.
56
*
67
* @author mcustiel
78
* @codeCoverageIgnore
89
*/
910
class ReplaceResult
1011
{
12+
/**
13+
* @var string
14+
*/
1115
private $result;
16+
/**
17+
* @var integer
18+
*/
1219
private $replacements;
1320

1421
/**
22+
* Class constructor.
1523
*
1624
* @param string $result
1725
* @param integer $replacements
@@ -23,13 +31,15 @@ public function __construct($result, $replacements)
2331
}
2432

2533
/**
34+
* @return string
2635
*/
2736
public function getResult()
2837
{
2938
return $this->result;
3039
}
3140

3241
/**
42+
* @return number
3343
*/
3444
public function getReplacements()
3545
{

0 commit comments

Comments
 (0)