Completed
Push — master ( 992921...3403b8 )
by recca
02:07
created

src/JString/Javascript.php (1 issue)

Labels

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 static
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
    }
29
30
    /**
31
     * The charAt() method returns the specified character from a string.
32
     *
33
     * @param  int $index
34
     * @return static
35
     */
36 2
    public function charAt($index = 0)
37
    {
38 2
        return ($this->length() > $index) ? $this->substr($index, 1) : '';
39
    }
40
41
    /**
42
     * The charCodeAt() method returns an int between 0 and 65535
43
     * representing the UTF-16 code unit at the given index
44
     * (the UTF-16 code unit matches the Unicode code point for code points representable in a single UTF-16 code unit,
45
     * but might also be the first code unit of a surrogate pair for code points not representable in a single UTF-16 code unit,
46
     * e.g. Unicode code points > 0x10000). If you want the entire code point value, use codePointAt().
47
     *
48
     * @param  int $index
49
     * @return int
50
     */
51 1
    public function charCodeAt($index = 0)
52
    {
53 1
        return ord($this->charAt($index));
54
    }
55
56
    /**
57
     * The codePointAt() method returns a non-negative int that is the Unicode code point value.
58
     *
59
     * @param  int $index
60
     * @return int
61
     */
62
    public function codePointAt($index = 0)
63
    {
64
    }
65
66
    /**
67
     * The concat() method combines the text of one or more strings and returns a new string.
68
     *
69
     * @return static
70
     */
71 1
    public function concat()
72
    {
73 1
        return (new JArray([$this->subject]))->concat(func_get_args())->join('');
1 ignored issue
show
The property subject does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
    }
75
76
    /**
77
     * The endsWith() method determines whether a string ends with the characters of another string,
78
     * returning true or false as appropriate.
79
     *
80
     * @param  string $searchString
81
     * @param  int $position
82
     * @return bool
83
     */
84 1
    public function endsWith($searchString, $position = null)
85
    {
86 1
        $length = $this->length();
87 1
        $position = is_null($position) === true || $position > $length ? $length : $position;
88 1
        $position -= strlen($searchString);
89 1
        $lastIndex = $this->lastIndexOf($searchString, $position);
90
91 1
        return $lastIndex !== -1 && $lastIndex === $position;
92
    }
93
94
    /**
95
     * The includes() method determines whether one string may be found within another string,
96
     * returning true or false as appropriate.
97
     *
98
     * @param  string  $searchString
99
     * @param  int $position
100
     * @return bool
101
     */
102 1
    public function includes($searchString, $position = 0)
103
    {
104 1
        if ($position + strlen($searchString) > $this->length()) {
105
            return false;
106
        }
107
108 1
        return $this->indexOf($searchString, $position) !== -1;
109
    }
110
111
    /**
112
     * The indexOf() method returns the index within the calling String object of the first occurrence of the specified value,
113
     * starting the search at fromIndex. Returns -1 if the value is not found.
114
     *
115
     * @param  string  $searchValue
116
     * @param  int $fromIndex
117
     * @return int
118
     */
119 2
    public function indexOf($searchValue, $fromIndex = 0)
120
    {
121 2
        if (empty($searchValue) === true) {
122 1
            return $fromIndex;
123
        }
124
125 2
        $result = strpos($this->subject, $searchValue, $fromIndex);
126
127 2
        return $result === false ? -1 : $result;
128
    }
129
130
    /**
131
     * The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value,
132
     * searching backwards from fromIndex. Returns -1 if the value is not found.
133
     *
134
     * @param  string $searchValue
135
     * @param  int $fromIndex
136
     * @return int
137
     */
138 2
    public function lastIndexOf($searchValue, $fromIndex = null)
139
    {
140 2
        if (empty($searchValue) === true) {
141 1
            return $fromIndex ?: $this->length();
142
        }
143
144 2
        if ($fromIndex === 0 || $fromIndex < 0) {
145 1
            return strrpos($this->subject, $searchValue, $fromIndex) !== 0 ? -1 : 0;
146
        }
147
148 2
        $fromIndex = $fromIndex ?: 0;
149 2
        $result = strrpos($this->subject, $searchValue, $fromIndex);
150
151 2
        return $result === false ? -1 : $result;
152
    }
153
154
    /**
155
     * The localeCompare() method returns a number indicating
156
     * whether a reference string comes before or after or is the same as the given string in sort order.
157
     *
158
     * @return int
159
     */
160
    public function localeCompare()
161
    {
162
    }
163
164
    /**
165
     * The match() method retrieves the matches when matching a string against a regular expression.
166
     *
167
     * @param  string  $regexp
168
     * @param  int  $flag
169
     * @param  int $offset
170
     * @return array
171
     */
172 1
    public function match($regexp, $flag = PREG_PATTERN_ORDER, $offset = 0)
173
    {
174 1
        if ((bool) preg_match_all($regexp, $this->subject, $matches, $flag, $offset) === false) {
175
            return;
176
        }
177
178 1
        return $matches;
179
    }
180
181
    /**
182
     * The padEnd() method pads the current string with a given string (possibly repeated)
183
     * so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
184
     *
185
     * @param  int $targetLength
186
     * @param  string $padString
187
     * @return static
188
     */
189
    public function padEnd($targetLength, $padString = ' ')
190
    {
191
        return $this->pad($targetLength, $padString, STR_PAD_RIGHT);
192
    }
193
194
    /**
195
     * The padStart() method pads the current string with a given string (eventually repeated)
196
     * so that the resulting string reaches a given length. The pad is applied from the start (left) of the current string.
197
     *
198
     * @param  int $targetLength
199
     * @param  string $padString
200
     * @return static
201
     */
202
    public function padStart($targetLength, $padString = ' ')
203
    {
204
        return $this->pad($targetLength, $padString, STR_PAD_LEFT);
205
    }
206
207
    /**
208
     * 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.
209
     *
210
     * @param  int $count
211
     * @return static
212
     */
213 1
    public function repeat($count)
214
    {
215 1
        return new static(str_repeat($this->subject, $count));
216
    }
217
218
    /**
219
     * 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.
220
     *
221
     * @param  string $regexp
222
     * @param  mix $replacement
223
     * @return static
224
     */
225 1
    public function replace($regexp, $replacement = null)
226
    {
227 1
        if (is_array($regexp) === true && is_null($replacement) === true) {
228 1
            return new static(strtr($this->subject, $regexp));
229
        }
230
231 1
        if (is_callable($replacement) === true) {
232 1
            return new static(preg_replace_callback($regexp, $replacement, $this->subject));
233
        }
234
235 1
        return new static(preg_replace($regexp, $replacement, $this->subject));
236
    }
237
238
    /**
239
     * The search() method executes a search for a match between a regular expression and this String object.
240
     *
241
     * @param  string $regexp
242
     * @return int
243
     */
244 1
    public function search($regexp)
245
    {
246 1
        if ((bool) preg_match($regexp, $this->subject, $match, PREG_OFFSET_CAPTURE) === false) {
247 1
            return -1;
248
        }
249
250 1
        return $match[0][1];
251
    }
252
253
    /**
254
     * The slice() method extracts a section of a string and returns a new string.
255
     *
256
     * @param  int $startSlice
257
     * @param  int $endSlice
258
     * @return static
259
     */
260 2
    public function slice($startSlice, $endSlice = null)
261
    {
262 2
        $length = $this->length();
263 2
        $startSlice = $startSlice < 0 ? $length + $startSlice : $startSlice;
264
265 2
        if (is_null($endSlice) === true) {
266 1
            return $this->substr($startSlice, $length - $startSlice);
267
        }
268
269 2
        $endSlice = $endSlice < 0 ? $length + $endSlice : $endSlice;
270 2
        $endSlice -= $startSlice;
271
272 2
        return $this->substr($startSlice, $endSlice);
273
    }
274
275
    /**
276
     * The split() method splits a String object into an array of strings by separating the string into substrings.
277
     *
278
     * @param  string|null $separator
279
     * @return Recca0120\LoDash\Arr
280
     */
281 1
    public function split($separator = null)
282
    {
283 1
        if (is_null($separator) === true) {
284 1
            return [$this->subject];
285
        }
286
287 1
        return new JArray(explode($separator, $this->subject));
288
    }
289
290
    /**
291
     * The startsWith() method determines whether a string begins with the characters of another string, returning true or false as appropriate.
292
     *
293
     * @param  string  $searchString
294
     * @param  int $position
295
     * @return bool
296
     */
297 1
    public function startsWith($searchString, $position = 0)
298
    {
299 1
        return $this->substr($position, strlen($searchString))->value() === $searchString;
300
    }
301
302
    /**
303
     * The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.
304
     *
305
     * @return static
306
     */
307 6
    public function substr()
308
    {
309 6
        $result = call_user_func_array('substr', array_merge([$this->subject], func_get_args()));
310 6
        $result = $result === false ? '' : $result;
311
312 6
        return new static($result);
313
    }
314
315
    /**
316
     * The substring() method returns a subset of a string between one index and another, or through the end of the string.
317
     *
318
     * @param  int $indexStart
319
     * @param  int $indexEnd
320
     * @return static
321
     */
322 1
    public function substring($indexStart, $indexEnd = null)
323
    {
324 1
        if (is_null($indexEnd) === true) {
325 1
            $indexEnd = $this->length();
326 1
        }
327
328 1
        $temp = [$indexStart, $indexEnd];
329 1
        sort($temp);
330
331 1
        return $this->slice($temp[0], $temp[1]);
332
    }
333
334
    /**
335
     * The toLocaleLowerCase() method returns the calling string value converted to lower case, according to any locale-specific case mappings.
336
     *
337
     * @return static
338
     */
339
    public function toLocaleLowerCase()
340
    {
341
        return $this->toLowerCase();
342
    }
343
344
    /**
345
     * The toLocaleUpperCase() method returns the calling string value converted to upper case, according to any locale-specific case mappings.
346
     *
347
     * @return static
348
     */
349
    public function toLocaleUpperCase()
350
    {
351
        return $this->toUpperCase();
352
    }
353
354
    /**
355
     * The toLowerCase() method returns the calling string value converted to lower case.
356
     *
357
     * @return static
358
     */
359 1
    public function toLowerCase()
360
    {
361 1
        return new static(strtolower($this->subject));
362
    }
363
364
    /**
365
     * The toString() method returns a string representing the specified object.
366
     *
367
     * @return string
368
     */
369
    public function toString()
370
    {
371
        return $this->subject;
372
    }
373
374
    /**
375
     * The toUpperCase() method returns the calling string value converted to upper case.
376
     *
377
     * @return static
378
     */
379 1
    public function toUpperCase()
380
    {
381 1
        return new static(strtoupper($this->subject));
382
    }
383
384
    /**
385
     * 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.).
386
     *
387
     * @param  string $characterMask
388
     * @return static
389
     */
390 1
    public function trim($characterMask = " \t\n\r\0\x0B")
391
    {
392 1
        return new static(trim($this->subject, $characterMask));
393
    }
394
395
    /**
396
     * The trimLeft() method removes whitespace from the left end of a string.
397
     *
398
     * @param  string $characterMask [description]
399
     * @return static
400
     */
401 1
    public function trimLeft($characterMask = " \t\n\r\0\x0B")
402
    {
403 1
        return $this->ltrim($characterMask);
404
    }
405
406
    /**
407
     * The trimRight() method removes whitespace from the right end of a string.
408
     *
409
     * @param  string $characterMask
410
     * @return static
411
     */
412 1
    public function trimRight($characterMask = " \t\n\r\0\x0B")
413
    {
414 1
        return $this->rtrim($characterMask);
415
    }
416
417
    /**
418
     * The valueOf() method returns the primitive value of a String object.
419
     *
420
     * @return string
421
     */
422 1
    public function valueOf()
423
    {
424 1
        return $this->subject;
425
    }
426
427
    /**
428
     * length.
429
     *
430
     * @return int
431
     */
432 7
    public function length()
433
    {
434 7
        return strlen($this->subject);
435
    }
436
}
437