Skip to content

Commit 26ff33a

Browse files
committed
Create a new expression to get file extension
1 parent cf8e2fb commit 26ff33a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/FileExtension.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\StringExpressionLanguage;
6+
7+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
8+
9+
final class FileExtension extends ExpressionFunction
10+
{
11+
public function __construct($name)
12+
{
13+
parent::__construct(
14+
$name,
15+
$this->compile(...)->bindTo($this),
16+
$this->evaluate(...)->bindTo($this)
17+
);
18+
}
19+
20+
private function compile(string $value): string
21+
{
22+
return <<<PHP
23+
(function () use (\$input) : string {
24+
\$parts = explode('.', basename($value));
25+
26+
return end(\$parts);
27+
})()
28+
PHP;
29+
}
30+
31+
private function evaluate(array $context, string $imageUrl): string
32+
{
33+
$parts = explode('.', basename($imageUrl));
34+
35+
return end($parts);
36+
}
37+
}

src/StringExpressionLanguageProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getFunctions(): array
3838
new AsInteger('asInteger'),
3939
new AsString('asString'),
4040
new Slugify('slugify'),
41+
new FileExtension('fileExtension'),
4142
];
4243
}
4344
}

0 commit comments

Comments
 (0)