From d5c38ce5784523b58e0360d978118c02f0acc2df Mon Sep 17 00:00:00 2001 From: Preciel Date: Thu, 5 Mar 2026 13:58:32 +0100 Subject: [PATCH] Add Symfony extensions --- php/symfony/src/Extension/PhpExtension.php | 50 ++++++++++++++ php/symfony/src/Extension/TwigExtension.php | 72 +++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 php/symfony/src/Extension/PhpExtension.php create mode 100644 php/symfony/src/Extension/TwigExtension.php diff --git a/php/symfony/src/Extension/PhpExtension.php b/php/symfony/src/Extension/PhpExtension.php new file mode 100644 index 0000000..993c1d9 --- /dev/null +++ b/php/symfony/src/Extension/PhpExtension.php @@ -0,0 +1,50 @@ + strlen($str) || substr_compare($str, $needle, 0, strlen($needle)) !== 0) { + return false; + } else { + return true; + } + } + + public function strEndWith(string $str, string $needle): bool { + if($str === '' || $needle === '' || strlen($needle) > strlen($str) || substr_compare($str, $needle, strlen($str)-strlen($needle), strlen($needle)) !== 0) { + return false; + } else { + return true; + } + } + public function strLike(string $str, string $needle, bool $ignoreCase = false): bool { + if($ignoreCase) { + return str_contains(strtolower($str), strtolower($needle)); + } else { + return str_contains($str, $needle); + } + } + + public function strPad(string $str, int $padLength, string $padString, string $direction = 'left'): string { + $strPad = null; + + if($direction == 'left') { + $strPad = STR_PAD_LEFT; + } elseif($direction == 'right') { + $strPad = STR_PAD_RIGHT; + } elseif($direction == 'both') { + $strPad = STR_PAD_BOTH; + } + + if($strPad !== null) { + return str_pad($str, $padLength, $padString, $strPad); + } else { + return $str; + } + } + + public function trim($input): array|string { + return !is_array($input) ? trim($input) : array_map([$this, 'trim'], $input); + } +} \ No newline at end of file diff --git a/php/symfony/src/Extension/TwigExtension.php b/php/symfony/src/Extension/TwigExtension.php new file mode 100644 index 0000000..08273fb --- /dev/null +++ b/php/symfony/src/Extension/TwigExtension.php @@ -0,0 +1,72 @@ +getUuidFunction(...)), + ]; + } + + public function getFilters(): array { + return [ + new TwigFilter(name: 'json_decode', callable: $this->jsonDecodeFilter(...)), + new TwigFilter(name: 'str_starts_with', callable: $this->strStartWithFilter(...)), + new TwigFilter(name: 'str_ends_with', callable: $this->strEndWithFilter(...)), + new TwigFilter(name: 'str_like', callable: $this->strLikeFilter(...)), + new TwigFilter(name: 'str_pad', callable: $this->strPadFilter(...)), + new TwigFilter(name: 'trim', callable: $this->trimFilter(...)), + new TwigFilter(name: 'shuffle_array', callable: $this->shuffleArrayFilter(...)), + ]; + } + + //ToDo Require symfony/uid + public function getUuidFunction(): string { + return Uuid::v7(); + } + + public function jsonDecodeFilter($json): mixed { + if(json_validate($json)) { + return json_decode($json, true); + } + + return false; + } + + public function strStartWithFilter(string $str, string $needle): bool { + return str_starts_with($str, $needle); + } + + public function strEndWithFilter(string $str, string $needle): bool { + return str_ends_with($str, $needle); + } + + public function strLikeFilter(string $str, string $needle, bool $ignoreCase = false): bool { + return $this->php->strLike($str, $needle, $ignoreCase); + } + + public function strPadFilter(string $str, int $padLength, string $padString, string $direction = 'left'): string { + return $this->php->strPad($str, $padLength, $padString, $direction); + } + + public function trimFilter($input): array|string { + return $this->php->trim($input); + } + + public function shuffleArrayFilter(array $array): array { + shuffle($array); + + return $array; + } +} \ No newline at end of file -- 2.43.0