Skip to content

Commit

Permalink
Added a convertCharCode() function
Browse files Browse the repository at this point in the history
  • Loading branch information
gplanchat authored Apr 25, 2023
1 parent c3e3631 commit 76702b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/ConvertCharCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

class ConvertCharCode extends ExpressionFunction
{
public function __construct($name)
{
parent::__construct(
$name,
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile(string $text, string $sourceCharCode, string $destinationCharCode): string
{
return <<<"PHP"
iconv($sourceCharCode, $destinationCharCode, $text)
PHP;
}

private function evaluate(array $context, string $text, string $sourceCharCode, string $destinationCharCode)
{
return iconv($sourceCharCode, $destinationCharCode, $text);
}
}
1 change: 1 addition & 0 deletions src/StringExpressionLanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function getFunctions(): array
new FormatDate('formatDate'),
new TruncateFileName('truncateFileName'),
new Now('now'),
new ConvertCharCode('convertCharCode')
];
}
}

0 comments on commit 76702b8

Please sign in to comment.