-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a new expression to get file extension
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters