Skip to content

Commit 821e55a

Browse files
antonch1989fabpot
authored andcommitted
[Finder] Get filename without extension
1 parent 468bca8 commit 821e55a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Symfony/Component/Finder/SplFileInfo.php

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public function getRelativePathname()
5757
return $this->relativePathname;
5858
}
5959

60+
public function getFilenameWithoutExtension(): string
61+
{
62+
$filename = $this->getFilename();
63+
64+
return \pathinfo($filename, PATHINFO_FILENAME);
65+
}
66+
6067
/**
6168
* Returns the contents of the file.
6269
*

src/Symfony/Component/Finder/Tests/FinderTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,40 @@ public function testRelativePathname()
10071007
$this->assertEquals($ref, $paths);
10081008
}
10091009

1010+
public function testGetFilenameWithoutExtension()
1011+
{
1012+
$finder = $this->buildFinder()->in(self::$tmpDir)->sortByName();
1013+
1014+
$fileNames = [];
1015+
1016+
foreach ($finder as $file) {
1017+
$fileNames[] = $file->getFilenameWithoutExtension();
1018+
}
1019+
1020+
$ref = [
1021+
'test',
1022+
'toto',
1023+
'test',
1024+
'foo',
1025+
'bar',
1026+
'foo bar',
1027+
'qux',
1028+
'baz_100_1',
1029+
'baz_1_2',
1030+
'qux_0_1',
1031+
'qux_1000_1',
1032+
'qux_1002_0',
1033+
'qux_10_2',
1034+
'qux_12_0',
1035+
'qux_2_0',
1036+
];
1037+
1038+
sort($fileNames);
1039+
sort($ref);
1040+
1041+
$this->assertEquals($ref, $fileNames);
1042+
}
1043+
10101044
public function testAppendWithAFinder()
10111045
{
10121046
$finder = $this->buildFinder();

0 commit comments

Comments
 (0)