-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStringBuilder.php
334 lines (309 loc) · 7.92 KB
/
StringBuilder.php
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace ChromaX\StringBuilder;
use ChromaX\StringBuilder\Util\ArgumentValidator;
use InvalidArgumentException;
/**
* Class StringBuilder
*
* @package ChromaX\StringBuilder
*/
class StringBuilder
{
/**
* @var string
*/
private $string = '';
/**
* SimpleStringBuilder constructor
*
* Takes an initial string as argument
*
* @param null $string
* @throws InvalidArgumentException
*/
public function __construct($string = null)
{
if ($string !== null) {
ArgumentValidator::validateScalar($string);
$this->string = (string)$string;
}
}
/**
* Appends the given string
*
* @param string $string
* @return $this
* @throws InvalidArgumentException
*/
public function append($string)
{
ArgumentValidator::validateScalar($string);
$this->string .= (string)$string;
return $this;
}
/**
* Prepends the given string
*
* @param string $string
* @return $this
* @throws InvalidArgumentException
*/
public function prepend($string)
{
ArgumentValidator::validateScalar($string);
$this->string = (string)$string . $this->string;
return $this;
}
/**
* Inserts the given string at the given position
*
* @param int $position
* @param string $string
* @return $this
* @throws InvalidArgumentException
*/
public function insert($position, $string)
{
ArgumentValidator::validateUnsignedInteger($position);
ArgumentValidator::validateScalar($string);
if ($position >= $this->length()) {
throw new InvalidArgumentException('Position invalid');
}
$this->string = mb_substr($this->string, 0, $position) . (string)$string . mb_substr($this->string, $position);
return $this;
}
/**
* Replaces the characters defined by the given position and length with the given string
*
* @param int $position
* @param int $length
* @param string $string
* @return $this
* @throws InvalidArgumentException
*/
public function replace($position, $length, $string)
{
ArgumentValidator::validateUnsignedInteger($position);
ArgumentValidator::validateUnsignedInteger($length);
ArgumentValidator::validateScalar($string);
if ($position >= $this->length()) {
throw new InvalidArgumentException('Position invalid');
}
if ($position + $length > $this->length()) {
throw new InvalidArgumentException('Length invalid');
}
$this->string = mb_substr($this->string, 0, $position) . (string)$string . mb_substr($this->string, $position + $length);
return $this;
}
/**
* Sets the character at the given position
*
* @param int $position
* @param string $character
* @return $this
* @throws InvalidArgumentException
*/
public function setCharAt($position, $character)
{
ArgumentValidator::validateUnsignedInteger($position);
ArgumentValidator::validateScalar($character);
if ($position >= $this->length()) {
throw new InvalidArgumentException('Position invalid');
}
if (mb_strlen((string)$character) !== 1) {
throw new InvalidArgumentException('Expected a scalar value of length 1');
}
$this->string = mb_substr($this->string, 0, $position) . (string)$character . mb_substr($this->string, $position + 1);
return $this;
}
/**
* Reverts the string to build
*
* @return $this
*/
public function reverse()
{
$length = $this->length();
$reversed = '';
while ($length-- > 0) {
$reversed .= mb_substr($this->string, $length, 1, mb_detect_encoding($this->string));
}
$this->string = $reversed;
return $this;
}
/**
* Removes the portion defined by the given position and length
*
* @param int $position
* @param int $length
* @return $this
* @throws InvalidArgumentException
*/
public function delete($position, $length = null)
{
ArgumentValidator::validateUnsignedInteger($position);
ArgumentValidator::validateUnsignedIntegerOrNull($length);
if ($position >= $this->length()) {
throw new InvalidArgumentException('Position invalid');
}
if ($length === null) {
$this->string = mb_substr($this->string, 0, $position);
} else {
$this->string = mb_substr($this->string, 0, $position) . mb_substr($this->string, $position + $length);
}
return $this;
}
/**
* Removes the character at the given position
*
* @param int $position
* @return $this
* @throws InvalidArgumentException
*/
public function deleteCharAt($position)
{
ArgumentValidator::validateUnsignedInteger($position);
if ($position >= $this->length()) {
throw new InvalidArgumentException('Position invalid');
}
$this->string = mb_substr($this->string, 0, $position) . mb_substr($this->string, $position + 1);
return $this;
}
/**
* Whether the string to build contains the given substring
*
* @param string $substring
* @return bool
* @throws InvalidArgumentException
*/
public function contains($substring)
{
ArgumentValidator::validateScalar($substring);
return strpos($this->string, (string)$substring) !== false;
}
/**
* Returns the index of the first occurence of the given substring or null
*
* Takes an optional parameter to begin searching after the given offset
*
* @param string $string
* @param int $offset
* @return int
* @throws InvalidArgumentException
*/
public function indexOf($string, $offset = 0)
{
ArgumentValidator::validateScalar($string);
ArgumentValidator::validateEmpty($string);
ArgumentValidator::validateUnsignedInteger($offset);
$index = mb_strpos($this->string, (string)$string, $offset);
return $index === false ? null : $index;
}
/**
* Returns the index of the last occurence of the given substring or null
*
* Takes an optional parameter to end searching before the given offset
*
* @param string $string
* @param int $offset
* @return int
* @throws InvalidArgumentException
*/
public function lastIndexOf($string, $offset = 0)
{
ArgumentValidator::validateScalar($string);
ArgumentValidator::validateEmpty($string);
ArgumentValidator::validateUnsignedInteger($offset);
$index = mb_strrpos($this->string, (string)$string, -1 * $offset);
return $index === false ? null : $index;
}
/**
* Returns the number of bytes of the string to build
*
* @return int
*/
public function size()
{
return strlen($this->string);
}
/**
* Returns the number of characters of the string to build
*
* @return int
*/
public function length()
{
return mb_strlen($this->string, mb_detect_encoding($this->string));
}
/**
* Returns the character at the given position
*
* @param int $position
* @return string
* @throws InvalidArgumentException
*/
public function charAt($position)
{
ArgumentValidator::validateUnsignedInteger($position);
if ($position >= $this->length()) {
throw new InvalidArgumentException('Position invalid');
}
return mb_substr($this->string, $position, 1);
}
/**
* Returns first character
*
* @return string
*/
public function firstChar()
{
return mb_substr($this->string, 0, 1, mb_detect_encoding($this->string));
}
/**
* Returns last character
*
* @return string
*/
public function lastChar()
{
return mb_substr($this->string, -1, null, mb_detect_encoding($this->string));
}
/**
* Returns an substring defined by startPosition and length
*
* @param int $startPosition
* @param int $length
* @return string
* @throws InvalidArgumentException
*/
public function buildSubstring($startPosition, $length = null)
{
ArgumentValidator::validateUnsignedInteger($startPosition);
ArgumentValidator::validateUnsignedIntegerOrNull($length);
if ($startPosition >= $this->length()) {
throw new InvalidArgumentException('Start position ' . (string)$startPosition . ' invalid');
}
if ($length === null) {
return mb_substr($this->string, $startPosition);
}
return mb_substr($this->string, $startPosition, $length);
}
/**
* Returns the whole resulting string
*
* @return string
*/
public function build()
{
return $this->string;
}
/**
* Returns the whole resulting string
*
* @return string
*/
public function __toString()
{
return $this->build();
}
}