From 8889fd152413c32d055b19452448adee752cc7aa Mon Sep 17 00:00:00 2001 From: localusercamp Date: Fri, 3 Mar 2023 22:32:01 +0500 Subject: [PATCH 1/3] [10.x] Add charAt method to both Str and Stringable --- src/Illuminate/Support/Str.php | 18 ++++++++++++++++++ src/Illuminate/Support/Stringable.php | 11 +++++++++++ tests/Support/SupportStrTest.php | 10 ++++++++++ tests/Support/SupportStringableTest.php | 10 ++++++++++ 4 files changed, 49 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index b62808f1b65b..727f1e814c03 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -216,6 +216,24 @@ public static function camel($value) return static::$camelCache[$value] = lcfirst(static::studly($value)); } + /** + * Get character at the specified index. + * + * @param string $subject + * @param int $index + * @return string|null + */ + public static function charAt($subject, $index) + { + $length = mb_strlen($subject); + + if ($index < 0 ? $index < -$length : $index > $length - 1) { + return null; + } + + return mb_substr($subject, $index, 1); + } + /** * Determine if a given string contains a given substring. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index db207f39b8b0..76bd04f5c404 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -109,6 +109,17 @@ public function classBasename() return new static(class_basename($this->value)); } + /** + * Get character at the specified index. + * + * @param int $index + * @return string|null + */ + public function charAt($index) + { + return Str::charAt($this->value, $index); + } + /** * Get the portion of a string before the first occurrence of a given value. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index babac7554c26..25cf6f9960ee 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -749,6 +749,16 @@ public function testCamel() $this->assertSame('fooBarBaz', Str::camel('foo-bar_baz')); } + public function testCharAt() + { + $this->assertEquals('р', Str::charAt('Привет, мир!', 1)); + $this->assertEquals('ち', Str::charAt('「こんにちは世界」', 4)); + $this->assertEquals('w', Str::charAt('Привет, world!', 8)); + $this->assertEquals('界', Str::charAt('「こんにちは世界」', -2)); + $this->assertEquals(null, Str::charAt('「こんにちは世界」', -200)); + $this->assertEquals(null, Str::charAt('Привет, мир!', 100)); + } + public function testSubstr() { $this->assertSame('Ё', Str::substr('БГДЖИЛЁ', -1)); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index cd0e6360f498..df7e5e482ed2 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -936,6 +936,16 @@ public function testCamel() $this->assertSame('fooBarBaz', (string) $this->stringable('foo-bar_baz')->camel()); } + public function testCharAt() + { + $this->assertEquals('р', $this->stringable('Привет, мир!')->charAt(1)); + $this->assertEquals('ち', $this->stringable('「こんにちは世界」')->charAt(4)); + $this->assertEquals('w', $this->stringable('Привет, world!')->charAt(8)); + $this->assertEquals('界', $this->stringable('「こんにちは世界」')->charAt(-2)); + $this->assertEquals(null, $this->stringable('「こんにちは世界」')->charAt(-200)); + $this->assertEquals(null, $this->stringable('Привет, мир!')->charAt('Привет, мир!', 100)); + } + public function testSubstr() { $this->assertSame('Ё', (string) $this->stringable('БГДЖИЛЁ')->substr(-1)); From 678c21a4fc89098dd376ccf46cd3f052e6e26df8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 5 Mar 2023 13:48:18 -0600 Subject: [PATCH 2/3] Update Str.php --- src/Illuminate/Support/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 727f1e814c03..bbe21250872a 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -217,7 +217,7 @@ public static function camel($value) } /** - * Get character at the specified index. + * Get the character at the specified index. * * @param string $subject * @param int $index From d7b0ed33882094d2c53eb1620392dde3d7916273 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 5 Mar 2023 13:48:41 -0600 Subject: [PATCH 3/3] Update Stringable.php --- src/Illuminate/Support/Stringable.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 76bd04f5c404..9a03d4a36143 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -100,24 +100,24 @@ public function basename($suffix = '') } /** - * Get the basename of the class path. + * Get the character at the specified index. * - * @return static + * @param int $index + * @return string|null */ - public function classBasename() + public function charAt($index) { - return new static(class_basename($this->value)); + return Str::charAt($this->value, $index); } /** - * Get character at the specified index. + * Get the basename of the class path. * - * @param int $index - * @return string|null + * @return static */ - public function charAt($index) + public function classBasename() { - return Str::charAt($this->value, $index); + return new static(class_basename($this->value)); } /**