Skip to content

Commit

Permalink
Improve the fileExtension expression
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Nov 8, 2024
1 parent c5a830b commit 8870077
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/FileExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,29 @@ public function __construct($name)
private function compile(string $value): string
{
return <<<PHP
(function () use (\$input) : string {
return \\pathinfo($value, \FILEINFO_EXTENSION)
(function () use (\$input) : ?string {
\$validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'pdf', 'mp4', 'webm', 'mp3'];
\$extension = \\pathinfo({$value}, \\FILEINFO_EXTENSION);
if (!\\in_array(\$extension, \$validExtensions, true)) {
return null;
}
return \$extension;
})()
PHP;
}

private function evaluate(array $context, string $file): string
private function evaluate(array $context, string $file): ?string
{
return pathinfo($file, \FILEINFO_EXTENSION);
$validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg'];

$extension = pathinfo($file, \FILEINFO_EXTENSION);

if (!\in_array($extension, $validExtensions, true)) {
return null;
}

return $extension;
}
}

0 comments on commit 8870077

Please sign in to comment.