1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Regex; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class MatchResult extends RegexResult |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
protected $pattern; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $subject; |
14
|
|
|
|
15
|
|
|
/** @var bool */ |
16
|
|
|
protected $hasMatch; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $matches; |
20
|
|
|
|
21
|
|
View Code Duplication |
public function __construct(string $pattern, string $subject, bool $hasMatch, array $matches) |
22
|
|
|
{ |
23
|
|
|
$this->pattern = $pattern; |
24
|
|
|
$this->subject = $subject; |
25
|
|
|
$this->hasMatch = $hasMatch; |
26
|
|
|
$this->matches = $matches; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $pattern |
31
|
|
|
* @param string $subject |
32
|
|
|
* |
33
|
|
|
* @return static |
34
|
|
|
* |
35
|
|
|
* @throws \Spatie\Regex\RegexFailed |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public static function for(string $pattern, string $subject) |
38
|
|
|
{ |
39
|
|
|
$matches = []; |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
$result = preg_match($pattern, $subject, $matches); |
43
|
|
|
} catch (Exception $exception) { |
44
|
|
|
throw RegexFailed::match($pattern, $subject, $exception->getMessage()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($result === false) { |
48
|
|
|
throw RegexFailed::match($pattern, $subject, static::lastPregError()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return new static($pattern, $subject, $result, $matches); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function hasMatch(): bool |
55
|
|
|
{ |
56
|
|
|
return $this->hasMatch; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string|null |
61
|
|
|
*/ |
62
|
|
|
public function result() |
63
|
|
|
{ |
64
|
|
|
return $this->matches[0] ?? null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $default |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function resultOr($default) |
73
|
|
|
{ |
74
|
|
|
return $this->result() ?? $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Match group by index or name. |
79
|
|
|
* |
80
|
|
|
* @param int|string $group |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
* |
84
|
|
|
* @throws RegexFailed |
85
|
|
|
*/ |
86
|
|
|
public function group($group): string |
87
|
|
|
{ |
88
|
|
|
if (! isset($this->matches[$group])) { |
89
|
|
|
throw RegexFailed::groupDoesntExist($this->pattern, $this->subject, $group); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->matches[$group]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return an array of the matches. |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function groups(): array |
101
|
|
|
{ |
102
|
|
|
return $this->matches; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Match group by index or return default value if group doesn't exist. |
107
|
|
|
* |
108
|
|
|
* @param int|string $group |
109
|
|
|
* @param string $default |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function groupOr($group, $default): string |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
return $this->group($group); |
117
|
|
|
} catch (RegexFailed $e) { |
118
|
|
|
return $default; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Match group by index or name. |
124
|
|
|
* |
125
|
|
|
* @param int|string $group |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
* |
129
|
|
|
* @throws RegexFailed |
130
|
|
|
*/ |
131
|
|
|
public function namedGroup($group): string |
132
|
|
|
{ |
133
|
|
|
return $this->group($group); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: