File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments