<?php
namespace App\Twig\Extension;
use App\Entity\System\School;
use App\Service\Content\WebUrlGenerator;
use App\Util\Querying;
use Cms\ContainerBundle\Entity\Container;
use Cms\FrontendBundle\Service\ResolverManager;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
*
*/
final class MiscellaneousExtension extends AbstractExtension
{
// DI
protected TranslatorInterface $translator;
protected ResolverManager $rm;
protected WebUrlGenerator $wug;
/**
* @param TranslatorInterface $translator
* @param ResolverManager $rm
* @param WebUrlGenerator $wug
*/
public function __construct(
TranslatorInterface $translator,
ResolverManager $rm,
WebUrlGenerator $wug
)
{
$this->translator = $translator;
$this->rm = $rm;
$this->wug = $wug;
}
/**
* {@inheritDoc}
*/
public function getFunctions(): array
{
return [
new TwigFunction(
'dir_flip',
[$this, 'dir_flip']
),
new TwigFunction(
'trans_check',
[$this, 'trans_check']
),
new TwigFunction(
'web_url_view',
[$this->wug, 'view']
),
new TwigFunction(
'web_url_landing',
[$this->wug, 'landing']
),
new TwigFunction(
'web_url_subscribe',
[$this->wug, 'subscribe']
),
new TwigFunction(
'web_url_catchall',
[$this->wug, 'catchall']
),
new TwigFunction(
'web_url_request',
[$this->wug, 'request']
),
];
}
/**
* {@inheritDoc}
*/
public function getFilters(): array
{
return [
new TwigFilter(
'decbin',
'decbin'
),
new TwigFilter(
'dir_flip',
[$this, 'dir_flip']
),
new TwigFilter(
'pad',
[$this, 'pad']
),
new TwigFilter(
'lpad',
[$this, 'lpad']
),
new TwigFilter(
'rpad',
[$this, 'rpad']
),
new TwigFilter(
'schoolify',
[$this, 'schoolify']
),
new TwigFilter(
'percentage',
[$this, 'percentage']
),
new TwigFilter(
'locale_get_display_language',
'locale_get_display_language'
)
];
}
/**
* @param numeric $value
* @return string
*/
public function percentage($value): string
{
return round($value * 100) . '%';
}
/**
* @param Container|School|null $thing
* @return School|null
*/
public function schoolify($thing): ?School
{
switch (true) {
case $thing instanceof School:
return $thing;
case $thing instanceof Container:
return $this->rm->getSchoolResolver()->resolveSchoolByDepartment($thing);
case $thing === null:
return null;
}
throw new \Exception();
}
/**
* @param string $dir
* @return string
*/
public function dir_flip(string $dir): string
{
return Querying::dirFlip($dir);
}
/**
* @param string $key
* @param string|null $domain
* @param string|null $locale
* @return string|null
*/
public function trans_check(string $key, ?string $domain = null, ?string $locale = null): ?string
{
$catalogue = $this->translator->getCatalogue($locale ?: $this->translator->getLocale());
return ($catalogue->defines($key, $domain ?: 'messages')) ? $key : null;
}
/**
* @param string $text
* @param int $length
* @param string $char
* @return string
*/
public function pad(string $text, int $length, string $char): string
{
return str_pad($text, $length, $char, STR_PAD_BOTH);
}
/**
* @param string $text
* @param int $length
* @param string $char
* @return string
*/
public function lpad(string $text, int $length, string $char): string
{
return str_pad($text, $length, $char, STR_PAD_LEFT);
}
/**
* @param string $text
* @param int $length
* @param string $char
* @return string
*/
public function rpad(string $text, int $length, string $char): string
{
return str_pad($text, $length, $char, STR_PAD_RIGHT);
}
}