3
3
namespace Spatie \Regex ;
4
4
5
5
use Exception ;
6
+ use Spatie \Regex \Exceptions \RegexFailed ;
6
7
7
8
class MatchResult extends RegexResult
8
9
{
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
- 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 ;
10
+ public function __construct (
11
+ protected string $ pattern ,
12
+ protected string $ subject ,
13
+ protected bool $ hasMatch ,
14
+ protected array $ matches ,
15
+ ) {
16
+ //
27
17
}
28
18
29
- /**
30
- * @param string $pattern
31
- * @param string $subject
32
- *
33
- * @return static
34
- *
35
- * @throws \Spatie\Regex\RegexFailed
36
- */
37
- public static function for (string $ pattern , string $ subject )
19
+ public static function for (string $ pattern , string $ subject ): static
38
20
{
39
21
$ matches = [];
40
22
@@ -56,20 +38,12 @@ public function hasMatch(): bool
56
38
return $ this ->hasMatch ;
57
39
}
58
40
59
- /**
60
- * @return string|null
61
- */
62
- public function result ()
41
+ public function result (): ?string
63
42
{
64
43
return $ this ->matches [0 ] ?? null ;
65
44
}
66
45
67
- /**
68
- * @param string $default
69
- *
70
- * @return string
71
- */
72
- public function resultOr ($ default )
46
+ public function resultOr (string $ default ): string
73
47
{
74
48
return $ this ->result () ?? $ default ;
75
49
}
@@ -83,7 +57,7 @@ public function resultOr($default)
83
57
*
84
58
* @throws RegexFailed
85
59
*/
86
- public function group ($ group ): string
60
+ public function group (int | string $ group ): string
87
61
{
88
62
if (! isset ($ this ->matches [$ group ])) {
89
63
throw RegexFailed::groupDoesntExist ($ this ->pattern , $ this ->subject , $ group );
@@ -110,7 +84,7 @@ public function groups(): array
110
84
*
111
85
* @return string
112
86
*/
113
- public function groupOr ($ group , $ default ): string
87
+ public function groupOr (int | string $ group , string $ default ): string
114
88
{
115
89
try {
116
90
return $ this ->group ($ group );
@@ -128,7 +102,7 @@ public function groupOr($group, $default): string
128
102
*
129
103
* @throws RegexFailed
130
104
*/
131
- public function namedGroup ($ group ): string
105
+ public function namedGroup (int | string $ group ): string
132
106
{
133
107
return $ this ->group ($ group );
134
108
}
0 commit comments