Skip to content

Commit e2b3efb

Browse files
committed
Added support for more external libs. Added preg_filter without error detection.
1 parent 2d5676a commit e2b3efb

File tree

12 files changed

+380
-36
lines changed

12 files changed

+380
-36
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,66 @@ try {
9090
}
9191
```
9292

93+
#### match:
94+
95+
```php
96+
try {
97+
if ($regexFacade->match('/\d+/', 'ab12cd34ef56')) {
98+
echo 'String matches pattern.'. PHP_EOL;
99+
} else {
100+
echo 'String does not match pattern.'. PHP_EOL;
101+
}
102+
} catch (\Exception $e) {
103+
echo 'An error occurred executing match';
104+
}
105+
```
106+
93107
#### replaceAndCount:
94108

95109
```php
96110
try {
111+
// Subject can also be an array.
97112
$result = $this->executor->replaceAndCount('/\d+/', 'potato', 'ab12cd34ef56');
98113
echo 'Number of replacements: ' . $result->getReplacements() . PHP_EOL;
99114
echo 'Replaced string: ' . $result->getResult() . PHP_EOL;
100115
} catch (\Exception $e) {
101116
echo 'An error occurred executing replaceAndCount';
102117
}
103118
```
119+
120+
#### replace:
121+
122+
```php
123+
try {
124+
// Subject can also be a string.
125+
$result = $this->executor->replaceAndCount('/\d+/', 'potato', ['ab12cd34ef56', 'ab12cd78ef90']);
126+
echo 'Replaced strings: ' . print_r($result->getResult(), true) . PHP_EOL;
127+
} catch (\Exception $e) {
128+
echo 'An error occurred executing replace';
129+
}
130+
```
131+
132+
#### replaceCallback:
133+
134+
```php
135+
try {
136+
// Subject can also be an array.
137+
$result = $this->executor->replaceCallback('/\d+/', function () { return 'potato'; }, 'ab12cd34ef56');
138+
echo 'Replaced string: ' . $result->getResult() . PHP_EOL;
139+
} catch (\Exception $e) {
140+
echo 'An error occurred executing replaceCallback';
141+
}
142+
```
143+
144+
#### replaceCallbackAndCount:
145+
146+
```php
147+
try {
148+
// Subject can also be an array.
149+
$result = $this->executor->replaceCallback('/\d+/', function () { return 'potato'; }, 'ab12cd34ef56');
150+
echo 'Number of replacements: ' . $result->getReplacements() . PHP_EOL;
151+
echo 'Replaced string: ' . $result->getResult() . PHP_EOL;
152+
} catch (\Exception $e) {
153+
echo 'An error occurred executing replaceCallbackAndCount';
154+
}
155+
```

build.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,6 @@
130130
<!-- ============================================ -->
131131
<target name="unittests">
132132
<echo msg="Running unit tests..." />
133-
<phpunit>
134-
<formatter type="clover" toDir="build/reports/coverage" />
135-
<formatter type="xml" outfile="build/reports/phpunit.xml" />
136-
<formatter type="summary" outfile="build/reports/phpunit.xml" />
137-
<batchtest>
138-
<fileset dir="${dir.tests}" />
139-
</batchtest>
140-
</phpunit>
141133
<exec command="./vendor/bin/phpunit -d zend.enable_gc=0 --log-junit ${dir.reports}/phpunit/phpunit.xml --coverage-clover ${dir.reports.coverage}/clover.xml --coverage-html ${dir.reports.coverage}/ --testdox-html ${dir.reports}/phpunit/testdox.html -c ${dir.tests}" outputProperty="output" returnProperty="returnout" />
142134
<echo msg="${output}" />
143135
<if>

composer.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"library"
77
],
88
"authors" : [{
9-
"name" : "Mariano Custiel",
10-
"email" : "jmcustiel@gmail.com",
11-
"homepage" : "https://github.com/mcustiel",
12-
"role" : "Developer"
13-
}],
9+
"name" : "Mariano Custiel",
10+
"email" : "jmcustiel@gmail.com",
11+
"homepage" : "https://github.com/mcustiel",
12+
"role" : "Developer"
13+
}
14+
],
1415
"autoload" : {
1516
"psr-4" : {
1617
"Mcustiel\\PhpSimpleRegex\\" : "src"
@@ -25,7 +26,7 @@
2526
"type" : "library",
2627
"description" : "This is a library with a set of utils to execute and get the response from regular expressions",
2728
"license" : "GPL-3.0+",
28-
"version" : "1.0.2",
29+
"version" : "1.0.2",
2930
"require" : {
3031
"php" : ">=5.5"
3132
},
@@ -37,6 +38,8 @@
3738
"sebastian/phpcpd" : ">=2.0.2",
3839
"phploc/phploc" : ">=2.1.3",
3940
"verbalexpressions/php-verbal-expressions" : "dev-master",
40-
"selvinortiz/flux" : "dev-master"
41+
"selvinortiz/flux" : "dev-master",
42+
"markwilson/verbal-expressions-php": "dev-master",
43+
"nordbjerg/verbal-expressions-php": "dev-master"
4144
}
42-
}
45+
}

src/Executor.php

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
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+
*/
218
namespace 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
}

src/Match.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
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+
*/
218
namespace Mcustiel\PhpSimpleRegex;
319

420
/**

0 commit comments

Comments
 (0)