<?php
namespace App\Http\Twig;
use App\Entity\Activities\Feedback;
use App\Entity\Auth\User;
use App\Repository\Activities\BookingRepository;
use App\Repository\Activities\FeedbackRepository;
use App\Repository\Auth\UserRepository;
use DateTime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class TwigExtension extends AbstractExtension
{
private FeedbackRepository $feedbackRepository;
private UserRepository $userRepository;
private BookingRepository $bookingRepository;
/**
* TwigExtension constructor.
* @param FeedbackRepository $feedbackRepository
* @param UserRepository $userRepository
* @param BookingRepository $bookingRepository
*/
public function __construct(FeedbackRepository $feedbackRepository,
UserRepository $userRepository,
BookingRepository $bookingRepository)
{
$this->feedbackRepository = $feedbackRepository;
$this->userRepository = $userRepository;
$this->bookingRepository = $bookingRepository;
}
public function getFunctions(): array
{
return [
new TwigFunction('icon', [$this, 'svgIcon'], ['is_safe' => ['html']]),
new TwigFunction('evaluation', [$this, 'profileEvaluation'], ['is_safe' => ['html']])
];
}
/**
* @return array<TwigFilter>
*/
public function getFilters(): array
{
return [
new TwigFilter('age', [$this, 'calculateAge']),
new TwigFilter('time_passed', [$this, 'formatDates']),
new TwigFilter('favorites', [$this, 'favoritesCounter']),
new TwigFilter('keeping', [$this, 'keepsCounter'])
];
}
/**
* Génère le code HTML pour une icone SVG.
* @param string $name
* @param int|null $size
* @return string
*/
public function svgIcon(string $name, ?int $size = null): string
{
$attrs = '';
if ($size) {
$attrs = " width=\"{$size}px\" height=\"{$size}px\"";
}
return <<<HTML
<svg class="icon icon-{$name}" {$attrs}>
<use xlink:href="/sprite.svg?logo#{$name}"></use>
</svg>
HTML;
}
/**
* @param DateTime|null $date
* @return int
*/
public function calculateAge(DateTime $date = null): int
{
if ($date) {
$birthDate = $date->format('d-m-Y');
$currentDate = date("d-m-Y");
return (int)(date_diff(date_create($birthDate), date_create($currentDate)))->format("%y");
}
return 0;
}
/**
* @param DateTime|null $date
* @return string
*/
public function formatDates(DateTime $date = null): ?string
{
if ($date == null) {
return null;
}
$currentDate = new DateTime();
$interval = $date->diff($currentDate);
if (intval($interval->y) !== 0) {
$result = "Il y'a " . $interval->y . " an";
$result .= $interval->y >= 1 ? 's' : '';
return $result;
}
if (intval($interval->m) !== 0) {
$result = "Il y'a " . $interval->m . " mois";
return $result;
}
if (intval($interval->d) !== 0) {
$result = "Il y'a " . $interval->d . " jour";
$result .= $interval->d > 1 ? 's' : '';
return $result;
}
if (intval($interval->h) !== 0) {
$result = "Il y'a " . $interval->h . " heure";
$result .= $interval->h > 1 ? 's' : '';
return $result;
}
if (intval($interval->i) !== 0) {
$result = "Il y'a " . $interval->i . " minute";
$result .= $interval->i > 1 ? 's' : '';
return $result;
}
if (intval($interval->s) !== 0) {
$result = "Il y'a " . $interval->s . " seconde";
$result .= $interval->s > 1 ? 's' : '';
return $result;
}
return '';
}
/**
* Génère le code HTML pour les évaluations d'un profil.
* @param User|null $user
* @param Feedback|null $feedback
* @return string
*/
public function profileEvaluation(User $user = null, Feedback $feedback = null): ?string
{
if ($user) {
$average = $this->feedbackRepository->calculateFeedbackMarks($user);
} else {
$average = $feedback->getMarks();
}
$classes = [
$average >= 1 ? 'star c_star' : 'star-o',
$average >= 2 ? 'star c_star' : 'star-o',
$average >= 3 ? 'star c_star' : 'star-o',
$average >= 4 ? 'star c_star' : 'star-o',
$average >= 5 ? 'star c_star' : 'star-o'
];
$display = $user ? '' : 'd-none';
return <<<HTML
<i class="fa fa-{$classes[0]}"></i>
<i class="fa fa-{$classes[1]}"></i>
<i class="fa fa-{$classes[2]}"></i>
<i class="fa fa-{$classes[3]}"></i>
<i class="fa fa-{$classes[4]}"></i>
<span class="list-inline-item badge badge-primary text-white ${display}">{$average}</span>
HTML;
}
/**
* @param User $user
* @return int
*/
public function favoritesCounter(User $user): int
{
if (!$user->isIsParent()) {
return 0;
}
return count($this->userRepository->getFavorites($user));
}
/**
* @param User $user
* @return int
*/
public function keepsCounter(User $user): int
{
if ($user->isIsParent()) {
return count($this->bookingRepository->findBy(['parent' => $user]));
} else {
return count($this->bookingRepository->findBy(['nanny' => $user]));
}
}
}