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
|
|
|
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
|
|
|
* Match group by index. |
69
|
|
|
* |
70
|
|
|
* @param int $index |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
* |
74
|
|
|
* @throws RegexFailed |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function group(int $index): string |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (! isset($this->matches[$index])) { |
79
|
|
|
throw RegexFailed::indexedGroupDoesntExist($this->pattern, $this->subject, $index); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->matches[$index]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Match group by name. |
87
|
|
|
* |
88
|
|
|
* @param string $groupName |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
* |
92
|
|
|
* @throws RegexFailed |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function namedGroup(string $groupName): string |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
if (! isset($this->matches[$groupName])) { |
97
|
|
|
throw RegexFailed::namedGroupDoesntExist($this->pattern, $this->subject, $groupName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->matches[$groupName]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.