-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringBuffer.php
More file actions
244 lines (220 loc) · 6.04 KB
/
StringBuffer.php
File metadata and controls
244 lines (220 loc) · 6.04 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
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
<?php declare(strict_types=1);
namespace Simlux\String;
use Simlux\String\Exceptions\UnknownMethodException;
use Simlux\String\Extensions\Loader;
/**
* Class StringBuffer
*
* @package Simlux\StringBuffer
*
**** from Conditions
* @method bool contains(string $string, bool $caseSensitive = false)
* @method bool containsOneOf(array $strings, bool $caseSensitive = false)
* @method bool beginsWith(string $string, bool $caseSensitive = true)
* @method bool beginsWithOneOf(array $strings, bool $caseSensitive = true)
* @method bool endsWith($string, bool $caseSensitive = true)
* @method bool endsWithOneOf(array $strings, bool $caseSensitive = true)
* @method bool equals(string $string, bool $caseSensitive = true)
* @method bool isOneOf(array $strings, bool $caseSensitive = true)
*
**** from Convention
* @method StringBuffer camelCase(bool $ucFirst = false)
* @method StringBuffer snakeCase(string $delimiter = '_')
* @method StringBuffer ucFirst()
* @method StringBuffer lcFirst()
* @method StringBuffer ucWords()
*
**** from Parser
* @method array parseCSV(string $string, string $delimiter = ',', string $enclosure = '"', string $escape = '\\')
*
**** from Properties
* @method int length()
*
* // from StringTransformer
* @method StringBuffer toLower()
* @method StringBuffer toUpper()
* @method float toFloat()
* @method int toInteger()
*
**** from Lister
* @method array split(string $delimiter)
* @method array splitUppercase(bool $strToLower = false)
*
**** from Manipulator
* @method StringBuffer trim(string $charList = " \t\n\r\0\x0B")
* @method StringBuffer trimLeft(string $charList = " \t\n\r\0\x0B")
* @method StringBuffer trimRight(string $charList = " \t\n\r\0\x0B")
* @method StringBuffer cutLeft(string $string, bool $caseSensitive = false)
* @method StringBuffer cutRight(string $string, bool $caseSensitive = false)
* @method StringBuffer replace(string|array $search, string|array $replace, bool $caseSensitive = true): StringBuffer
* @method StringBuffer remove(string|array $string, bool $caseSensitive = true)
*
**** from Hashes
* @method StringBuffer md5()
* @method StringBuffer sha1()
*
**** from Process
* @method StringBuffer when(bool $condition, callable $then, callable $else = null)
*
**** from Url
* @method StringBuffer urlEncode()
* @method StringBuffer urlDecode()
* @method StringBuffer base64Encode()
* @method StringBuffer base64Decode()
*/
class StringBuffer
{
/**
* @var string
*/
private $string;
/**
* @var Loader
*/
private $loader;
/**
* StringBuffer constructor.
*
* @param string $string
*/
public function __construct(string $string)
{
$this->string = $string;
$this->loader = new Loader($this);
}
/**
* @param string $string
*
* @return StringBuffer
*/
public static function create(string $string): StringBuffer
{
return new StringBuffer($string);
}
/**
* @param string $name
* @param array $arguments
*
* @return mixed
* @throws UnknownMethodException
*/
public function __call(string $name, array $arguments)
{
foreach ($this->loader->getExtensions() as $extension) {
if ($this->loader->extensionHasMethod($extension, $name)) {
return call_user_func_array([$this->loader->factory($extension), $name], $arguments);
}
}
throw new UnknownMethodException($name);
}
/**
* @return string
*/
public function toString(): string
{
return $this->string;
}
/**
* @return string
*/
public function __toString(): string
{
return $this->toString();
}
/**
* @param string $string
*
* @return StringBuffer
*/
public function setString(string $string): StringBuffer
{
$this->string = $string;
return $this;
}
/**
* @param string $string
*
* @return StringBuffer
*/
public function append(string $string): StringBuffer
{
$this->string .= $string;
return $this;
}
/**
* @param bool $condition
* @param string $string
* @param string|null $else
*
* @return StringBuffer
*/
public function appendIf(bool $condition, string $string, string $else = null): StringBuffer
{
if ($condition) {
return $this->append($string);
} else {
if (!is_null($else)) {
return $this->append($else);
}
}
return $this;
}
/**
* @param string $string
*
* @return StringBuffer
*/
public function prepend(string $string): StringBuffer
{
$this->string = $string . $this->string;
return $this;
}
/**
* @param bool $condition
* @param string $string
* @param string|null $else
*
* @return StringBuffer
*/
public function prependIf(bool $condition, string $string, string $else = null): StringBuffer
{
if ($condition) {
return $this->prepend($string);
} else {
if (!is_null($else)) {
return $this->prepend($else);
}
}
return $this;
}
/**
* @param int $start
* @param int|null $length
*
* @return StringBuffer
*/
public function substring(int $start, int $length = null): StringBuffer
{
if (is_null($length)) {
$this->string = substr($this->string, $start);
} else {
$this->string = substr($this->string, $start, $length);
}
return $this;
}
/**
* @param bool $utf8
*
* @return StringBuffer
*/
public function reverse(bool $utf8 = false): StringBuffer
{
if ($utf8) {
preg_match_all('/./us', $this->string, $matches);
$this->string = join('', array_reverse($matches[0]));
} else {
$this->string = strrev($this->string);
}
return $this;
}
}