<?php
namespace App\Twig;
use App\Services\Core\Core;
use App\Services\Core\Tools;
use App\Services\Core\Users;
use App\Services\Cvs;
use App\Services\MarkdownParserService;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class CoreExtension extends AbstractExtension
{
public function __construct(Core $coreService,
Tools $toolService,
Users $usersService,
Cvs $cvsService,
MarkdownParserService $markdownParser
) {
$this->core = $coreService;
$this->tool = $toolService;
$this->users = $usersService;
$this->cvs = $cvsService;
$this->markdownParser = $markdownParser;
}
public function getFunctions(): array
{
return [
new TwigFunction('GetJsonDecode', [$this, 'GetJsonDecode']),
new TwigFunction('asset', [$this, 'asset']),
new TwigFunction('file_exists', [$this, 'file_exists']),
new TwigFunction('getFirstLetters', [$this, 'getFirstLetters']),
new TwigFunction('cleanSubstr', [$this, 'cleanSubstr']),
new TwigFunction('timeago', [$this, 'timeago']),
new TwigFunction('cleanMail', [$this, 'cleanMail']),
new TwigFunction('cleanRemoveText', [$this, 'cleanRemoveText']),
new TwigFunction('getCoreTool', [$this, 'getCoreTool']),
new TwigFunction('ctsToEur', [$this, 'ctsToEur']),
new TwigFunction('getContractPartner', [$this, 'getContractPartner']),
];
}
public function getFilters()
{
return [
new TwigFilter('clean_n', [$this, 'clean_n']),
new TwigFilter('jour_francais', [$this, 'traduireJour']),
new TwigFilter('jour_court_francais', [$this, 'traduireJourCourt']),
new TwigFilter('parseMarkdown', [$this, 'parseMarkdown']),
new TwigFilter('parseMarkdownElearning', [$this, 'parseMarkdownElearning']),
new TwigFilter('parseMarkdownTag', [$this, 'parseMarkdownTag']),
];
}
public function parseMarkdown(string $content): string
{
return $this->markdownParser->parse($content);
}
public function parseMarkdownElearning(string $content): string
{
return $this->markdownParser->parseElearning($content);
}
public function parseMarkdownTag(string $content, string $interditKey): string
{
return $this->markdownParser->parseTag($content,$interditKey);
}
public function getCoreTool($toolID)
{
return $this->tool->getTool($toolID);
}
public function cleanRemoveText($text,$html)
{
return $this->core->cleanRemoveText($text,$html);
}
public function cleanMail($html)
{
return $this->core->cleanMail($html);
}
public function timeago($datetime)
{
return $this->core->timeago($datetime);
}
public function cleanSubstr($chain,$number)
{
return $this->core->cleanSubstr($chain,$number);
}
public function getFirstLetters($chain)
{
return $this->core->getFirstLetters($chain);
}
public function clean_n($chain)
{
return $this->core->clean_n($chain);
}
/**
* Décoder un JSON en Array.
*/
public function GetJsonDecode($chain)
{
return json_decode($chain,true);
}
public function asset($chain)
{
return $chain;
}
public function file_exists($filename)
{
return file_exists($filename);
}
public function ctsToEur($montant)
{
return $this->core->ctsToEur($montant);
}
public function getContractPartner($userID)
{
return $this->users->getContractPartner($userID);
}
public function traduireJour($date)
{
$jours = [
'Monday' => 'Lundi',
'Tuesday' => 'Mardi',
'Wednesday' => 'Mercredi',
'Thursday' => 'Jeudi',
'Friday' => 'Vendredi',
'Saturday' => 'Samedi',
'Sunday' => 'Dimanche',
];
// Assurez-vous que $date est un objet \DateTime
if (!$date instanceof \DateTime) {
$date = new \DateTime($date);
}
$jourAnglais = $date->format('l');
return $jours[$jourAnglais] ?? 'Jour inconnu';
}
public function traduireJourCourt($date)
{
$joursCourts = [
'Monday' => 'Lun',
'Tuesday' => 'Mar',
'Wednesday' => 'Mer',
'Thursday' => 'Jeu',
'Friday' => 'Ven',
'Saturday' => 'Sam',
'Sunday' => 'Dim',
];
// Assurez-vous que $date est un objet \DateTime
if (!$date instanceof \DateTime) {
$date = new \DateTime($date);
}
$jourAnglais = $date->format('l');
return $joursCourts[$jourAnglais] ?? 'Inconnu';
}
}