Skip to content

Commit 23261d9

Browse files
committed
Improve the fileExtension expression
1 parent c5a830b commit 23261d9

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/FileExtension.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,26 @@ private function compile(string $value): string
2121
{
2222
return <<<PHP
2323
(function () use (\$input) : string {
24-
return \\pathinfo($value, \FILEINFO_EXTENSION)
24+
\$validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'pdf', 'mp4', 'webm', 'mp3'];
25+
26+
\$extension = \\pathinfo({$value}, \\FILEINFO_EXTENSION);
27+
if (!\\in_array(\$extension, \$validExtensions, true)) {
28+
return null;
29+
}
2530
})()
2631
PHP;
2732
}
2833

29-
private function evaluate(array $context, string $file): string
34+
private function evaluate(array $context, string $file): ?string
3035
{
31-
return pathinfo($file, \FILEINFO_EXTENSION);
36+
$validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg'];
37+
38+
$extension = pathinfo($file, \FILEINFO_EXTENSION);
39+
40+
if (!\in_array($extension, $validExtensions, true)) {
41+
return null;
42+
}
43+
44+
return $extension;
3245
}
3346
}

0 commit comments

Comments
 (0)