1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StringObject; |
4
|
|
|
|
5
|
|
|
use StringObject\AnyString; |
6
|
|
|
|
7
|
|
|
class AStringBuilder extends AString |
8
|
|
|
{ |
9
|
|
|
// MODIFYING METHODS |
10
|
|
|
|
11
|
|
|
public function append($str) |
12
|
|
|
{ |
13
|
|
|
$this->raw .= $str; |
14
|
|
|
return $this; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function chunk($length = 76, $ending = "\r\n") |
18
|
|
|
{ |
19
|
|
|
$this->raw = \chunk_split($this->raw, $length, $ending); |
20
|
|
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function escape($flags = self::NORMAL, $charlist = '') |
24
|
|
|
{ |
25
|
|
|
$flagsmap = [ |
26
|
|
|
self::NORMAL => 'addslashes', |
27
|
|
|
self::C_STYLE => 'addcslashes', |
28
|
|
|
self::META => 'quotemeta', |
29
|
|
|
]; |
30
|
|
View Code Duplication |
if ($flags === self::C_STYLE) { |
|
|
|
|
31
|
|
|
$this->raw = \call_user_func($flagsmap[$flags], $this->raw, $charlist); |
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
$this->raw = \call_user_func($flagsmap[$flags], $this->raw); |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function pad($newlength, $padding = ' ', $flags = self::END) |
39
|
|
|
{ |
40
|
|
|
$this->raw = \str_pad($this->raw, $newlength, $padding, $flags); |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function prepend($str) |
45
|
|
|
{ |
46
|
|
|
$this->raw = $str . $this->raw; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function repeat($times) |
51
|
|
|
{ |
52
|
|
|
$this->raw = \str_repeat($this->raw, $times); |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $replace |
58
|
|
|
*/ |
59
|
|
|
public function replace($search, $replace, $flags = self::NORMAL) |
60
|
|
|
{ |
61
|
|
|
if ($flags & self::CASE_INSENSITIVE) { |
62
|
|
|
$this->raw = \str_ireplace($search, $replace, $this->raw); |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
$this->raw = \str_replace($search, $replace, $this->raw); |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function replaceSubstr($replacement, $start, $length = null) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if ($length === null) { |
72
|
|
|
$length = $this->length(); |
73
|
|
|
} |
74
|
|
|
$this->raw = \substr_replace($this->raw, $replacement, $start, $length); |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function replaceWhole($replacement = '') |
79
|
|
|
{ |
80
|
|
|
self::testStringableObject($replacement); |
81
|
|
|
$this->raw = (string) $replacement; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function reverse() |
86
|
|
|
{ |
87
|
|
|
$this->raw = \strrev($this->raw); |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function shuffle() |
92
|
|
|
{ |
93
|
|
|
$this->raw = \str_shuffle($this->raw); |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function translate($search, $replace = '') |
98
|
|
|
{ |
99
|
|
|
if (is_array($search)) { |
100
|
|
|
$this->raw = \strtr($this->raw, $search); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
$this->raw = \strtr($this->raw, $search, $replace); |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function trim($mask = " \t\n\r\0\x0B", $flags = self::BOTH_ENDS) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$flagsmap = [ |
110
|
|
|
self::START => 'ltrim', |
111
|
|
|
self::END => 'rtrim', |
112
|
|
|
self::BOTH_ENDS => 'trim', |
113
|
|
|
]; |
114
|
|
|
$this->raw = \call_user_func($flagsmap[$flags], $this->raw, $mask); |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
public function unescape($flags = self::NORMAL) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$flagsmap = [ |
121
|
|
|
self::NORMAL => 'stripslashes', |
122
|
|
|
self::C_STYLE => 'stripcslashes', |
123
|
|
|
self::META => 'stripslashes', |
124
|
|
|
]; |
125
|
|
|
$this->raw = \call_user_func($flagsmap[$flags], $this->raw); |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function uuDecode() |
130
|
|
|
{ |
131
|
|
|
$this->raw = \convert_uudecode($this->raw); |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function uuEncode() |
136
|
|
|
{ |
137
|
|
|
$this->raw = \convert_uuencode($this->raw); |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function wordwrap($width = 75, $break = "\n") |
142
|
|
|
{ |
143
|
|
|
$this->raw = \wordwrap($this->raw, $width, $break, false); |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function wordwrapBreaking($width = 75, $break = "\n") |
148
|
|
|
{ |
149
|
|
|
$this->raw = \wordwrap($this->raw, $width, $break, true); |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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.