Skip to content

Commit 1d4f563

Browse files
authored
feat(support): add basename to string utils (#1039)
1 parent f5e38d0 commit 1d4f563

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Tempest/Support/src/Str/ManipulatesString.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ public function lowerFirst(): static
217217
return $this->createOrModify(lower_first($this->value));
218218
}
219219

220+
/**
221+
* Keeps only the base name of the instance.
222+
*/
223+
public function basename(string $suffix = ''): static
224+
{
225+
return $this->createOrModify(basename($this->value, $suffix));
226+
}
227+
220228
/**
221229
* Keeps only the base name of the instance, assuming the instance is a class name.
222230
*/

src/Tempest/Support/tests/Str/ManipulatesStringTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,4 +648,19 @@ public function test_tap(): void
648648
$this->assertSame($string, $result);
649649
$this->assertEquals('foo', $log);
650650
}
651+
652+
public function test_class_basename(): void
653+
{
654+
$this->assertSame('ImmutableString', str(ImmutableString::class)->classBasename()->toString());
655+
$this->assertSame('ImmutableString', str('ImmutableString')->classBasename()->toString());
656+
$this->assertSame('', str()->classBasename()->toString());
657+
}
658+
659+
public function test_basename(): void
660+
{
661+
$this->assertSame('file.txt', str('path/to/file.txt')->basename()->toString());
662+
$this->assertSame('file.txt', str('file.txt')->basename()->toString());
663+
$this->assertSame('file', str('file.txt')->basename('.txt')->toString());
664+
$this->assertSame('', str()->basename()->toString());
665+
}
651666
}

0 commit comments

Comments
 (0)