Skip to content

Commit

Permalink
Create a new expression to get file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Nov 8, 2024
1 parent cf8e2fb commit 26ff33a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/FileExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

final class FileExtension extends ExpressionFunction
{
public function __construct($name)
{
parent::__construct(
$name,
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile(string $value): string
{
return <<<PHP
(function () use (\$input) : string {
\$parts = explode('.', basename($value));
return end(\$parts);
})()
PHP;
}

private function evaluate(array $context, string $imageUrl): string
{
$parts = explode('.', basename($imageUrl));

return end($parts);
}
}
1 change: 1 addition & 0 deletions src/StringExpressionLanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getFunctions(): array
new AsInteger('asInteger'),
new AsString('asString'),
new Slugify('slugify'),
new FileExtension('fileExtension'),
];
}
}

0 comments on commit 26ff33a

Please sign in to comment.