-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringer.php
More file actions
244 lines (220 loc) · 7.62 KB
/
Copy pathStringer.php
File metadata and controls
244 lines (220 loc) · 7.62 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 namespace Nabeghe\Stringer;
/**
* Stringer class.
*
* Holds a string within itself & can access the methods of the {@see Str} class with the same name.
* Most methods apply changes to the original value and return the {@see Stringer} object.
*
* @method self after(string $search)
* @method self afterLast(string $search)
* @method self before(string $search)
* @method self beforeLast(string $search)
* @method self between(string $from, string $to)
* @method bool bool(string|null $value)
* @method self camel(string|null $value)
* @method string[] chars(string $text)
* @method string charAt(int $index)
* @method self chopStart(string $needle, bool $ignoreCase = false)
* @method self chopEnd(string $needle, bool $ignoreCase = false)
* @method string[] chunk(int $limit)
* @method bool contains(string|string[] $needles, bool $ignoreCase = false)
* @method bool containsAll(string[] $needles, bool $ignoreCase = false)
* @method bool doesntContain(string|string[] $needles, bool $ignoreCase = false)
* @method self convertCase(int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8')
* @method self def(?string $def = '')
* @method self deduplicate(string $character = ' ')
* @method bool endsWith(string $haystack, string $needle)
* @method self escape(string[] $chars)
* @method self escapeMarkdown()
* @method self escapeMarkdownCode()
* @method self escapeMarkdownV2()
* @method self escapeMarkdownV2CodeBlock()
* @method string[] explode(string $separator)
* @method self finish(string $cap)
* @method self firstWord(string $wordsSeperator = ' ')
* @method bool hasChar(string|string[] $chars)
* @method int|false ipos(string $needle, int $offset = 0)
* @method bool isMadeOf(string|string[] $chars)
* @method bool string|string[] $pattern)
* @method bool isUrl(string[] $protocols = [])
* @method bool isUuid()
* @method self kebab()
* @method int len(string|null $encoding = null, bool $emojible = true)
* @method self lcfirst()
* @method self limit(int $limit, string $extra = '...')
* @method self limitWords(int $words = 100, string $end = '...')
* @method string[] lines()
* @method self lower()
* @method int longestSequence(string $char = ' ')
* @method self mask(string $character, int $index, int|null $length = null, string $encoding = 'UTF-8')
* @method self normalizeNumbers()
* @method self normalizeArabicNumbers()
* @method self normalizePersianNumbers()
* @method self normalizePersianChars(bool $all = true)
* @method self normalizeWhitespace()
* @method self numbers()
* @method self padBoth(int $length, string $pad = ' ')
* @method self padLeft(int $length, string $pad = ' ')
* @method self padRight(int $length, string $pad = ' ')
* @method array<int, string|null> parseCallback($callback, $default = null)
* @method array<int, string|null> pos(string $haystack, string $needle, int $offset = 0)
* @method array readingTime()
* @method self replaceDeeply(string|array $search)
* @method self reverse()
* @method self snake(string $delimiter = '_')
* @method self start(string $prefix)
* @method self sub(int $start, int|null $length = null, string $encoding = 'UTF-8')
* @method string[] split(int $length)
* @method self squish()
* @method bool startsWith(string $needle)
* @method self studly()
* @method self take(int $limit)
* @method self toBase64()
* @method self trim(string|null $charlist = null)
* @method self ltrim(string|null $charlist = null)
* @method self rtrim(string|null $charlist = null)
* @method self title()
* @method self headline()
* @method self apa()
* @method self ucfirst()
* @method string[] ucsplit()
* @method self upper()
* @method int wordCount(string|null $characters = null)
* @method self wordWrap(int $characters = 75, string $break = "\n", bool $cutLongWords = false)
* @method self wrap(string $before, string|null $after = null)
* @method self unwrap(string $value, string $before, string|null $after = null)
*/
class Stringer
{
protected string $value;
public const RETURNS = [
'charAt',
];
/**
* Constructor.
*
* @param string $value Raw string.
*/
public function __construct($value)
{
$this->value = Str::tryCast($value);
}
/**
* Creates a `Stringer` object from a base64 string by decoding it.
*
* @param string $string
* @param bool $strict
* @return static
*/
public static function fromBase64($string, $strict = false)
{
$decoded = base64_decode($string, $strict);
if ($decoded === false) {
return null;
}
return new static($decoded);
}
/**
* Generates a random string and creates a Stringer object with it.
*
* @param int $length
* @param string $source
* @return string
*/
public function fromRandom($length, $source = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
return Str::random($length, $this->value);
}
public function is($pattern)
{
return Str::is($pattern, $this->value);
}
public function isJson(&$decoded = null)
{
return Str::isJson($this->value, $decoded);
}
public function match($pattern)
{
return Str::match($pattern, $this->value);
}
public function isMatch($pattern)
{
return Str::isMatch($pattern, $this->value);
}
public function replace($search, $replace, $caseSensitive = true)
{
$this->value = Str::replace($search, $replace, $this->value, $caseSensitive);
return $this;
}
public function replaceFirst($search, $replace)
{
$this->value = Str::replaceFirst($search, $replace, $this->value);
return $this;
}
public function replaceStart($search, $replace)
{
$this->value = Str::replaceStart($search, $replace, $this->value);
return $this;
}
public function replaceLast($search, $replace)
{
$this->value = Str::replaceLast($search, $replace, $this->value);
return $this;
}
public function replaceEnd($search, $replace)
{
$this->value = Str::replaceLast($search, $replace, $this->value);
return $this;
}
public function replaceMatches($pattern, $replace, $subject, $limit = -1)
{
$this->value = Str::replaceMatches($pattern, $replace, $this->value, $limit);
return $this;
}
public function replaceFirstLetters($needle, $replace)
{
$this->value = Str::replaceFirstLetters($needle, $replace, $this->value);
return $this;
}
public function replaceOnce($needle, $replace)
{
$this->value = Str::replaceOnce($needle, $replace, $this->value);
return $this;
}
public function remove($search, $ignoreCase = false)
{
$this->value = Str::remove($search, $this->value, $ignoreCase);
return $this;
}
public function swap($map)
{
$this->value = Str::swap($map, $this->value);
return $this;
}
/**
* Generates a random string and creates a Stringer object with it.
*
* @param int $length
* @return string
*/
public function random($length)
{
return Str::random($length, $this->value);
}
public function __toString()
{
return $this->value;
}
public function __call($name, $arguments)
{
$result = Str::$name($this->value, ...$arguments);
if (is_string($result) || $result === null) {
if (in_array($name, self::RETURNS)) {
return $result;
}
$this->value = $result === null ? '' : $result;
return $this;
}
return $result;
}
}