Skip to content

Commit 8d399c2

Browse files
committed
Added new methods. Moved all to traits. Added tests. Still working.
1 parent e2b3efb commit 8d399c2

File tree

8 files changed

+584
-296
lines changed

8 files changed

+584
-296
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ What is it
55

66
PhpSimpleRegex is an object oriented regular expressions library for PHP.
77

8-
This library allows to execute preg_* functions in PHP and use the results as objects, making the use of preg_* functions testeable. PhpSimpleRegex is integrated with [PhpVerbalExpressions](https://github.com/VerbalExpressions/PHPVerbalExpressions) and [Flux](https://github.com/selvinortiz/flux), to allow a full Object Oriented approach to Regular Expressions in PHP.
8+
This library allows to execute preg_* functions in PHP and use the results as objects, making the use of preg_* functions testeable. PhpSimpleRegex is integrated with [\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions](https://github.com/VerbalExpressions/PHPVerbalExpressions), [SelvinOrtiz\Utils\Flux\Flux](https://github.com/selvinortiz/flux) and also [MarkWilson\VerbalExpression](https://github.com/markwilson/VerbalExpressionsPhp) to allow a full Object Oriented approach to Regular Expressions in PHP.
99

1010
#### Why Simple?
1111

@@ -47,13 +47,16 @@ $regexFacade = new RegexExecutor();
4747

4848
#### List of methods:
4949

50-
* MatchResult __getAllMatches__(string $pattern, string $subject, integer $offset = 0)
51-
* Match __getOneMatch__(string $pattern, string $subject, integer $offset = 0)
52-
* boolean __match__(string $pattern, string $subject, integer $offset = 0)
53-
* ReplaceResult __replaceAndCount__(string $pattern, string $replacement, mixed $subject, integer $limit = -1)
54-
* mixed __replace__(string $pattern, string $replacement, mixed $subject, integer $limit = -1)
55-
* mixed __replaceCallback__(string $pattern, callable $callback, mixed $subject, integer $limit = -1)
56-
* ReplaceResult __replaceCallbackAndCount__(string $pattern, callable $callback, mixed $subject, integer $limit = -1)
50+
* MatchResult __getAllMatches__(mixed $pattern, string $subject, integer $offset = 0)
51+
* Match __getOneMatch__(mixed $pattern, string $subject, integer $offset = 0)
52+
* boolean __match__(mixed $pattern, string $subject, integer $offset = 0)
53+
* ReplaceResult __replaceAndCount__(mixed $pattern, string $replacement, mixed $subject, integer $limit = -1)
54+
* mixed __replace__(mixed $pattern, string $replacement, mixed $subject, integer $limit = -1)
55+
* mixed __replaceCallback__(mixed $pattern, callable $callback, mixed $subject, integer $limit = -1)
56+
* ReplaceResult __replaceCallbackAndCount__(mixed $pattern, callable $callback, mixed $subject, integer $limit = -1)
57+
* array __split__(mixed $pattern, string $string, integer $limit = -1, bool $returnOnlyNotEmpty = false, bool $captureOffset = false, bool $captureSubpatterns = false)
58+
* array __grep__(mixed $pattern, array $input)
59+
* array __grepNotMatching__(mixed $pattern, array $input)
5760

5861
For each method, the pattern can be a string, a Flux object, or a PhpVerbalExpression object.
5962

src/Executor.php

Lines changed: 23 additions & 285 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
namespace 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.
@@ -25,290 +30,7 @@
2530
*/
2631
class 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

Comments
 (0)