Skip to content

Commit 9e081e1

Browse files
committed
Added new functions (currentDateTime and formatNumber)
1 parent 10d9b7d commit 9e081e1

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/CurrentDateTime.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+
class CurrentDateTime 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(string $timezone = null)
21+
{
22+
return <<<PHP
23+
(new \DateTime('now', {$timezone} !== null ? new \DateTimeZone({$timezone}) : null))
24+
PHP;
25+
}
26+
27+
private function evaluate(array $context, string $timezone = null)
28+
{
29+
return new \DateTime('now', $timezone !== null ? new \DateTimeZone($timezone) : null);
30+
}
31+
}

src/StringExpressionLanguageProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ public function getFunctions(): array
1616
ExpressionFunction::fromPhp('strtolower', 'toLowerCase'),
1717
ExpressionFunction::fromPhp('substr', 'search'),
1818
ExpressionFunction::fromPhp('strtoupper', 'toUpperCase'),
19+
ExpressionFunction::fromPhp('number_format', 'formatNumber'),
1920
new FileName('fileName'),
2021
new DateTime('dateTime'),
2122
new FormatDate('formatDate'),
2223
new TruncateFileName('truncateFileName'),
24+
new CurrentDateTime('currentDateTime'),
2325
];
2426
}
2527
}

0 commit comments

Comments
 (0)