Skip to content

Commit 4dc5f2a

Browse files
committed
Added the String casting functions
1 parent 76702b8 commit 4dc5f2a

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/AsFloat.php

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

src/AsInteger.php

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

src/DateTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct($name)
1717
);
1818
}
1919

20-
private function compile(string $date, string $format, string $timezone = null): string
20+
private function compile(string $date, string $format, string $timezone = 'null'): string
2121
{
2222
return <<<"PHP"
2323
\\DateTimeImmutable::createFromFormat({$format}, {$date}, {$timezone} !== null ? new \\DateTimeZone({$timezone}) : null)

src/StringExpressionLanguageProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public function getFunctions(): array
3232
new FormatDate('formatDate'),
3333
new TruncateFileName('truncateFileName'),
3434
new Now('now'),
35-
new ConvertCharCode('convertCharCode')
35+
new ConvertCharCode('convertCharCode'),
36+
new AsFloat('asFloat'),
37+
new AsInteger('asInteger'),
3638
];
3739
}
3840
}

0 commit comments

Comments
 (0)