You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.5 KiB
98 lines
2.5 KiB
4 weeks ago
|
<?php
|
||
|
|
||
|
namespace App\TwigExtensions;
|
||
|
|
||
|
use App\Configuration;
|
||
|
use DateTime;
|
||
|
use DateTimeZone;
|
||
|
use Twig\Extension\AbstractExtension;
|
||
|
use Twig\TwigFilter;
|
||
|
|
||
|
require_once dirname($_SERVER["DOCUMENT_ROOT"]) . "/vendor/autoload.php";
|
||
|
|
||
|
class FiltersExtension extends AbstractExtension
|
||
|
{
|
||
|
public function getFilters()
|
||
|
{
|
||
|
return [
|
||
|
new TwigFilter('translate', [$this, 'Translate']),
|
||
|
new TwigFilter('base64_encode', [$this, 'Base64Encode']),
|
||
|
new TwigFilter('base64_decode', [$this, 'Base64Decode']),
|
||
|
new TwigFilter('get_type', [$this, 'GetType']),
|
||
|
new TwigFilter('pretty_date', [$this, 'PrettyDate']),
|
||
|
new TwigFilter('pretty_time', [$this, 'PrettyTime']),
|
||
|
new TwigFilter('pretty_date_time', [$this, 'PrettyDateTime']),
|
||
|
|
||
|
new TwigFilter('json_encode', [$this, 'JSONEncode']),
|
||
|
new TwigFilter('json_decode', [$this, 'JSONDecode']),
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public function JSONEncode(mixed $target): bool|string
|
||
|
{
|
||
|
return json_encode(value: $target);
|
||
|
}
|
||
|
public function JSONDecode(string $target): ?array
|
||
|
{
|
||
|
return json_decode($target, true);
|
||
|
}
|
||
|
|
||
|
public function Translate(?string $target): string
|
||
|
{
|
||
|
if($target == null) return "!!$target!!";
|
||
|
|
||
|
if(key_exists(key: $target, array: Configuration::GetDictionary()))
|
||
|
return Configuration::GetDictionary()[$target];
|
||
|
/*
|
||
|
$temp = Configuration::GetDictionary()[$target];
|
||
|
if($temp == null) return "!!$target!!";
|
||
|
return $temp;
|
||
|
*/
|
||
|
return "!!$target!!";
|
||
|
}
|
||
|
|
||
|
|
||
|
public function Base64Encode($target) : string
|
||
|
{
|
||
|
return base64_encode($target) . "==";
|
||
|
}
|
||
|
|
||
|
public function Base64Decode($target)
|
||
|
{
|
||
|
return base64_decode($target);
|
||
|
}
|
||
|
|
||
|
|
||
|
public function GetType($target): string
|
||
|
{
|
||
|
return gettype($target);
|
||
|
}
|
||
|
|
||
|
|
||
|
public function PrettyDate(?string $target): string
|
||
|
{
|
||
|
if($target == "" || $target == null) return "meow :3";
|
||
|
|
||
|
$temp = new DateTime(
|
||
|
datetime: $target,
|
||
|
);
|
||
|
|
||
|
return $temp->format("l d F Y");
|
||
|
}
|
||
|
public function PrettyTime(?string $target): string
|
||
|
{
|
||
|
if($target == [] || $target == "" || $target == null) return "meow :3";
|
||
|
|
||
|
$temp = new DateTime(
|
||
|
datetime: $target,
|
||
|
);
|
||
|
|
||
|
return $temp->format("h:i:s A");
|
||
|
}
|
||
|
|
||
|
public function PrettyDateTime(?string $target): string
|
||
|
{
|
||
|
return $this->PrettyDate(target: $target) . " at " . $this->PrettyTime(target: $target);
|
||
|
}
|
||
|
}
|