src/Twig/CoreExtension.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Services\Core\Core;
  4. use App\Services\Core\Tools;
  5. use App\Services\Core\Users;
  6. use App\Services\Cvs;
  7. use App\Services\MarkdownParserService;
  8. use Twig\Extension\AbstractExtension;
  9. use Twig\TwigFilter;
  10. use Twig\TwigFunction;
  11. class CoreExtension extends AbstractExtension
  12. {
  13.     public function __construct(Core $coreService,
  14.                                 Tools $toolService,
  15.                                 Users $usersService,
  16.                                 Cvs $cvsService,
  17.                                 MarkdownParserService $markdownParser
  18.     ) {
  19.         $this->core $coreService;
  20.         $this->tool $toolService;
  21.         $this->users $usersService;
  22.         $this->cvs $cvsService;
  23.         $this->markdownParser $markdownParser;
  24.     }
  25.     public function getFunctions(): array
  26.     {
  27.         return [
  28.             new  TwigFunction('GetJsonDecode', [$this'GetJsonDecode']),
  29.             new  TwigFunction('asset', [$this'asset']),
  30.             new  TwigFunction('file_exists', [$this'file_exists']),
  31.             new  TwigFunction('getFirstLetters', [$this'getFirstLetters']),
  32.             new  TwigFunction('cleanSubstr', [$this'cleanSubstr']),
  33.             new  TwigFunction('timeago', [$this'timeago']),
  34.             new  TwigFunction('cleanMail', [$this'cleanMail']),
  35.             new  TwigFunction('cleanRemoveText', [$this'cleanRemoveText']),
  36.             new  TwigFunction('getCoreTool', [$this'getCoreTool']),
  37.             new  TwigFunction('ctsToEur', [$this'ctsToEur']),
  38.             new  TwigFunction('getContractPartner', [$this'getContractPartner']),
  39.         ];
  40.     }
  41.     public function getFilters()
  42.     {
  43.         return [
  44.             new TwigFilter('clean_n', [$this'clean_n']),
  45.             new TwigFilter('jour_francais', [$this'traduireJour']),
  46.             new TwigFilter('jour_court_francais', [$this'traduireJourCourt']),
  47.             new TwigFilter('parseMarkdown', [$this'parseMarkdown']),
  48.             new TwigFilter('parseMarkdownElearning', [$this'parseMarkdownElearning']),
  49.             new TwigFilter('parseMarkdownTag', [$this'parseMarkdownTag']),
  50.         ];
  51.     }
  52.     public function parseMarkdown(string $content): string
  53.     {
  54.         return $this->markdownParser->parse($content);
  55.     }
  56.     public function parseMarkdownElearning(string $content): string
  57.     {
  58.         return $this->markdownParser->parseElearning($content);
  59.     }
  60.     public function parseMarkdownTag(string $contentstring $interditKey): string
  61.     {
  62.         return $this->markdownParser->parseTag($content,$interditKey);
  63.     }
  64.     public function getCoreTool($toolID)
  65.     {
  66.         return  $this->tool->getTool($toolID);
  67.     }
  68.     public function cleanRemoveText($text,$html)
  69.     {
  70.         return $this->core->cleanRemoveText($text,$html);
  71.     }
  72.     public function cleanMail($html)
  73.     {
  74.         return $this->core->cleanMail($html);
  75.     }
  76.     public function timeago($datetime)
  77.     {
  78.         return $this->core->timeago($datetime);
  79.     }
  80.     public function cleanSubstr($chain,$number)
  81.     {
  82.         return $this->core->cleanSubstr($chain,$number);
  83.     }
  84.     public function getFirstLetters($chain)
  85.     {
  86.         return $this->core->getFirstLetters($chain);
  87.     }
  88.     public function clean_n($chain)
  89.     {
  90.         return $this->core->clean_n($chain);
  91.     }
  92.     /**
  93.      * Décoder un JSON en Array.
  94.      */
  95.     public function GetJsonDecode($chain)
  96.     {
  97.         return json_decode($chain,true);
  98.     }
  99.     public function asset($chain)
  100.     {
  101.         return $chain;
  102.     }
  103.     public function file_exists($filename)
  104.     {
  105.         return file_exists($filename);
  106.     }
  107.     public function ctsToEur($montant)
  108.     {
  109.         return $this->core->ctsToEur($montant);
  110.     }
  111.     public function getContractPartner($userID)
  112.     {
  113.         return $this->users->getContractPartner($userID);
  114.     }
  115.     public function traduireJour($date)
  116.     {
  117.         $jours = [
  118.             'Monday'    => 'Lundi',
  119.             'Tuesday'   => 'Mardi',
  120.             'Wednesday' => 'Mercredi',
  121.             'Thursday'  => 'Jeudi',
  122.             'Friday'    => 'Vendredi',
  123.             'Saturday'  => 'Samedi',
  124.             'Sunday'    => 'Dimanche',
  125.         ];
  126.         // Assurez-vous que $date est un objet \DateTime
  127.         if (!$date instanceof \DateTime) {
  128.             $date = new \DateTime($date);
  129.         }
  130.         $jourAnglais $date->format('l');
  131.         return $jours[$jourAnglais] ?? 'Jour inconnu';
  132.     }
  133.     public function traduireJourCourt($date)
  134.     {
  135.         $joursCourts = [
  136.             'Monday'    => 'Lun',
  137.             'Tuesday'   => 'Mar',
  138.             'Wednesday' => 'Mer',
  139.             'Thursday'  => 'Jeu',
  140.             'Friday'    => 'Ven',
  141.             'Saturday'  => 'Sam',
  142.             'Sunday'    => 'Dim',
  143.         ];
  144.         // Assurez-vous que $date est un objet \DateTime
  145.         if (!$date instanceof \DateTime) {
  146.             $date = new \DateTime($date);
  147.         }
  148.         $jourAnglais $date->format('l');
  149.         return $joursCourts[$jourAnglais] ?? 'Inconnu';
  150.     }
  151. }