Skip to content

Commit 76702b8

Browse files
authored
Added a convertCharCode() function
1 parent c3e3631 commit 76702b8

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/ConvertCharCode.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 ConvertCharCode 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 $text, string $sourceCharCode, string $destinationCharCode): string
21+
{
22+
return <<<"PHP"
23+
iconv($sourceCharCode, $destinationCharCode, $text)
24+
PHP;
25+
}
26+
27+
private function evaluate(array $context, string $text, string $sourceCharCode, string $destinationCharCode)
28+
{
29+
return iconv($sourceCharCode, $destinationCharCode, $text);
30+
}
31+
}

src/StringExpressionLanguageProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function getFunctions(): array
3232
new FormatDate('formatDate'),
3333
new TruncateFileName('truncateFileName'),
3434
new Now('now'),
35+
new ConvertCharCode('convertCharCode')
3536
];
3637
}
3738
}

0 commit comments

Comments
 (0)