1
|
|
|
<?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
|
|
|
*/ |
18
|
|
|
namespace Mcustiel\PhpSimpleRegex; |
19
|
|
|
|
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
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Facade class that wraps preg_* functions and returns the result of calling this function in an |
27
|
|
|
* object oriented way when necessary. |
28
|
|
|
* |
29
|
|
|
* @author mcustiel |
30
|
|
|
*/ |
31
|
|
|
class Executor |
32
|
|
|
{ |
33
|
|
|
use GrepFunctions, MatchFunctions, ReplaceFunctions, SplitFunctions; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed $pattern |
37
|
|
|
* @throws \InvalidArgumentException |
38
|
|
|
* @return string|string[] |
39
|
|
|
*/ |
40
|
15 |
|
protected function getPatternForReplace($pattern) |
41
|
|
|
{ |
42
|
15 |
|
if (is_array($pattern)) { |
43
|
1 |
|
return $this->getPatternsFromArray($pattern); |
44
|
|
|
} |
45
|
14 |
|
return $this->getPatternByType($pattern); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param mixed $pattern |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
33 |
|
protected function getPatternByType($pattern) |
54
|
|
|
{ |
55
|
33 |
|
if (is_string($pattern)) { |
56
|
29 |
|
return $pattern; |
57
|
|
|
} |
58
|
4 |
|
if ($this->isValidObjectPattern($pattern)) { |
59
|
3 |
|
return $pattern->__toString(); |
60
|
|
|
} |
61
|
1 |
|
throw new \InvalidArgumentException( |
62
|
|
|
'Pattern must be a string, an instance of ' |
63
|
|
|
. 'VerbalExpressions\PHPVerbalExpressions\VerbalExpressions ' |
64
|
|
|
. ', SelvinOrtiz\Utils\Flux\Flux' |
65
|
1 |
|
. ' or MarkWilson\VerbalExpression' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $result |
71
|
|
|
* @param string $pattern |
72
|
|
|
* |
73
|
|
|
* @throws \RuntimeException |
74
|
|
|
*/ |
75
|
17 |
|
protected function checkResultIsOkOrThrowException($result, $pattern) |
76
|
|
|
{ |
77
|
17 |
|
if ($result === false) { |
78
|
4 |
|
throw new \RuntimeException( |
79
|
4 |
|
'An error occurred executing the pattern ' . var_export($pattern, true) |
80
|
|
|
); |
81
|
|
|
} |
82
|
13 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $result |
86
|
|
|
* @param string $pattern |
87
|
|
|
* |
88
|
|
|
* @throws \RuntimeException |
89
|
|
|
*/ |
90
|
11 |
|
protected function checkResultIsOkForReplaceOrThrowException($result, $pattern) |
91
|
|
|
{ |
92
|
11 |
|
if ($result === null) { |
93
|
1 |
|
throw new \RuntimeException( |
94
|
1 |
|
'An error occurred replacing the pattern ' . var_export($pattern, true) |
95
|
|
|
); |
96
|
|
|
} |
97
|
10 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string[]|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions[]|SelvinOrtiz\Utils\Flux\Flux[]|MarkWilson\VerbalExpression[] $patterns |
101
|
|
|
* @return string[] |
102
|
|
|
* @throws \InvalidArgumentException |
103
|
|
|
*/ |
104
|
1 |
|
protected function getPatternsFromArray(array $patterns) |
105
|
|
|
{ |
106
|
1 |
|
$stringPatterns = []; |
107
|
1 |
|
foreach ($patterns as $pattern) { |
108
|
1 |
|
$stringPatterns[] = $this->getPatternByType($pattern); |
109
|
|
|
} |
110
|
1 |
|
return $stringPatterns; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param mixed $pattern |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
4 |
|
private function isValidObjectPattern($pattern) |
118
|
|
|
{ |
119
|
4 |
|
return is_object($pattern) && ( |
120
|
4 |
|
is_a($pattern, 'SelvinOrtiz\Utils\Flux\Flux') |
121
|
3 |
|
|| is_a($pattern, 'VerbalExpressions\PHPVerbalExpressions\VerbalExpressions') |
122
|
4 |
|
|| is_a($pattern, 'MarkWilson\VerbalExpression') |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|