1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\Violin; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class RegularExpressionHandler |
7
|
|
|
* @internal |
8
|
|
|
*/ |
9
|
|
|
final class RegularExpressionHandler |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $pattern; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Violin |
18
|
|
|
*/ |
19
|
|
|
private $str; |
20
|
|
|
|
21
|
|
|
private function __construct() |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $pattern |
27
|
|
|
* @param bool $ensureUtf8Compatible |
28
|
|
|
* @return RegularExpressionHandler |
29
|
|
|
* @throws \InvalidArgumentException |
30
|
|
|
*/ |
31
|
|
|
public static function make($pattern, Violin $str) |
32
|
|
|
{ |
33
|
|
|
static $prototype; |
34
|
|
|
|
35
|
|
|
if (!isset($prototype)) { |
36
|
|
|
$prototype = new self; |
37
|
|
|
} else { |
38
|
|
|
$prototype = clone $prototype; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$delimiter = $pattern[0]; |
42
|
|
|
$delimiterPos = \strrpos($pattern, $delimiter); |
43
|
|
|
$modifiers = \substr($pattern, $delimiterPos + \strlen($delimiter), \strlen($pattern)); |
44
|
|
|
if (false === \strpos($modifiers, 'u')) { |
45
|
|
|
$pattern = $pattern . 'u'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$prototype->pattern = $pattern; |
49
|
|
|
$prototype->str = $str; |
50
|
|
|
return $prototype; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function hasMatches(): bool |
57
|
|
|
{ |
58
|
|
|
return 0 < \preg_match($this->pattern, $this->str); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function findMatches(): array |
65
|
|
|
{ |
66
|
|
|
\preg_match($this->pattern, $this->str, $matches); |
67
|
|
|
return $matches; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function findAllMatches(): array |
74
|
|
|
{ |
75
|
|
|
\preg_match_all($this->pattern, $this->str, $matches); |
76
|
|
|
return $matches; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $replacement |
81
|
|
|
* @param int $limit |
82
|
|
|
* @param int|null $count |
83
|
|
|
* @return Violin |
84
|
|
|
* @throws \InvalidArgumentException |
85
|
|
|
*/ |
86
|
|
|
public function replaceWith($replacement, int $limit = -1, int &$count = null): Violin |
87
|
|
|
{ |
88
|
|
|
return Violin::tune(\preg_replace($this->pattern, $replacement, $this->str, $limit, $count)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param callable $callback |
93
|
|
|
* @param int $limit |
94
|
|
|
* @param int|null $count |
95
|
|
|
* @return Violin |
96
|
|
|
* @throws \InvalidArgumentException |
97
|
|
|
*/ |
98
|
|
|
public function replaceWithCallback(callable $callback, int $limit = -1, int &$count = null): Violin |
99
|
|
|
{ |
100
|
|
|
return Violin::tune(\preg_replace_callback($this->pattern, $callback, $this->str, $limit, $count)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|