Skip to content

Commit 5fa1755

Browse files
committed
Created a new class instead of using the fromPhp call
1 parent b815acb commit 5fa1755

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/AsString.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 AsString 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+
((string) {$value})
24+
PHP;
25+
}
26+
27+
private function evaluate(array $context, int|float|string $value): string
28+
{
29+
return (string) $value;
30+
}
31+
}

src/StringExpressionLanguageProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public function getFunctions(): array
2727
ExpressionFunction::fromPhp('ucwords', 'capitalizeWords'),
2828
ExpressionFunction::fromPhp('rtrim', 'removeWhitespaces'),
2929
ExpressionFunction::fromPhp('explode', 'splitIntoArray'),
30-
ExpressionFunction::fromPhp('strval', 'transformToString'),
31-
ExpressionFunction::fromPhp('strrchr', 'findLast'),
3230
new FileName('fileName'),
3331
new DateTime('dateTime'),
3432
new FormatDate('formatDate'),
@@ -37,6 +35,7 @@ public function getFunctions(): array
3735
new ConvertCharCode('convertCharCode'),
3836
new AsFloat('asFloat'),
3937
new AsInteger('asInteger'),
38+
new AsString('asString'),
4039
];
4140
}
4241
}

0 commit comments

Comments
 (0)