|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mcustiel\PhpSimpleRegex\PregFunctions; |
|
3
|
|
|
|
|
4
|
|
|
use Mcustiel\PhpSimpleRegex\MatchResult; |
|
5
|
|
|
use Mcustiel\PhpSimpleRegex\Match; |
|
6
|
|
|
|
|
7
|
|
|
trait MatchFunctions |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Searches for all matches for the given pattern. |
|
11
|
|
|
* |
|
12
|
|
|
* @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
|
13
|
|
|
* $pattern |
|
14
|
|
|
* @param string |
|
15
|
|
|
* $subject |
|
16
|
|
|
* @param number |
|
17
|
|
|
* $offset |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \RuntimeException |
|
20
|
|
|
* @throws \InvalidArgumentException |
|
21
|
|
|
* |
|
22
|
|
|
* @return \Mcustiel\PhpSimpleRegex\MatchResult |
|
23
|
|
|
*/ |
|
24
|
6 |
|
public function getAllMatches($pattern, $subject, $offset = 0) |
|
25
|
|
|
{ |
|
26
|
6 |
|
$matches = array(); |
|
27
|
6 |
|
$result = @preg_match_all( |
|
28
|
6 |
|
$this->getPatternByType($pattern), |
|
29
|
5 |
|
$subject, |
|
30
|
5 |
|
$matches, |
|
31
|
5 |
|
PREG_SET_ORDER | PREG_OFFSET_CAPTURE, |
|
32
|
5 |
|
$offset |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
5 |
|
$this->checkResultIsOkOrThrowException($result, $pattern); |
|
36
|
|
|
|
|
37
|
5 |
|
return new MatchResult($matches); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Searches for matches for the given pattern and returns the first match. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
|
44
|
|
|
* $pattern |
|
45
|
|
|
* @param string |
|
46
|
|
|
* $subject |
|
47
|
|
|
* @param number |
|
48
|
|
|
* $offset |
|
49
|
|
|
* |
|
50
|
|
|
* @throws \RuntimeException |
|
51
|
|
|
* @throws \InvalidArgumentException |
|
52
|
|
|
* |
|
53
|
|
|
* @return \Mcustiel\PhpSimpleRegex\Match|NULL |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function getOneMatch($pattern, $subject, $offset = 0) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$matches = array(); |
|
58
|
2 |
|
$pattern = $this->getPatternByType($pattern); |
|
59
|
2 |
|
$result = @preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, $offset); |
|
60
|
2 |
|
$this->checkResultIsOkOrThrowException($result, $pattern); |
|
61
|
2 |
|
return $result? new Match($matches) : null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Checks weather the string matches the given pattern. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
|
68
|
|
|
* $pattern |
|
69
|
|
|
* @param string |
|
70
|
|
|
* $subject |
|
71
|
|
|
* @param number |
|
72
|
|
|
* $offset |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \RuntimeException |
|
75
|
|
|
* @throws \InvalidArgumentException |
|
76
|
|
|
* |
|
77
|
|
|
* @return boolean |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function match($pattern, $subject, $offset = 0) |
|
80
|
|
|
{ |
|
81
|
3 |
|
$matches = []; |
|
82
|
3 |
|
$result = @preg_match( |
|
83
|
3 |
|
$this->getPatternByType($pattern), |
|
84
|
3 |
|
$subject, |
|
85
|
3 |
|
$matches, |
|
86
|
3 |
|
0, |
|
87
|
3 |
|
$offset |
|
88
|
|
|
); |
|
89
|
3 |
|
$this->checkResultIsOkOrThrowException($result, $pattern); |
|
90
|
2 |
|
return (boolean) $result; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param mixed $pattern |
|
95
|
|
|
* @throws \InvalidArgumentException |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
abstract protected function getPatternByType($pattern); |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param mixed $result |
|
102
|
|
|
* @param string $pattern |
|
103
|
|
|
* |
|
104
|
|
|
* @throws \RuntimeException |
|
105
|
|
|
*/ |
|
106
|
|
|
abstract protected function checkResultIsOkOrThrowException($result, $pattern); |
|
107
|
|
|
} |
|
108
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.