|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Regex; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
class ReplaceResult extends RegexResult |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var string|array */ |
|
10
|
|
|
protected $pattern; |
|
11
|
|
|
|
|
12
|
|
|
/** @var string|array */ |
|
13
|
|
|
protected $replacement; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string|array */ |
|
16
|
|
|
protected $subject; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string|array */ |
|
19
|
|
|
protected $result; |
|
20
|
|
|
|
|
21
|
|
|
/** @var int */ |
|
22
|
|
|
protected $count; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct($pattern, $replacement, $subject, $result, int $count) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->pattern = $pattern; |
|
27
|
|
|
$this->replacement = $replacement; |
|
28
|
|
|
$this->subject = $subject; |
|
29
|
|
|
$this->result = $result; |
|
30
|
|
|
$this->count = $count; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string|array $pattern |
|
35
|
|
|
* @param string|array|callable $replacement |
|
36
|
|
|
* @param string|array $subject |
|
37
|
|
|
* @param int $limit |
|
38
|
|
|
* |
|
39
|
|
|
* @return \Spatie\Regex\ReplaceResult |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function for($pattern, $replacement, $subject, $limit) |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
|
|
[$result, $count] = ! is_string($replacement) && is_callable($replacement) ? |
|
|
|
|
|
|
45
|
|
|
static::doReplacementWithCallable($pattern, $replacement, $subject, $limit) : |
|
46
|
|
|
static::doReplacement($pattern, $replacement, $subject, $limit); |
|
47
|
|
|
} catch (Exception $exception) { |
|
48
|
|
|
throw RegexFailed::replace($pattern, $subject, $exception->getMessage()); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($result === null) { |
|
52
|
|
|
throw RegexFailed::replace($pattern, $subject, static::lastPregError()); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return new static($pattern, $replacement, $subject, $result, $count); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected static function doReplacement($pattern, $replacement, $subject, $limit): array |
|
59
|
|
|
{ |
|
60
|
|
|
$count = 0; |
|
61
|
|
|
|
|
62
|
|
|
$result = preg_replace($pattern, $replacement, $subject, $limit, $count); |
|
63
|
|
|
|
|
64
|
|
|
return [$result, $count]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected static function doReplacementWithCallable($pattern, callable $replacement, $subject, $limit): array |
|
68
|
|
|
{ |
|
69
|
|
|
$replacement = function (array $matches) use ($pattern, $subject, $replacement) { |
|
70
|
|
|
return $replacement(new MatchResult($pattern, $subject, true, $matches)); |
|
71
|
|
|
}; |
|
72
|
|
|
|
|
73
|
|
|
$count = 0; |
|
74
|
|
|
|
|
75
|
|
|
$result = preg_replace_callback($pattern, $replacement, $subject, $limit, $count); |
|
76
|
|
|
|
|
77
|
|
|
return [$result, $count]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string|array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function result() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->result; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function count(): int |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->count; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.