src/Misc/TwigExtension.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Misc;
  3. use App\Constant\Common;
  4. use Twig\TwigTest;
  5. use Twig\TwigFilter;
  6. use Twig\TwigFunction;
  7. use App\Service\CampusService;
  8. use App\Service\ProgramService;
  9. use Twig\Extension\AbstractExtension;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. class TwigExtension extends AbstractExtension
  14. {
  15.     public function __construct(
  16.         TranslatorInterface $translator,
  17.         ProgramService $programService,
  18.         CampusService $campusService,
  19.         UrlGeneratorInterface $urlGenerator
  20.     ) {
  21.         $this->translator $translator;
  22.         $this->programService $programService;
  23.     }
  24.     public function getFilters()
  25.     {
  26.         return [
  27.             new TwigFilter('readable', [$this'readable']),
  28.             new TwigFilter('encodeString', [$this'encodeString']),
  29.             new TwigFilter('translateOfferKey', [$this'translateOfferKey']),
  30.             new TwigFilter('translateRegion', [$this'translateRegion']),
  31.             new TwigFilter('translateRecruitmentKey', [$this'translateRecruitmentKey']),
  32.             new TwigFilter('translateCandidateKey', [$this'translateCandidateKey']),
  33.             new TwigFilter('implodeProperty', [$this'implodeProperty']),
  34.             new TwigFilter('youtubeId', [$this'youtubeId']),
  35.             new TwigFilter('formatDateLocale', [$this'formatDateLocale']),
  36.             new TwigFilter('formatGender', [$this'formatGender']),
  37.             new TwigFilter('getFilterFileByType', [$this'getFilterFileByType']),
  38.             new TwigFilter('getContractById', [$this'getContractById']),
  39.             new TwigFilter('getPlaceContract', [$this'getPlaceContract']),
  40.             new TwigFilter('getTutor', [$this'getTutor']),
  41.             new TwigFilter('formatCurrency', [$this'formatCurrency']),
  42.         ];
  43.     }
  44.     public function formatDateLocale($datetime$format)
  45.     {
  46.         $formatter = new \IntlDateFormatter($this->translator->getLocale(), \IntlDateFormatter::FULL\IntlDateFormatter::FULL);
  47.         $formatter->setPattern($format);
  48.         return $formatter->format(\strtotime($datetime));
  49.     }
  50.     public function getFunctions()
  51.     {
  52.         return [
  53.             new TwigFunction('headerMenu', [$this'headerMenu'])
  54.         ];
  55.     }
  56.     public function getTests()
  57.     {
  58.         return array(
  59.             new TwigTest('object', [$this'isObject']),
  60.             new TwigTest('array', [$this'isArray']),
  61.             new TwigTest('booleanTrue', [$this'isBooleanTrue'])
  62.         );
  63.     }
  64.     public function isObject($object)
  65.     {
  66.         return is_object($object);
  67.     }
  68.     public function isArray($value)
  69.     {
  70.         return is_array($value);
  71.     }
  72.     public function isBooleanTrue($value)
  73.     {
  74.         return !!$value;
  75.     }
  76.     public function translateOfferKey($key)
  77.     {
  78.         $key $this->readable($key'fromCamelCase');
  79.         $map = [
  80.             'fullname' => 'Nom de l\'entreprise',
  81.             'website' => 'Site web',
  82.             'presentation' => 'Présentation de la société',
  83.             'representative' => 'Représentant',
  84.             'phone' => 'Téléphone',
  85.             'email' => 'Email',
  86.             'offer type' => 'Type d\'offre',
  87.             'position' => 'Intitulé du poste',
  88.             'office address' => 'Lieu du poste',
  89.             'expect profile' => 'Profil rechéche',
  90.             'description' => 'Description du poste',
  91.             'valid date' => 'Date de validité',
  92.             'description file url' => 'Fiche de poste',
  93.             'logo url' => 'Logo',
  94.             'campuses' => 'Campus concernés',
  95.             'contract' => 'Type de contrat',
  96.             'expertise' => 'Domaine/Expertise',
  97.             'study level' => 'Niveau minimum requis',
  98.             'offer title' => 'Intitulé de l\'offre',
  99.             'company name' => 'Nom de l\'entreprise',
  100.             'skill' => 'Skill',
  101.         ];
  102.         return isset($map[$key]) ? $map[$key] : $key;
  103.     }
  104.     public function translateCandidateKey($key)
  105.     {
  106.         $key $this->readable($key'fromCamelCase');
  107.         $map = [
  108.             'first name' => 'Prénom',
  109.             'last name' => 'Nom',
  110.             'email' => 'Email',
  111.             'level' => 'Niveau d\'étude',
  112.             'phone' => 'Téléphone',
  113.             'gender' => 'Vous êtes',
  114.             'status' => 'Mon statut Éstiam',
  115.             'study level' => 'Niveau d\'étude'
  116.         ];
  117.         return isset($map[$key]) ? $map[$key] : $key;
  118.     }
  119.     public function translateRecruitmentKey($key)
  120.     {
  121.         $key $this->readable($key'fromCamelCase');
  122.         $map = [
  123.             'first name' => 'Prénom',
  124.             'last name' => 'Nom',
  125.             'email' => 'Email',
  126.             'pronoun' => 'Préfix',
  127.             'phone' => 'Téléphone',
  128.             'nationality' => 'Nationalité',
  129.             'living in' => 'Pays de résidence',
  130.             'research' => 'Précision sur votre recherche',
  131.             'cv file' => 'CV',
  132.             'letter file' => 'Lettre de motivation',
  133.             'other files' => 'Autres documents facultatifs'
  134.         ];
  135.         return isset($map[$key]) ? $map[$key] : $key;
  136.     }
  137.     public function translateRegion($region)
  138.     {
  139.         $regions = [
  140.             'Europe' => $this->translator->trans('Europe'),
  141.             'États-Unis' => $this->translator->trans('États-Unis'),
  142.             'Asie' => $this->translator->trans('Asie'),
  143.             'Moyen-Orient' => $this->translator->trans('Moyen-Orient'),
  144.         ];
  145.         return $regions[$region];
  146.     }
  147.     public function readable($string$from)
  148.     {
  149.         return $this->{$from}($string);
  150.     }
  151.     public function encodeString($string$to)
  152.     {
  153.         return $this->{$to}($string);
  154.     }
  155.     function fromCamelCase(string $string)
  156.     {
  157.         // Replace repeated spaces to underscore
  158.         $string preg_replace('/[\s.]+/'' '$string);
  159.         // Replace un-willing chars to hyphen.
  160.         $string preg_replace('/[^0-9a-zA-Z_\-]/'' '$string);
  161.         // Skewer the capital letters
  162.         $string strtolower(preg_replace('/[A-Z]+/'' \0'$string));
  163.         $string trim($string);
  164.         return preg_replace('/[_\-][_\-]+/'' '$string);
  165.     }
  166.     function toSnakeCase(string $input)
  167.     {
  168.         preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!'$input$matches);
  169.         $ret $matches[0];
  170.         foreach ($ret as &$match) {
  171.             $match $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
  172.         }
  173.         return implode('_'$ret);
  174.     }
  175.     function headerMenu(Request $request)
  176.     {
  177.         return $this->programService->headerMenu($request);
  178.     }
  179.     function implodeProperty($object$propertyName 'name')
  180.     {
  181.         return implode(', 'array_column($object$propertyName));
  182.     }
  183.     public function youtubeId($url)
  184.     {
  185.         $parts parse_url($url);
  186.         if (!empty($parts['query'])) {
  187.             parse_str($parts['query'], $query);
  188.             return $query['v'];
  189.         } else {
  190.             return "";
  191.         }
  192.     }
  193.     public function formatGender($genderValue)
  194.     {
  195.         switch ($genderValue) {
  196.             case Common::MALE:
  197.                 return $this->translator->trans('male');
  198.             case Common::FEMALE:
  199.                 return $this->translator->trans('female');
  200.             default:
  201.                 return '';
  202.         }
  203.     }
  204.     public function getFilterFileByType($data$type)
  205.     {
  206.         $result = [];
  207.         foreach ($data as $v) {
  208.             if ($v->type == $type) {
  209.                 $result[] = $v;
  210.             }
  211.         }
  212.         return $result;
  213.     }
  214.     public function getContractById($contractId)
  215.     {
  216.         $contracts = [
  217.             => $this->translator->trans('Apprentissage'),
  218.             => $this->translator->trans('Professionnalisation'),
  219.             => $this->translator->trans('Initiale'),
  220.             => $this->translator->trans('En recherche'),
  221.         ];
  222.         return $contracts[$contractId] ?? null;
  223.     }
  224.     public function getPlaceContract($itemEntreprise)
  225.     {
  226.         if (isset($itemEntreprise['diff']['address']) && $itemEntreprise['diff']['address']) {
  227.             return sprintf(
  228.                 '%s %s %s',
  229.                 $itemEntreprise['diff']['address'],
  230.                 isset($itemEntreprise['diff']['cp'])  ? $itemEntreprise['diff']['cp'] : null,
  231.                 isset($itemEntreprise['diff']['city']) ? $itemEntreprise['diff']['city'] : null,
  232.             );
  233.         } else if (isset($itemEntreprise['headOffice']['street'])) {
  234.             return sprintf(
  235.                 '%s %s %s %s',
  236.                 isset($itemEntreprise['headOffice']['number']) ? $itemEntreprise['headOffice']['number'] : null,
  237.                 $itemEntreprise['headOffice']['street'],
  238.                 isset($itemEntreprise['headOffice']['cp']) ? $itemEntreprise['headOffice']['cp'] : null,
  239.                 isset($itemEntreprise['headOffice']['city']) ? $itemEntreprise['headOffice']['city'] : null,
  240.             );
  241.         }
  242.         return null;
  243.     }
  244.     public function getTutor($entreprise$key)
  245.     {
  246.         if (isset($entreprise['tutor'])) {
  247.             $listLineArr array_map(function ($item) use ($key) {
  248.                 return isset($item[$key]) && $item[$key]  ? '<li>' $item[$key] . '</li>' '';
  249.             }, $entreprise['tutor']);
  250.             return $listLineArr '<ul>' implode(''$listLineArr) . '</ul>' '';
  251.         }
  252.         return null;
  253.     }
  254.     public function formatCurrency(float $amount)
  255.     {
  256.         if ($amount == (int)$amount) {
  257.             return number_format($amount0'.'' ') . '€';
  258.         }
  259.         return str_replace(',00'''number_format($amount2'.'' ')) . '€';
  260.     }
  261. }