src/Twig/Extension/AppExtension.php line 38

  1. <?php
  2. namespace App\Twig\Extension;
  3. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  4. use Twig\Extension\AbstractExtension;
  5. use Twig\TwigFilter;
  6. use Twig\TwigFunction;
  7. class AppExtension extends AbstractExtension
  8. {
  9.     public function getFilters(): array
  10.     {
  11.         return [
  12.             // If your filter generates SAFE HTML, you should add a third
  13.             // parameter: ['is_safe' => ['html']]
  14.             // Reference: https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping
  15.             new TwigFilter('cart_product', [$this'getCartProduct']),
  16.             new TwigFilter('truncate', [$this'truncateFilter']),
  17.             new TwigFilter('services_by_socialnetwork', [$this'getServicesBySocialNetwork']),
  18.             new TwigFilter('services_by_category', [$this'getServicesByCategory']),
  19.             new TwigFilter('groupby', [$this'groupByFilter']),
  20.             new TwigFilter('customSort', [$this'customSort']),
  21.         ];
  22.     }
  23.     public function getFunctions()
  24.     {
  25.         return [
  26.             new TwigFunction('cart_product', [$this'cart_product']),
  27.         ];
  28.     }
  29.     public function getCartProduct($session)
  30.     {
  31.         // Votre logique pour récupérer les produits du panier ici
  32.         return $session->get('products', []);
  33.     }
  34.     public function truncateFilter(string $stringint $length 30string $suffix '...'): string
  35.     {
  36.         if (mb_strlen($string) <= $length) {
  37.             return $string;
  38.         }
  39.         return rtrim(mb_substr($string0$length)) . $suffix;
  40.     }
  41.     public function getServicesBySocialNetwork(array $services)
  42.     {
  43.         $servicesBySocialNetwork = [];
  44.         foreach ($services as $service) {
  45.             $socialNetwork $service->getSocialNetwork();
  46.             $position $service->getPosition();
  47.             if (!isset($socialNetwork) || empty(trim($socialNetwork))) {
  48.                 continue;
  49.             }
  50.             if ($service->isActif() !== true) {
  51.                 continue;
  52.             }
  53.             if (!array_key_exists($socialNetwork$servicesBySocialNetwork)) {
  54.                 $servicesBySocialNetwork[$socialNetwork] = [];
  55.             }
  56.         }
  57.         foreach ($servicesBySocialNetwork as &$services) { // Ajoutez une référence avec "&" pour modifier directement le tableau d'origine
  58.             usort($services, function ($a$b) {
  59.                 return $a->getPosition() - $b->getPosition();
  60.             });
  61.         }
  62.         return $servicesBySocialNetwork;
  63.     }
  64.     public function getServicesByCategory(array $socialNetwork)
  65.     {
  66.         $servicesByCategory = [];
  67.         foreach ($socialNetwork as $service) {
  68.             $category$service->getCategory();
  69.             if (!isset($category) || empty(trim($category)) ) {
  70.                 continue;
  71.             }
  72.             if ($service->isActif() !== true) {
  73.                 continue;
  74.             }
  75.             if (!array_key_exists($category$servicesByCategory)) {
  76.                 $servicesByCategory[$category] = [];
  77.             }
  78.             $servicesByCategory[$category][] = $service;
  79.         }
  80.         return $servicesByCategory;
  81.     }
  82.     public function getServicesByProduct(array $socialNetwork)
  83.     {
  84.         $servicesByProduct = [];
  85.         foreach ($socialNetwork as $service) {
  86.             $category$service->get();
  87.             if (!isset($category) || empty(trim($category)) ) {
  88.                 continue;
  89.             }
  90.             if ($service->isActif() !== true) {
  91.                 continue;
  92.             }
  93.             if (!array_key_exists($category$servicesByCategory)) {
  94.                 $servicesByCategory[$category] = [];
  95.             }
  96.             $servicesByCategory[$category][] = $service;
  97.         }
  98.         return $servicesByCategory;
  99.     }
  100.     public function groupByFilter($array$groupByField)
  101.     {
  102.         $result = [];
  103.         foreach ($array as $item) {
  104.             $group $item[$groupByField];
  105.             if (!array_key_exists($group$result)) {
  106.                 $result[$group] = [];
  107.             }
  108.             $result[$group][] = $item;
  109.         }
  110.         return $result;
  111.     }
  112.     public function customSort($subscriptions)
  113.     {
  114.         // Implement your custom sorting logic here and return the sorted array of subscriptions
  115.         // Example: Sorting by the "createdAt" property in descending order
  116.         usort($subscriptions, function ($a$b) {
  117.             return $b->getCreateAt() <=> $a->getCreateAt();
  118.         });
  119.         return $subscriptions;
  120.     }
  121. }