Add Symfony extensions #4
50
php/symfony/src/Extension/PhpExtension.php
Normal file
50
php/symfony/src/Extension/PhpExtension.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\src\Extension;
|
||||
|
||||
class PhpExtension {
|
||||
public function strStartWith(string $str, string $needle): bool {
|
||||
if($str === '' || $needle === '' || strlen($needle) > 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);
|
||||
}
|
||||
}
|
||||
72
php/symfony/src/Extension/TwigExtension.php
Normal file
72
php/symfony/src/Extension/TwigExtension.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\src\Extension;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class TwigExtension extends AbstractExtension {
|
||||
public function __construct(
|
||||
private readonly PhpExtension $php,
|
||||
) {}
|
||||
|
||||
public function getFunctions(): array {
|
||||
return [
|
||||
new TwigFunction(name: 'get_uuid', callable: $this->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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user