Skip to content

Commit

Permalink
Add custom startsWith again
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed May 19, 2022
1 parent 80eefb5 commit aed91d0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Helpers/ActiveUrlChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function check(string $url): bool
$itemPath = Str::ensureRight('/', $url->getPath());

// If this url doesn't start with the rootPath, it's inactive.
if (! str_starts_with($itemPath, $rootPath)) {
if (! Str::startsWith($itemPath, $rootPath)) {
return false;
}

Expand All @@ -48,7 +48,7 @@ public function check(string $url): bool
$matchPath = Str::removeFromStart($rootPath, $matchPath);

// If this url starts with the url we're matching with, it's active.
if ($matchPath === $itemPath || str_starts_with($matchPath, $itemPath)) {
if ($matchPath === $itemPath || Str::startsWith($matchPath, $itemPath)) {
return true;
}

Expand Down
11 changes: 10 additions & 1 deletion src/Helpers/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

class Str
{
public static function startsWith(string $haystack, string $needle): bool
{
if ($needle != '' && substr($haystack, 0, strlen($needle)) === $needle) {
return true;
}

return false;
}

public static function removeFromStart(string $remove, string $subject): string
{
if (! str_starts_with($subject, $remove)) {
if (! self::startsWith($subject, $remove)) {
return $subject;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/NavigationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function test_it_can_get_the_active_section()

public function test_it_returns_null_when_there_is_no_active_section()
{
$activeSection = (new Navigation($this->activeUrlChecker))->add('Home', 'home')->activeSection();
$activeSection = (new Navigation($this->activeUrlChecker))->add('Home', '/')->activeSection();

$this->assertNull($activeSection);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

class StrTest extends TestCase
{
/** @test * */
public function it_can_check_if_string_starts_with()
{
$this->assertTrue(Str::startsWith('startEnd', 'start'));
$this->assertFalse(Str::startsWith('startEnd', 'end'));
$this->assertFalse(Str::startsWith('/', '/foo'));
$this->assertTrue(Str::startsWith('/foo', '/'));
}

/** @test * */
public function it_can_remove_from_start()
{
Expand Down

0 comments on commit aed91d0

Please sign in to comment.