|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recca0120\Lodash\JString; |
|
4
|
|
|
|
|
5
|
|
|
use Recca0120\Lodash\JArray; |
|
6
|
|
|
|
|
7
|
|
|
trait Javascript |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The static String.fromCharCode() method returns a string created by using the specified sequence of Unicode values. |
|
11
|
|
|
* |
|
12
|
|
|
* @return JString |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function fromCharCode() |
|
15
|
|
|
{ |
|
16
|
1 |
|
return (new JArray(func_get_args()))->map(function ($code) { |
|
17
|
1 |
|
return chr($code); |
|
18
|
1 |
|
})->join(''); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The static String.fromCodePoint() method returns a string created by using the specified sequence of code points. |
|
23
|
|
|
* |
|
24
|
|
|
* @return static |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function fromCodePoint() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this; |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The charAt() method returns the specified character from a string. |
|
33
|
|
|
* |
|
34
|
|
|
* @param int $index |
|
35
|
|
|
* @return static |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function charAt($index = 0) |
|
38
|
|
|
{ |
|
39
|
2 |
|
return ($this->length() > $index) ? $this->substr($index, 1) : ''; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The charCodeAt() method returns an int between 0 and 65535 |
|
44
|
|
|
* representing the UTF-16 code unit at the given index |
|
45
|
|
|
* (the UTF-16 code unit matches the Unicode code point for code points representable in a single UTF-16 code unit, |
|
46
|
|
|
* but might also be the first code unit of a surrogate pair for code points not representable in a single UTF-16 code unit, |
|
47
|
|
|
* e.g. Unicode code points > 0x10000). If you want the entire code point value, use codePointAt(). |
|
48
|
|
|
* |
|
49
|
|
|
* @param int $index |
|
50
|
|
|
* @return int |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function charCodeAt($index = 0) |
|
53
|
|
|
{ |
|
54
|
1 |
|
return ord($this->charAt($index)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The codePointAt() method returns a non-negative int that is the Unicode code point value. |
|
59
|
|
|
* |
|
60
|
|
|
* @param int $index |
|
61
|
|
|
* @return int |
|
62
|
|
|
*/ |
|
63
|
|
|
public function codePointAt($index = 0) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The concat() method combines the text of one or more strings and returns a new string. |
|
69
|
|
|
* |
|
70
|
|
|
* @return JString |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function concat() |
|
73
|
|
|
{ |
|
74
|
1 |
|
return (new JArray([$this->subject]))->concat(func_get_args())->join(''); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The endsWith() method determines whether a string ends with the characters of another string, |
|
79
|
|
|
* returning true or false as appropriate. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $searchString |
|
82
|
|
|
* @param int $position |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function endsWith($searchString, $position = null) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$length = $this->length(); |
|
88
|
1 |
|
$position = is_null($position) === true || $position > $length ? $length : $position; |
|
89
|
1 |
|
$position -= strlen($searchString); |
|
90
|
1 |
|
$lastIndex = $this->lastIndexOf($searchString, $position); |
|
91
|
|
|
|
|
92
|
1 |
|
return $lastIndex !== -1 && $lastIndex === $position; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* The includes() method determines whether one string may be found within another string, |
|
97
|
|
|
* returning true or false as appropriate. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $searchString |
|
100
|
|
|
* @param int $position |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function includes($searchString, $position = 0) |
|
104
|
|
|
{ |
|
105
|
1 |
|
if ($position + strlen($searchString) > $this->length()) { |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
return $this->indexOf($searchString, $position) !== -1; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, |
|
114
|
|
|
* starting the search at fromIndex. Returns -1 if the value is not found. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $searchValue |
|
117
|
|
|
* @param int $fromIndex |
|
118
|
|
|
* @return int |
|
119
|
|
|
*/ |
|
120
|
2 |
|
public function indexOf($searchValue, $fromIndex = 0) |
|
121
|
|
|
{ |
|
122
|
2 |
|
if (empty($searchValue) === true) { |
|
123
|
1 |
|
return $fromIndex; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
$result = strpos($this->subject, $searchValue, $fromIndex); |
|
127
|
|
|
|
|
128
|
2 |
|
return $result === false ? -1 : $result; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, |
|
133
|
|
|
* searching backwards from fromIndex. Returns -1 if the value is not found. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $searchValue |
|
136
|
|
|
* @param int $fromIndex |
|
137
|
|
|
* @return int |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function lastIndexOf($searchValue, $fromIndex = null) |
|
140
|
|
|
{ |
|
141
|
2 |
|
if (empty($searchValue) === true) { |
|
142
|
1 |
|
return $fromIndex ?: $this->length(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
if ($fromIndex === 0 || $fromIndex < 0) { |
|
146
|
1 |
|
return strrpos($this->subject, $searchValue, $fromIndex) !== 0 ? -1 : 0; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
$fromIndex = $fromIndex ?: 0; |
|
150
|
2 |
|
$result = strrpos($this->subject, $searchValue, $fromIndex); |
|
151
|
|
|
|
|
152
|
2 |
|
return $result === false ? -1 : $result; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* The localeCompare() method returns a number indicating |
|
157
|
|
|
* whether a reference string comes before or after or is the same as the given string in sort order. |
|
158
|
|
|
* |
|
159
|
|
|
* @return int |
|
160
|
|
|
*/ |
|
161
|
|
|
public function localeCompare() |
|
162
|
|
|
{ |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* The match() method retrieves the matches when matching a string against a regular expression. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $regexp |
|
169
|
|
|
* @param int $flag |
|
170
|
|
|
* @param int $offset |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
1 |
|
public function match($regexp, $flag = PREG_PATTERN_ORDER, $offset = 0) |
|
174
|
|
|
{ |
|
175
|
1 |
|
if ((bool) preg_match_all($regexp, $this->subject, $matches, $flag, $offset) === false) { |
|
176
|
|
|
return; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
return $matches; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* The padEnd() method pads the current string with a given string (possibly repeated) |
|
184
|
|
|
* so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string. |
|
185
|
|
|
* |
|
186
|
|
|
* @param int $targetLength |
|
187
|
|
|
* @param string $padString |
|
188
|
|
|
* @return static |
|
189
|
|
|
*/ |
|
190
|
|
|
public function padEnd($targetLength, $padString = ' ') |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->pad($targetLength, $padString, STR_PAD_RIGHT); |
|
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* The padStart() method pads the current string with a given string (eventually repeated) |
|
197
|
|
|
* so that the resulting string reaches a given length. The pad is applied from the start (left) of the current string. |
|
198
|
|
|
* |
|
199
|
|
|
* @param int $targetLength |
|
200
|
|
|
* @param string $padString |
|
201
|
|
|
* @return static |
|
202
|
|
|
*/ |
|
203
|
|
|
public function padStart($targetLength, $padString = ' ') |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->pad($targetLength, $padString, STR_PAD_LEFT); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together. |
|
210
|
|
|
* |
|
211
|
|
|
* @param int $count |
|
212
|
|
|
* @return static |
|
213
|
|
|
*/ |
|
214
|
1 |
|
public function repeat($count) |
|
215
|
|
|
{ |
|
216
|
1 |
|
return new static(str_repeat($this->subject, $count)); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $regexp |
|
223
|
|
|
* @param mixed $replacement |
|
224
|
|
|
* @return static |
|
225
|
|
|
*/ |
|
226
|
1 |
|
public function replace($regexp, $replacement = null) |
|
227
|
|
|
{ |
|
228
|
1 |
|
if (is_array($regexp) === true && is_null($replacement) === true) { |
|
229
|
1 |
|
return new static(strtr($this->subject, $regexp)); |
|
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
1 |
|
if (is_callable($replacement) === true) { |
|
233
|
1 |
|
return new static(preg_replace_callback($regexp, $replacement, $this->subject)); |
|
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
1 |
|
return new static(preg_replace($regexp, $replacement, $this->subject)); |
|
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* The search() method executes a search for a match between a regular expression and this String object. |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $regexp |
|
243
|
|
|
* @return int |
|
244
|
|
|
*/ |
|
245
|
1 |
|
public function search($regexp) |
|
246
|
|
|
{ |
|
247
|
1 |
|
if ((bool) preg_match($regexp, $this->subject, $match, PREG_OFFSET_CAPTURE) === false) { |
|
248
|
1 |
|
return -1; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
1 |
|
return $match[0][1]; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* The slice() method extracts a section of a string and returns a new string. |
|
256
|
|
|
* |
|
257
|
|
|
* @param int $startSlice |
|
258
|
|
|
* @param int $endSlice |
|
259
|
|
|
* @return static |
|
260
|
|
|
*/ |
|
261
|
2 |
|
public function slice($startSlice, $endSlice = null) |
|
262
|
|
|
{ |
|
263
|
2 |
|
$length = $this->length(); |
|
264
|
2 |
|
$startSlice = $startSlice < 0 ? $length + $startSlice : $startSlice; |
|
265
|
|
|
|
|
266
|
2 |
|
if (is_null($endSlice) === true) { |
|
267
|
1 |
|
return $this->substr($startSlice, $length - $startSlice); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
2 |
|
$endSlice = $endSlice < 0 ? $length + $endSlice : $endSlice; |
|
271
|
2 |
|
$endSlice -= $startSlice; |
|
272
|
|
|
|
|
273
|
2 |
|
return $this->substr($startSlice, $endSlice); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* The split() method splits a String object into an array of strings by separating the string into substrings. |
|
278
|
|
|
* |
|
279
|
|
|
* @param string|null $separator |
|
280
|
|
|
* @return Recca0120\Lodash\Arr |
|
281
|
|
|
*/ |
|
282
|
1 |
|
public function split($separator = null) |
|
283
|
|
|
{ |
|
284
|
1 |
|
if (is_null($separator) === true) { |
|
285
|
1 |
|
return [$this->subject]; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
1 |
|
return new JArray(explode($separator, $this->subject)); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* The startsWith() method determines whether a string begins with the characters of another string, returning true or false as appropriate. |
|
293
|
|
|
* |
|
294
|
|
|
* @param string $searchString |
|
295
|
|
|
* @param int $position |
|
296
|
|
|
* @return bool |
|
297
|
|
|
*/ |
|
298
|
1 |
|
public function startsWith($searchString, $position = 0) |
|
299
|
|
|
{ |
|
300
|
1 |
|
return $this->substr($position, strlen($searchString))->value() === $searchString; |
|
|
|
|
|
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* The substr() method returns the characters in a string beginning at the specified location through the specified number of characters. |
|
305
|
|
|
* |
|
306
|
|
|
* @return static |
|
307
|
|
|
*/ |
|
308
|
6 |
|
public function substr() |
|
309
|
|
|
{ |
|
310
|
6 |
|
$result = call_user_func_array('substr', array_merge([$this->subject], func_get_args())); |
|
311
|
6 |
|
$result = $result === false ? '' : $result; |
|
312
|
|
|
|
|
313
|
6 |
|
return new static($result); |
|
|
|
|
|
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* The substring() method returns a subset of a string between one index and another, or through the end of the string. |
|
318
|
|
|
* |
|
319
|
|
|
* @param int $indexStart |
|
320
|
|
|
* @param int $indexEnd |
|
321
|
|
|
* @return static |
|
322
|
|
|
*/ |
|
323
|
1 |
|
public function substring($indexStart, $indexEnd = null) |
|
324
|
|
|
{ |
|
325
|
1 |
|
if (is_null($indexEnd) === true) { |
|
326
|
1 |
|
$indexEnd = $this->length(); |
|
327
|
1 |
|
} |
|
328
|
|
|
|
|
329
|
1 |
|
$temp = [$indexStart, $indexEnd]; |
|
330
|
1 |
|
sort($temp); |
|
331
|
|
|
|
|
332
|
1 |
|
return $this->slice($temp[0], $temp[1]); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* The toLocaleLowerCase() method returns the calling string value converted to lower case, according to any locale-specific case mappings. |
|
337
|
|
|
* |
|
338
|
|
|
* @return static |
|
339
|
|
|
*/ |
|
340
|
|
|
public function toLocaleLowerCase() |
|
341
|
|
|
{ |
|
342
|
|
|
return $this->toLowerCase(); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* The toLocaleUpperCase() method returns the calling string value converted to upper case, according to any locale-specific case mappings. |
|
347
|
|
|
* |
|
348
|
|
|
* @return static |
|
349
|
|
|
*/ |
|
350
|
|
|
public function toLocaleUpperCase() |
|
351
|
|
|
{ |
|
352
|
|
|
return $this->toUpperCase(); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* The toLowerCase() method returns the calling string value converted to lower case. |
|
357
|
|
|
* |
|
358
|
|
|
* @return static |
|
359
|
|
|
*/ |
|
360
|
1 |
|
public function toLowerCase() |
|
361
|
|
|
{ |
|
362
|
1 |
|
return new static(strtolower($this->subject)); |
|
|
|
|
|
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* The toString() method returns a string representing the specified object. |
|
367
|
|
|
* |
|
368
|
|
|
* @return string |
|
369
|
|
|
*/ |
|
370
|
|
|
public function toString() |
|
371
|
|
|
{ |
|
372
|
|
|
return $this->subject; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* The toUpperCase() method returns the calling string value converted to upper case. |
|
377
|
|
|
* |
|
378
|
|
|
* @return static |
|
379
|
|
|
*/ |
|
380
|
1 |
|
public function toUpperCase() |
|
381
|
|
|
{ |
|
382
|
1 |
|
return new static(strtoupper($this->subject)); |
|
|
|
|
|
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.). |
|
387
|
|
|
* |
|
388
|
|
|
* @param string $characterMask |
|
389
|
|
|
* @return static |
|
390
|
|
|
*/ |
|
391
|
1 |
|
public function trim($characterMask = " \t\n\r\0\x0B") |
|
392
|
|
|
{ |
|
393
|
1 |
|
return new static(trim($this->subject, $characterMask)); |
|
|
|
|
|
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* The trimLeft() method removes whitespace from the left end of a string. |
|
398
|
|
|
* |
|
399
|
|
|
* @param string $characterMask |
|
400
|
|
|
* @return static |
|
401
|
|
|
*/ |
|
402
|
1 |
|
public function trimLeft($characterMask = " \t\n\r\0\x0B") |
|
403
|
|
|
{ |
|
404
|
1 |
|
return $this->ltrim($characterMask); |
|
|
|
|
|
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* The trimRight() method removes whitespace from the right end of a string. |
|
409
|
|
|
* |
|
410
|
|
|
* @param string $characterMask |
|
411
|
|
|
* @return static |
|
412
|
|
|
*/ |
|
413
|
1 |
|
public function trimRight($characterMask = " \t\n\r\0\x0B") |
|
414
|
|
|
{ |
|
415
|
1 |
|
return $this->rtrim($characterMask); |
|
|
|
|
|
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* The valueOf() method returns the primitive value of a String object. |
|
420
|
|
|
* |
|
421
|
|
|
* @return string |
|
422
|
|
|
*/ |
|
423
|
1 |
|
public function valueOf() |
|
424
|
|
|
{ |
|
425
|
1 |
|
return $this->subject; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* length. |
|
430
|
|
|
* |
|
431
|
|
|
* @return int |
|
432
|
|
|
*/ |
|
433
|
7 |
|
public function length() |
|
434
|
|
|
{ |
|
435
|
7 |
|
return strlen($this->subject); |
|
436
|
|
|
} |
|
437
|
|
|
} |
|
438
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.