Skip to content

Commit 8f13ce5

Browse files
authored
Merge pull request #1 from php-etl/feature/fileName-function
feature/fileName-function : add fileName function
2 parents 28ce550 + 8b7d8f0 commit 8f13ce5

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/FileName.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 FileName extends ExpressionFunction
10+
{
11+
public function __construct($name)
12+
{
13+
parent::__construct(
14+
$name,
15+
\Closure::fromCallable([$this, 'compile'])->bindTo($this),
16+
\Closure::fromCallable([$this, 'evaluate'])->bindTo($this)
17+
);
18+
}
19+
20+
private function compile($file)
21+
{
22+
return <<<COMPILED
23+
(!is_string(${file}) ? null : pathinfo({$file}, PATHINFO_FILENAME))
24+
COMPILED;
25+
}
26+
27+
private function evaluate(array $context, $file)
28+
{
29+
return !is_string($file) ? null : pathinfo($file, PATHINFO_FILENAME);
30+
}
31+
}

src/StringExpressionLanguageProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class StringExpressionLanguageProvider implements ExpressionFunctionProviderInte
1010
public function getFunctions(): array
1111
{
1212
return [
13-
ExpressionFunction::fromPhp('sprintf', 'format')
13+
ExpressionFunction::fromPhp('sprintf', 'format'),
14+
new FileName('fileName')
1415
];
1516
}
1617
}

0 commit comments

Comments
 (0)