src/Twig/ContentCategoryExtension.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Service\Client\Content\CategoryService;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\Routing\RouterInterface;
  6. use Twig\Extension\AbstractExtension;
  7. use Twig\TwigFunction;
  8. class ContentCategoryExtension extends AbstractExtension
  9. {
  10.     protected RouterInterface $router;
  11.     protected CategoryService $categoryService;
  12.     protected RequestStack $requestStack;
  13.     public function __construct(CategoryService $categoryServiceRouterInterface $routerRequestStack $stack)
  14.     {
  15.         $this->router $router;
  16.         $this->categoryService $categoryService;
  17.         $this->requestStack $stack;
  18.     }
  19.     /**
  20.      * @return array
  21.      */
  22.     public function getFunctions(): array
  23.     {
  24.         return [
  25.             new TwigFunction('contentCategoryTranslation', [$this'getContentCategoryTranslation']),
  26.             new TwigFunction('contentCategoryUri', [$this'getContentCategoryUri']),
  27.             new TwigFunction('categoryGroupUri', [$this'getContentGroupUri']),
  28.             new TwigFunction('contentCategoryTree', [$this'getCategoryTree']),
  29.         ];
  30.     }
  31.     /**
  32.      * @return array
  33.      * @throws \Psr\Cache\InvalidArgumentException
  34.      * @throws \ReflectionException
  35.      */
  36.     public function getCategoryTree(): array
  37.     {
  38.         return $this->categoryService->getTree();
  39.     }
  40.     /**
  41.      * @param string $type
  42.      * @param string $slug
  43.      * @return string
  44.      */
  45.     public function getContentGroupUri(string $typestring $slug): string
  46.     {
  47.         if($type == 'video') {
  48.             return $this->router->generate('app_content_categoryvideo_group', [
  49.                 'slug' => $slug,
  50.             ]);
  51.         }
  52.         return $this->router->generate('app_content_categoryimageset_group', [
  53.             'slug' => $slug,
  54.         ]);
  55.     }
  56.     /**
  57.      * @param string $type
  58.      * @param string $group
  59.      * @param string $category
  60.      * @return string
  61.      * @throws \Psr\Cache\InvalidArgumentException
  62.      * @throws \ReflectionException
  63.      */
  64.     public function getContentCategoryUri(string $typestring $group$category): string
  65.     {
  66.         $route 'app_content_categoryvideo_category';
  67.         if($type == 'imageset') {
  68.             $route 'app_content_categoryimageset_category';
  69.         }
  70.         if(empty($group)) {
  71.             $group $this->categoryService->getCategoryGroupByCategoryId($category)['slug'];
  72.         }
  73.         if(is_int($category)) {
  74.             $specs $this->categoryService->getCategoryById($category);
  75.             $key $type == 'imageset' 'imageset_slug' 'video_slug';
  76.             if(isset($specs[$key])) {
  77.                 $category $specs[$key];
  78.             }
  79.         }
  80.         $params = [
  81.             'group' => $group,
  82.             'slug' => $category
  83.         ];
  84.         $orderByParam $this->requestStack->getMainRequest()->query->get('o'false);
  85.         if ($orderByParam) {
  86.             $params['o'] = $orderByParam;
  87.         }
  88.         return $this->router->generate($route$params);
  89.     }
  90.     /**
  91.      * @param int $category
  92.      * @return string
  93.      */
  94.     public function getContentCategoryTranslation(int $category): string
  95.     {
  96.         foreach($this->categoryService->getKeyValueStore() as $id => $specs) {
  97.             if($id == $category) {
  98.                 return $specs['name'];
  99.             }
  100.         }
  101.         return 'Kategorie #' $category;
  102.     }
  103. }