Skip to content

Commit

Permalink
feat(documentator): Added Text::getPathTitle() (converts filename i…
Browse files Browse the repository at this point in the history
…nto document title).
  • Loading branch information
LastDragon-ru committed Aug 13, 2024
1 parent 2d18aa5 commit cbb7458
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/documentator/src/Utils/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
use function mb_strlen;
use function mb_substr;
use function min;
use function pathinfo;
use function preg_replace;
use function preg_split;
use function rtrim;
use function str_repeat;
use function str_replace;
use function str_starts_with;
use function trim;

use const PATHINFO_FILENAME;
use const PHP_INT_MAX;

class Text {
Expand Down Expand Up @@ -56,4 +61,14 @@ public static function setPadding(string $text, int $level, string $padding = '
public static function getLines(string $text): array {
return preg_split('/\R/u', $text) ?: [];
}

public static function getPathTitle(string $path): string {
$title = pathinfo($path, PATHINFO_FILENAME);
$title = str_replace(['_', '.'], ' ', $title);
$title = (string) preg_replace('/(\p{Ll})(\p{Lu})/u', '$1 $2', $title);
$title = (string) preg_replace('/\s+/u', ' ', $title);
$title = trim($title);

return $title;
}
}
7 changes: 7 additions & 0 deletions packages/documentator/src/Utils/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public function testGetLines(): void {
Text::getLines("a\n\nb\r\nc\nd\r\re"),
);
}

public function testGetPathTitle(): void {
self::assertEquals('file', Text::getPathTitle('path/to/file.txt'));
self::assertEquals('file Name', Text::getPathTitle('path/to/fileName.txt'));
self::assertEquals('File name second', Text::getPathTitle('path/to/File name.second.txt'));
self::assertEquals('File name', Text::getPathTitle('path/to/File name'));
}
//</editor-fold>

// <editor-fold desc="DataProviders">
Expand Down

0 comments on commit cbb7458

Please sign in to comment.