|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Helldar\Support\Facades\Helpers\Str as Helper; |
|
6
|
|
|
use Stringable as StringableContract; |
|
7
|
|
|
|
|
8
|
|
|
final class Stringable implements StringableContract |
|
9
|
|
|
{ |
|
10
|
|
|
protected $value; |
|
11
|
|
|
|
|
12
|
38 |
|
public function __construct(?string $value = null) |
|
13
|
|
|
{ |
|
14
|
38 |
|
$this->value = (string) $value; |
|
15
|
38 |
|
} |
|
16
|
|
|
|
|
17
|
38 |
|
public function __toString(): string |
|
18
|
|
|
{ |
|
19
|
38 |
|
return $this->value; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Creates the current object in case of accessing the Stringable through the facade. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string|null $value |
|
26
|
|
|
* |
|
27
|
|
|
* @return $this |
|
28
|
|
|
*/ |
|
29
|
18 |
|
public function of(?string $value): self |
|
30
|
|
|
{ |
|
31
|
18 |
|
return new self($value); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Replacing multiple spaces with a single space. |
|
36
|
|
|
* |
|
37
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function removeSpaces(): self |
|
40
|
|
|
{ |
|
41
|
4 |
|
return new self(Helper::removeSpaces($this->value)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Begin a string with a single instance of a given value. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $prefix |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function start(string $prefix): self |
|
52
|
|
|
{ |
|
53
|
4 |
|
return new self(Helper::start($this->value, $prefix)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Cap a string with a single instance of a given value. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $cap |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function finish(string $cap = '/'): self |
|
64
|
|
|
{ |
|
65
|
2 |
|
return new self(Helper::finish($this->value, $cap)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Convert the given string to lower-case. |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function lower(): self |
|
74
|
|
|
{ |
|
75
|
2 |
|
return new self(Helper::lower($this->value)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Convert the given string to upper-case. |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
82
|
|
|
*/ |
|
83
|
4 |
|
public function upper(): self |
|
84
|
|
|
{ |
|
85
|
4 |
|
return new self(Helper::upper($this->value)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Convert a value to studly caps case. |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function studly(): self |
|
94
|
|
|
{ |
|
95
|
2 |
|
return new self(Helper::studly($this->value)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Convert a value to camel case. |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
102
|
|
|
*/ |
|
103
|
4 |
|
public function camel(): self |
|
104
|
|
|
{ |
|
105
|
4 |
|
return new self(Helper::camel($this->value)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Convert a string to snake case. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string|null $delimiter |
|
112
|
|
|
* |
|
113
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function snake(?string $delimiter = '_'): self |
|
116
|
|
|
{ |
|
117
|
2 |
|
return new self(Helper::snake($this->value, $delimiter)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Generate a URL friendly "slug" from a given string. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $separator |
|
124
|
|
|
* @param string|null $language |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
127
|
|
|
*/ |
|
128
|
6 |
|
public function slug(string $separator = '-', ?string $language = 'en'): self |
|
129
|
|
|
{ |
|
130
|
6 |
|
return new self(Helper::slug($this->value, $separator, $language)); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Convert the given string to title case. |
|
135
|
|
|
* |
|
136
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function title(): self |
|
139
|
|
|
{ |
|
140
|
2 |
|
return new self(Helper::title($this->value)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns the portion of string specified by the start and length parameters. |
|
145
|
|
|
* |
|
146
|
|
|
* @param int $start |
|
147
|
|
|
* @param int|null $length |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
150
|
|
|
*/ |
|
151
|
2 |
|
public function substr(int $start, int $length = null): self |
|
152
|
|
|
{ |
|
153
|
2 |
|
return new self(Helper::substr($this->value, $start, $length)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Replace all occurrences of the search string with the replacement string. |
|
158
|
|
|
* |
|
159
|
|
|
* @param array $values |
|
160
|
|
|
* @param string|null $key_format |
|
161
|
|
|
* |
|
162
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
163
|
|
|
*/ |
|
164
|
4 |
|
public function replace(array $values, string $key_format = null): self |
|
165
|
|
|
{ |
|
166
|
4 |
|
return new self(Helper::replace($this->value, $values, $key_format)); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get the portion of a string before the first occurrence of a given value. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $search |
|
173
|
|
|
* |
|
174
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
175
|
|
|
*/ |
|
176
|
4 |
|
public function before(string $search): self |
|
177
|
|
|
{ |
|
178
|
4 |
|
return new self(Helper::before($this->value, $search)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Return the remainder of a string after the first occurrence of a given value. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $search |
|
185
|
|
|
* |
|
186
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
187
|
|
|
*/ |
|
188
|
4 |
|
public function after(string $search): self |
|
189
|
|
|
{ |
|
190
|
4 |
|
return new self(Helper::after($this->value, $search)); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Strip whitespace (or other characters) from the beginning and end of a string. |
|
195
|
|
|
* |
|
196
|
|
|
* @see https://php.net/manual/en/function.trim.php |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $characters |
|
199
|
|
|
* |
|
200
|
2 |
|
* @return $this |
|
201
|
|
|
*/ |
|
202
|
2 |
|
public function trim(string $characters = " \t\n\r\0\x0B"): self |
|
203
|
|
|
{ |
|
204
|
|
|
return new self(trim($this->value, $characters)); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Get the string matching the given pattern. |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $pattern |
|
211
|
|
|
* |
|
212
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
213
|
2 |
|
*/ |
|
214
|
|
|
public function match(string $pattern): self |
|
215
|
2 |
|
{ |
|
216
|
|
|
return new self(Helper::match($this->value, $pattern)); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Replace a given value in the string. |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $pattern |
|
223
|
|
|
* @param string $replacement |
|
224
|
|
|
* |
|
225
|
4 |
|
* @return \Helldar\Support\Helpers\Stringable |
|
226
|
|
|
*/ |
|
227
|
4 |
|
public function pregReplace(string $pattern, string $replacement): self |
|
228
|
|
|
{ |
|
229
|
|
|
return new self(Helper::pregReplace($this->value, $pattern, $replacement)); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Transliterate a UTF-8 value to ASCII. |
|
234
|
|
|
* |
|
235
|
|
|
* @param string|null $language |
|
236
|
|
|
* |
|
237
|
|
|
* @return \Helldar\Support\Helpers\Stringable |
|
238
|
|
|
*/ |
|
239
|
|
|
public function ascii(?string $language = 'en'): self |
|
240
|
|
|
{ |
|
241
|
|
|
return new self(Helper::ascii($this->value, $language)); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|