From 3ac79d687eb172e3762bcf2dcc86c9bc899acd49 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Wed, 25 Sep 2024 13:59:24 +0200 Subject: [PATCH] feat: add str::startswith and endswith (#484) --- src/Tempest/Support/src/StringHelper.php | 10 ++++++++++ src/Tempest/Support/tests/StringHelperTest.php | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Tempest/Support/src/StringHelper.php b/src/Tempest/Support/src/StringHelper.php index d0a093573..a423cbed2 100644 --- a/src/Tempest/Support/src/StringHelper.php +++ b/src/Tempest/Support/src/StringHelper.php @@ -221,4 +221,14 @@ public function classBasename(): self { return new self(basename(str_replace('\\', '/', $this->string))); } + + public function startsWith(string $needle): bool + { + return str_starts_with($this->string, $needle); + } + + public function endsWith(string $needle): bool + { + return str_ends_with($this->string, $needle); + } } diff --git a/src/Tempest/Support/tests/StringHelperTest.php b/src/Tempest/Support/tests/StringHelperTest.php index d68a0bd8c..d3f3667e1 100644 --- a/src/Tempest/Support/tests/StringHelperTest.php +++ b/src/Tempest/Support/tests/StringHelperTest.php @@ -189,4 +189,16 @@ public function test_str_before_last(): void $this->assertTrue(str('tempest framework')->beforeLast(' ')->equals('tempest')); $this->assertTrue(str("yvette\tyv0et0te")->beforeLast("\t")->equals('yvette')); } + + public function test_starts_with(): void + { + $this->assertTrue(str('abc')->startsWith('a')); + $this->assertFalse(str('abc')->startsWith('c')); + } + + public function test_ends_with(): void + { + $this->assertTrue(str('abc')->endsWith('c')); + $this->assertFalse(str('abc')->endsWith('a')); + } }