-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Replacements.php
More file actions
106 lines (92 loc) · 2.45 KB
/
Copy pathString_Replacements.php
File metadata and controls
106 lines (92 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* String_Replacements class file
*
* @package Mantle
*/
namespace Mantle\Support;
/**
* Collects pairs of strings to search and replace.
*/
class String_Replacements {
/**
* Collected strings to search for.
*
* @var string[]|string[][]
*/
protected $search = [];
/**
* Collected strings to replace found search values.
*
* @var string[]|string[][]
*/
protected $replace = [];
/**
* Number of search-replace pairs collected.
*
* @var int
*/
protected $length = 0;
/**
* Whether only individual strings have been added and thus can be passed to
* \str_replace() as arrays of strings.
*
* @var bool
*/
protected $only_strings = true;
/**
* Add a search/replace pair.
*
* @param string|string[] $search The value or values being searched for.
* @param string|string[] $replace The value or values that replaces found $search values.
*/
public function add( $search, $replace ): void {
// Allow passing the results of expressions that might not generate different values.
if ( $search === $replace ) {
return;
}
if ( $this->only_strings && ( ! \is_string( $search ) || ! \is_string( $replace ) ) ) {
$this->only_strings = false;
}
$this->search[] = $search;
$this->replace[] = $replace;
$this->length++;
}
/**
* Apply the search/replace pairs to a subject using \str_replace().
*
* @param string|string[] $subject String or strings to alter.
* @return string|string[] Altered string or strings.
*/
public function replace( $subject ) {
return $this->apply( $subject, '\str_replace' );
}
/**
* Apply the search/replace pairs to a subject using \str_ireplace().
*
* @param string|string[] $subject String or strings to alter.
* @return string|string[] Altered string or strings.
*/
public function ireplace( $subject ) {
return $this->apply( $subject, '\str_ireplace' );
}
/**
* Wrapper to apply the \str_*() function to the subject.
*
* @param string|string[] $subject Subject.
* @param callable $callable \str_replace() or \str_ireplace().
* @return string|string[] Altered string or strings.
*/
private function apply( $subject, callable $callable ) {
if ( ! $this->length ) {
return $subject;
}
if ( $this->only_strings ) {
return $callable( $this->search, $this->replace, $subject );
}
for ( $i = 0; $i < $this->length; $i++ ) {
$subject = $callable( $this->search[ $i ], $this->replace[ $i ], $subject );
}
return $subject;
}
}