Skip to content

Commit

Permalink
Added getLast method to the Compass\Path class, which returns the des…
Browse files Browse the repository at this point in the history
…tination directory or file name. Updated tests and README.md
  • Loading branch information
vinogradsoft committed Oct 20, 2023
1 parent c86cf39 commit 2665f85
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ echo '<br>', $path; # newPath/newTo/file.v
$path->replace('newPath','path');
$path->updateSource();
echo '<br>', $path; # path/newTo/file.v
echo '<br>', $path->getLast(); # file.v
```

## Тестировать
Expand Down
12 changes: 10 additions & 2 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ public static function createBlank(string $separator = DIRECTORY_SEPARATOR, ?Pat
}

/**
* @param bool $withSuffix
* @return string|null
*/
public function getLast(): ?string
public function getLast(bool $withSuffix = true): ?string
{
return $this->items[count($this->items) - 1] ?? null;
$lastItem = $this->items[count($this->items) - 1] ?? null;
if ($lastItem === null) {
return null;
}
if ($withSuffix && !empty($this->suffix)) {
return $lastItem . $this->suffix;
}
return $lastItem;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function testGetAssertionNegative()
/**
* @dataProvider getCasesGet
*/
public function testGetLast($source, $expect)
public function testGetLast($source, $withSuffix, $expect)
{
$path = new Path($source);
self::assertEquals($expect, $path->getLast());
self::assertEquals($expect, $path->getLast($withSuffix));
}

/**
Expand All @@ -49,10 +49,10 @@ public function testGetLast($source, $expect)
public function getCasesGet(): array
{
return [
'standard' => ['/src/Scanner/Driver/File/index.php', 'index.php'],
'dot' => ['/src/Scanner/Driver/File/.php', '.php'],
'no dot' => ['/src/Scanner/Driver/File/name', 'name'],
'empty' => ['/', null],
'standard' => ['/src/Scanner/Driver/File/index.php', true, 'index.php'],
'dot' => ['/src/Scanner/Driver/File/.php', true, '.php'],
'no dot' => ['/src/Scanner/Driver/File/name', true, 'name'],
'empty' => ['/', true, null],
];
}

Expand Down

0 comments on commit 2665f85

Please sign in to comment.