src/App/Twig/Extension/MiscellaneousExtension.php line 132

Open in your IDE?
  1. <?php
  2. namespace App\Twig\Extension;
  3. use App\Entity\System\School;
  4. use App\Service\Content\WebUrlGenerator;
  5. use App\Util\Querying;
  6. use Cms\ContainerBundle\Entity\Container;
  7. use Cms\FrontendBundle\Service\ResolverManager;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. use Twig\Extension\AbstractExtension;
  10. use Twig\TwigFilter;
  11. use Twig\TwigFunction;
  12. /**
  13.  *
  14.  */
  15. final class MiscellaneousExtension extends AbstractExtension
  16. {
  17.     // DI
  18.     protected TranslatorInterface $translator;
  19.     protected ResolverManager $rm;
  20.     protected WebUrlGenerator $wug;
  21.     /**
  22.      * @param TranslatorInterface $translator
  23.      * @param ResolverManager $rm
  24.      * @param WebUrlGenerator $wug
  25.      */
  26.     public function __construct(
  27.         TranslatorInterface $translator,
  28.         ResolverManager $rm,
  29.         WebUrlGenerator $wug
  30.     )
  31.     {
  32.         $this->translator $translator;
  33.         $this->rm $rm;
  34.         $this->wug $wug;
  35.     }
  36.     /**
  37.      * {@inheritDoc}
  38.      */
  39.     public function getFunctions(): array
  40.     {
  41.         return [
  42.             new TwigFunction(
  43.                 'dir_flip',
  44.                 [$this'dir_flip']
  45.             ),
  46.             new TwigFunction(
  47.                 'trans_check',
  48.                 [$this'trans_check']
  49.             ),
  50.             new TwigFunction(
  51.                 'web_url_view',
  52.                 [$this->wug'view']
  53.             ),
  54.             new TwigFunction(
  55.                 'web_url_landing',
  56.                 [$this->wug'landing']
  57.             ),
  58.             new TwigFunction(
  59.                 'web_url_subscribe',
  60.                 [$this->wug'subscribe']
  61.             ),
  62.             new TwigFunction(
  63.                 'web_url_catchall',
  64.                 [$this->wug'catchall']
  65.             ),
  66.             new TwigFunction(
  67.                 'web_url_request',
  68.                 [$this->wug'request']
  69.             ),
  70.         ];
  71.     }
  72.     /**
  73.      * {@inheritDoc}
  74.      */
  75.     public function getFilters(): array
  76.     {
  77.         return [
  78.             new TwigFilter(
  79.                 'decbin',
  80.                 'decbin'
  81.             ),
  82.             new TwigFilter(
  83.                 'dir_flip',
  84.                 [$this'dir_flip']
  85.             ),
  86.             new TwigFilter(
  87.                 'pad',
  88.                 [$this'pad']
  89.             ),
  90.             new TwigFilter(
  91.                 'lpad',
  92.                 [$this'lpad']
  93.             ),
  94.             new TwigFilter(
  95.                 'rpad',
  96.                 [$this'rpad']
  97.             ),
  98.             new TwigFilter(
  99.                 'schoolify',
  100.                 [$this'schoolify']
  101.             ),
  102.             new TwigFilter(
  103.                 'percentage',
  104.                 [$this'percentage']
  105.             ),
  106.             new TwigFilter(
  107.                 'locale_get_display_language',
  108.                 'locale_get_display_language'
  109.             )
  110.         ];
  111.     }
  112.     /**
  113.      * @param numeric $value
  114.      * @return string
  115.      */
  116.     public function percentage($value): string
  117.     {
  118.         return round($value 100) . '%';
  119.     }
  120.     /**
  121.      * @param Container|School|null $thing
  122.      * @return School|null
  123.      */
  124.     public function schoolify($thing): ?School
  125.     {
  126.         switch (true) {
  127.             case $thing instanceof School:
  128.                 return $thing;
  129.             case $thing instanceof Container:
  130.                 return $this->rm->getSchoolResolver()->resolveSchoolByDepartment($thing);
  131.             case $thing === null:
  132.                 return null;
  133.         }
  134.         throw new \Exception();
  135.     }
  136.     /**
  137.      * @param string $dir
  138.      * @return string
  139.      */
  140.     public function dir_flip(string $dir): string
  141.     {
  142.         return Querying::dirFlip($dir);
  143.     }
  144.     /**
  145.      * @param string $key
  146.      * @param string|null $domain
  147.      * @param string|null $locale
  148.      * @return string|null
  149.      */
  150.     public function trans_check(string $key, ?string $domain null, ?string $locale null): ?string
  151.     {
  152.         $catalogue $this->translator->getCatalogue($locale ?: $this->translator->getLocale());
  153.         return ($catalogue->defines($key$domain ?: 'messages')) ? $key null;
  154.     }
  155.     /**
  156.      * @param string $text
  157.      * @param int $length
  158.      * @param string $char
  159.      * @return string
  160.      */
  161.     public function pad(string $textint $lengthstring $char): string
  162.     {
  163.         return str_pad($text$length$charSTR_PAD_BOTH);
  164.     }
  165.     /**
  166.      * @param string $text
  167.      * @param int $length
  168.      * @param string $char
  169.      * @return string
  170.      */
  171.     public function lpad(string $textint $lengthstring $char): string
  172.     {
  173.         return str_pad($text$length$charSTR_PAD_LEFT);
  174.     }
  175.     /**
  176.      * @param string $text
  177.      * @param int $length
  178.      * @param string $char
  179.      * @return string
  180.      */
  181.     public function rpad(string $textint $lengthstring $char): string
  182.     {
  183.         return str_pad($text$length$charSTR_PAD_RIGHT);
  184.     }
  185. }