Skip to content

Commit 0d62637

Browse files
committed
Improve the fileExtension expression
1 parent c5a830b commit 0d62637

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/FileExtension.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,29 @@ public function __construct($name)
2020
private function compile(string $value): string
2121
{
2222
return <<<PHP
23-
(function () use (\$input) : string {
24-
return \\pathinfo($value, \FILEINFO_EXTENSION)
23+
(function () use (\$input) : ?string {
24+
\$validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'pdf', 'mp4', 'webm', 'mp3'];
25+
26+
\$extension = \\pathinfo({$value})['extension'] ?? null;
27+
if (!\\in_array(\$extension, \$validExtensions, true)) {
28+
return null;
29+
}
30+
31+
return \$extension;
2532
})()
2633
PHP;
2734
}
2835

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

0 commit comments

Comments
 (0)