custom/plugins/MoorlForms/src/Core/Framework/Twig/FbExtension.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlForms\Core\Framework\Twig;
  3. use MoorlForms\Core\Content\Element\ElementEntity;
  4. use MoorlForms\Core\Content\Form\FormEntity;
  5. use MoorlForms\Core\Service\FbService;
  6. use Shopware\Core\Framework\Adapter\Twig\TemplateFinder;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Twig\Extension\AbstractExtension;
  9. use Twig\TwigFunction;
  10. class FbExtension extends AbstractExtension
  11. {
  12.     /**
  13.      * @var string[]
  14.      */
  15.     private array $caseCache = [];
  16.     private TemplateFinder $finder;
  17.     private FbService $fbService;
  18.     public function __construct(
  19.         TemplateFinder $finder,
  20.         FbService $fbService
  21.     ) {
  22.         $this->finder $finder;
  23.         $this->fbService $fbService;
  24.     }
  25.     public function getTokenParsers(): array
  26.     {
  27.         return [
  28.             new FbTokenParser()
  29.         ];
  30.     }
  31.     public function getFunctions(): array
  32.     {
  33.         return [
  34.             new TwigFunction('fb_form', [$this'getForm'], ['needs_context' => true]),
  35.             new TwigFunction('fb_date', [$this'getDate']),
  36.             new TwigFunction('fb_stylesheet', [$this'getStyleSheet'])
  37.         ];
  38.     }
  39.     /**
  40.      * @param null|FormEntity|ElementEntity $element
  41.      * @return array
  42.      */
  43.     public function getStyleSheet($element null): array
  44.     {
  45.         $arr = [];
  46.         if (!$element) {
  47.             return $arr;
  48.         }
  49.         $config $element->getConfig();
  50.         if (isset($config['css']) && is_array($config['css'])) {
  51.             $config['css'] = array_filter($config['css'], 'strlen');
  52.             foreach ($config['css'] as $key => $value) {
  53.                 $arr[] = sprintf("%s:%s"$this->camelCaseToSnailCase($key), $value);
  54.             }
  55.             if ($element->getMedia()) {
  56.                 $arr[] = sprintf("background-image:url(%s);background-size:cover;background-position: center center"$element->getMedia()->getUrl());
  57.             }
  58.         }
  59.         return $arr;
  60.     }
  61.     public function getFinder(): TemplateFinder
  62.     {
  63.         return $this->finder;
  64.     }
  65.     public function getDate(?string $value null): string
  66.     {
  67.         if (empty($value)) {
  68.             return "";
  69.         }
  70.         $date = new \DateTimeImmutable($value);
  71.         return $date->format("Y-m-d");
  72.     }
  73.     public function getForm(array $twigContextstring $action): FormEntity
  74.     {
  75.         if (!$action) {
  76.             throw new \RuntimeException("Missing form technical name in Twig snippet");
  77.         }
  78.         return $this->fbService->initForm($action$this->getContext($twigContext));
  79.     }
  80.     private function getContext(array $context): SalesChannelContext
  81.     {
  82.         if (!isset($context['context'])) {
  83.             throw new \RuntimeException('Missing sales channel context object');
  84.         }
  85.         $context $context['context'];
  86.         if (!$context instanceof SalesChannelContext) {
  87.             throw new \RuntimeException('Missing sales channel context object');
  88.         }
  89.         return $context;
  90.     }
  91.     private function camelCaseToSnailCase(string $input): string
  92.     {
  93.         if (isset($this->caseCache[$input])) {
  94.             return $this->caseCache[$input];
  95.         }
  96.         $input str_replace('_''-'$input);
  97.         return $this->caseCache[$input] = ltrim(mb_strtolower(preg_replace('/[A-Z]/''-$0'$input)), '-');
  98.     }
  99. }