Skip to content

Commit 4ed58f8

Browse files
committed
Added String.substr() @deprecated but available
1 parent 6bbdea6 commit 4ed58f8

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

Tasks.todo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ JSPHP:
4343
☐ String.slice()
4444
☐ String.split()
4545
☐ String.startsWith()
46+
✔ String.substr() @done (01/07/2024 20:53:17)
4647
✔ String.substring() @done (06/01/2024 16:00:51)
4748
☐ String.toLocaleLowerCase()
4849
☐ String.toLocaleUpperCase()

src/Globals/JSString.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,31 @@ function padEnd(int $maxLength, string $fillString = ' '): self {
273273
function split($separator, ?int $limit = null): JSArray {
274274
return JSArray(...explode($separator, $this->value));
275275
}
276+
277+
/**
278+
* Gets a substring beginning at the specified location and having the specified length.
279+
* @deprecated A legacy feature for browser compatibility
280+
* @param int $from The starting position of the desired substring. The index of the first character in the string is zero.
281+
* @param ?int $length The number of characters to include in the returned substring.
282+
*/
283+
function substr($from = 0, $length = null): self {
284+
$params = [$this->value, $from];
285+
286+
if ($length !== null) {
287+
$params[] = $length;
288+
}
289+
290+
if (
291+
$length < 0
292+
|| Number::isNaN($from)
293+
|| Number::isNaN($length)
294+
|| $from + ($length ?? 0) >= $this->length
295+
) {
296+
return new self;
297+
}
298+
299+
return new self(substr(...$params));
300+
}
276301
}
277302

278303
/**

tests/PHP/JSString/substrTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\PHP\JSString;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class substrTest extends TestCase {
10+
function test_Demo_String_substr(): void {
11+
$str = String('Mozilla');
12+
13+
self::assertEquals('oz', $str->substr(1, 2));
14+
self::assertEquals('zilla', $str->substr(2));
15+
}
16+
17+
function test_Description(): void {
18+
$str = String('Mozilla');
19+
20+
// If start >= str.length, an empty string is returned.
21+
self::assertEquals('', $str->substr(7));
22+
23+
// If start < 0, the index starts counting from the end of the string. More formally, in this case the substring starts at max(start + str.length, 0).
24+
self::assertEquals('a', $str->substr(-1));
25+
self::assertEquals($str, $str->substr(-7));
26+
self::assertEquals($str, $str->substr(-8));
27+
28+
// If start is omitted or undefined, it's treated as 0.
29+
self::assertEquals($str, $str->substr());
30+
31+
// TODO: If length is omitted or undefined, or if start + length >= str.length, substr() extracts characters to the end of the string.
32+
33+
// If length < 0, an empty string is returned.
34+
self::assertEquals('', $str->substr(0, -1));
35+
36+
// For both start and length, NaN is treated as 0.
37+
self::assertEquals('', $str->substr(0, NaN));
38+
self::assertEquals('', $str->substr(NaN));
39+
self::assertEquals('', $str->substr(NaN, NaN));
40+
}
41+
42+
function test_Using_substr(): void {
43+
$aString = String('Mozilla');
44+
45+
self::assertEquals('M', $aString->substr(0, 1));
46+
self::assertEquals('', $aString->substr(1, 0));
47+
self::assertEquals('a', $aString->substr(-1, 1));
48+
self::assertEquals('', $aString->substr(1, -1));
49+
self::assertEquals('lla', $aString->substr(-3));
50+
self::assertEquals('ozilla', $aString->substr(1));
51+
self::assertEquals('Mo', $aString->substr(-20, 2));
52+
self::assertEquals('', $aString->substr(20, 2));
53+
}
54+
}

tests/PHP/JSString/substringTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function test_The_difference_between_substring_and_substr(): void {
3939
$text = String('Mozilla');
4040

4141
self::assertEquals('zil', $text->substring(2, 5));
42-
// self::assertEquals('zil', $text->substr(2, 3)); // TODO
42+
self::assertEquals('zil', $text->substr(2, 3));
4343
}
4444

4545
function test_Differences_between_substring_and_slice(): void {

0 commit comments

Comments
 (0)