src/Http/Twig/TwigExtension.php line 166

Open in your IDE?
  1. <?php
  2. namespace App\Http\Twig;
  3. use App\Entity\Activities\Feedback;
  4. use App\Entity\Auth\User;
  5. use App\Repository\Activities\BookingRepository;
  6. use App\Repository\Activities\FeedbackRepository;
  7. use App\Repository\Auth\UserRepository;
  8. use DateTime;
  9. use Twig\Extension\AbstractExtension;
  10. use Twig\TwigFilter;
  11. use Twig\TwigFunction;
  12. class TwigExtension extends AbstractExtension
  13. {
  14.     private FeedbackRepository $feedbackRepository;
  15.     private UserRepository $userRepository;
  16.     private BookingRepository $bookingRepository;
  17.     /**
  18.      * TwigExtension constructor.
  19.      * @param FeedbackRepository $feedbackRepository
  20.      * @param UserRepository $userRepository
  21.      * @param BookingRepository $bookingRepository
  22.      */
  23.     public function __construct(FeedbackRepository $feedbackRepository,
  24.                                 UserRepository $userRepository,
  25.                                 BookingRepository $bookingRepository)
  26.     {
  27.         $this->feedbackRepository $feedbackRepository;
  28.         $this->userRepository $userRepository;
  29.         $this->bookingRepository $bookingRepository;
  30.     }
  31.     public function getFunctions(): array
  32.     {
  33.         return [
  34.             new TwigFunction('icon', [$this'svgIcon'], ['is_safe' => ['html']]),
  35.             new TwigFunction('evaluation', [$this'profileEvaluation'], ['is_safe' => ['html']])
  36.         ];
  37.     }
  38.     /**
  39.      * @return array<TwigFilter>
  40.      */
  41.     public function getFilters(): array
  42.     {
  43.         return [
  44.             new TwigFilter('age', [$this'calculateAge']),
  45.             new TwigFilter('time_passed', [$this'formatDates']),
  46.             new TwigFilter('favorites', [$this'favoritesCounter']),
  47.             new TwigFilter('keeping', [$this'keepsCounter'])
  48.         ];
  49.     }
  50.     /**
  51.      * Génère le code HTML pour une icone SVG.
  52.      * @param string $name
  53.      * @param int|null $size
  54.      * @return string
  55.      */
  56.     public function svgIcon(string $name, ?int $size null): string
  57.     {
  58.         $attrs '';
  59.         if ($size) {
  60.             $attrs " width=\"{$size}px\" height=\"{$size}px\"";
  61.         }
  62.         return <<<HTML
  63.         <svg class="icon icon-{$name}{$attrs}>
  64.           <use xlink:href="/sprite.svg?logo#{$name}"></use>
  65.         </svg>
  66.         HTML;
  67.     }
  68.     /**
  69.      * @param DateTime|null $date
  70.      * @return int
  71.      */
  72.     public function calculateAge(DateTime $date null): int
  73.     {
  74.         if ($date) {
  75.             $birthDate $date->format('d-m-Y');
  76.             $currentDate date("d-m-Y");
  77.             return (int)(date_diff(date_create($birthDate), date_create($currentDate)))->format("%y");
  78.         }
  79.         return 0;
  80.     }
  81.     /**
  82.      * @param DateTime|null $date
  83.      * @return string
  84.      */
  85.     public function formatDates(DateTime $date null): ?string
  86.     {
  87.         if ($date == null) {
  88.             return null;
  89.         }
  90.         $currentDate = new DateTime();
  91.         $interval $date->diff($currentDate);
  92.         if (intval($interval->y) !== 0) {
  93.             $result "Il y'a " $interval->" an";
  94.             $result .= $interval->>= 's' '';
  95.             return $result;
  96.         }
  97.         if (intval($interval->m) !== 0) {
  98.             $result "Il y'a " $interval->" mois";
  99.             return $result;
  100.         }
  101.         if (intval($interval->d) !== 0) {
  102.             $result "Il y'a " $interval->" jour";
  103.             $result .= $interval->'s' '';
  104.             return $result;
  105.         }
  106.         if (intval($interval->h) !== 0) {
  107.             $result "Il y'a " $interval->" heure";
  108.             $result .= $interval->'s' '';
  109.             return $result;
  110.         }
  111.         if (intval($interval->i) !== 0) {
  112.             $result "Il y'a " $interval->" minute";
  113.             $result .= $interval->'s' '';
  114.             return $result;
  115.         }
  116.         if (intval($interval->s) !== 0) {
  117.             $result "Il y'a " $interval->" seconde";
  118.             $result .= $interval->'s' '';
  119.             return $result;
  120.         }
  121.         return '';
  122.     }
  123.     /**
  124.      * Génère le code HTML pour les évaluations d'un profil.
  125.      * @param User|null $user
  126.      * @param Feedback|null $feedback
  127.      * @return string
  128.      */
  129.     public function profileEvaluation(User $user nullFeedback $feedback null): ?string
  130.     {
  131.         if ($user) {
  132.             $average $this->feedbackRepository->calculateFeedbackMarks($user);
  133.         } else {
  134.             $average $feedback->getMarks();
  135.         }
  136.         $classes = [
  137.             $average >= 'star c_star' 'star-o',
  138.             $average >= 'star c_star' 'star-o',
  139.             $average >= 'star c_star' 'star-o',
  140.             $average >= 'star c_star' 'star-o',
  141.             $average >= 'star c_star' 'star-o'
  142.         ];
  143.         $display $user '' 'd-none';
  144.         return <<<HTML
  145.         <i class="fa fa-{$classes[0]}"></i>
  146.         <i class="fa fa-{$classes[1]}"></i>
  147.         <i class="fa fa-{$classes[2]}"></i>
  148.         <i class="fa fa-{$classes[3]}"></i>
  149.         <i class="fa fa-{$classes[4]}"></i>
  150.         <span class="list-inline-item badge badge-primary text-white ${display}">{$average}</span>
  151.         HTML;
  152.     }
  153.     /**
  154.      * @param User $user
  155.      * @return int
  156.      */
  157.     public function favoritesCounter(User $user): int
  158.     {
  159.         if (!$user->isIsParent()) {
  160.             return 0;
  161.         }
  162.         return count($this->userRepository->getFavorites($user));
  163.     }
  164.     /**
  165.      * @param User $user
  166.      * @return int
  167.      */
  168.     public function keepsCounter(User $user): int
  169.     {
  170.         if ($user->isIsParent()) {
  171.             return count($this->bookingRepository->findBy(['parent' => $user]));
  172.         } else {
  173.             return count($this->bookingRepository->findBy(['nanny' => $user]));
  174.         }
  175.     }
  176. }