<?php
namespace App\Misc;
use App\Constant\Common;
use Twig\TwigTest;
use Twig\TwigFilter;
use Twig\TwigFunction;
use App\Service\CampusService;
use App\Service\ProgramService;
use Twig\Extension\AbstractExtension;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class TwigExtension extends AbstractExtension
{
public function __construct(
TranslatorInterface $translator,
ProgramService $programService,
CampusService $campusService,
UrlGeneratorInterface $urlGenerator
) {
$this->translator = $translator;
$this->programService = $programService;
}
public function getFilters()
{
return [
new TwigFilter('readable', [$this, 'readable']),
new TwigFilter('encodeString', [$this, 'encodeString']),
new TwigFilter('translateOfferKey', [$this, 'translateOfferKey']),
new TwigFilter('translateRegion', [$this, 'translateRegion']),
new TwigFilter('translateRecruitmentKey', [$this, 'translateRecruitmentKey']),
new TwigFilter('translateCandidateKey', [$this, 'translateCandidateKey']),
new TwigFilter('implodeProperty', [$this, 'implodeProperty']),
new TwigFilter('youtubeId', [$this, 'youtubeId']),
new TwigFilter('formatDateLocale', [$this, 'formatDateLocale']),
new TwigFilter('formatGender', [$this, 'formatGender']),
new TwigFilter('getFilterFileByType', [$this, 'getFilterFileByType']),
new TwigFilter('getContractById', [$this, 'getContractById']),
new TwigFilter('getPlaceContract', [$this, 'getPlaceContract']),
new TwigFilter('getTutor', [$this, 'getTutor']),
new TwigFilter('formatCurrency', [$this, 'formatCurrency']),
];
}
public function formatDateLocale($datetime, $format)
{
$formatter = new \IntlDateFormatter($this->translator->getLocale(), \IntlDateFormatter::FULL, \IntlDateFormatter::FULL);
$formatter->setPattern($format);
return $formatter->format(\strtotime($datetime));
}
public function getFunctions()
{
return [
new TwigFunction('headerMenu', [$this, 'headerMenu'])
];
}
public function getTests()
{
return array(
new TwigTest('object', [$this, 'isObject']),
new TwigTest('array', [$this, 'isArray']),
new TwigTest('booleanTrue', [$this, 'isBooleanTrue'])
);
}
public function isObject($object)
{
return is_object($object);
}
public function isArray($value)
{
return is_array($value);
}
public function isBooleanTrue($value)
{
return !!$value;
}
public function translateOfferKey($key)
{
$key = $this->readable($key, 'fromCamelCase');
$map = [
'fullname' => 'Nom de l\'entreprise',
'website' => 'Site web',
'presentation' => 'Présentation de la société',
'representative' => 'Représentant',
'phone' => 'Téléphone',
'email' => 'Email',
'offer type' => 'Type d\'offre',
'position' => 'Intitulé du poste',
'office address' => 'Lieu du poste',
'expect profile' => 'Profil rechéche',
'description' => 'Description du poste',
'valid date' => 'Date de validité',
'description file url' => 'Fiche de poste',
'logo url' => 'Logo',
'campuses' => 'Campus concernés',
'contract' => 'Type de contrat',
'expertise' => 'Domaine/Expertise',
'study level' => 'Niveau minimum requis',
'offer title' => 'Intitulé de l\'offre',
'company name' => 'Nom de l\'entreprise',
'skill' => 'Skill',
];
return isset($map[$key]) ? $map[$key] : $key;
}
public function translateCandidateKey($key)
{
$key = $this->readable($key, 'fromCamelCase');
$map = [
'first name' => 'Prénom',
'last name' => 'Nom',
'email' => 'Email',
'level' => 'Niveau d\'étude',
'phone' => 'Téléphone',
'gender' => 'Vous êtes',
'status' => 'Mon statut Éstiam',
'study level' => 'Niveau d\'étude'
];
return isset($map[$key]) ? $map[$key] : $key;
}
public function translateRecruitmentKey($key)
{
$key = $this->readable($key, 'fromCamelCase');
$map = [
'first name' => 'Prénom',
'last name' => 'Nom',
'email' => 'Email',
'pronoun' => 'Préfix',
'phone' => 'Téléphone',
'nationality' => 'Nationalité',
'living in' => 'Pays de résidence',
'research' => 'Précision sur votre recherche',
'cv file' => 'CV',
'letter file' => 'Lettre de motivation',
'other files' => 'Autres documents facultatifs'
];
return isset($map[$key]) ? $map[$key] : $key;
}
public function translateRegion($region)
{
$regions = [
'Europe' => $this->translator->trans('Europe'),
'États-Unis' => $this->translator->trans('États-Unis'),
'Asie' => $this->translator->trans('Asie'),
'Moyen-Orient' => $this->translator->trans('Moyen-Orient'),
];
return $regions[$region];
}
public function readable($string, $from)
{
return $this->{$from}($string);
}
public function encodeString($string, $to)
{
return $this->{$to}($string);
}
function fromCamelCase(string $string)
{
// Replace repeated spaces to underscore
$string = preg_replace('/[\s.]+/', ' ', $string);
// Replace un-willing chars to hyphen.
$string = preg_replace('/[^0-9a-zA-Z_\-]/', ' ', $string);
// Skewer the capital letters
$string = strtolower(preg_replace('/[A-Z]+/', ' \0', $string));
$string = trim($string);
return preg_replace('/[_\-][_\-]+/', ' ', $string);
}
function toSnakeCase(string $input)
{
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
}
return implode('_', $ret);
}
function headerMenu(Request $request)
{
return $this->programService->headerMenu($request);
}
function implodeProperty($object, $propertyName = 'name')
{
return implode(', ', array_column($object, $propertyName));
}
public function youtubeId($url)
{
$parts = parse_url($url);
if (!empty($parts['query'])) {
parse_str($parts['query'], $query);
return $query['v'];
} else {
return "";
}
}
public function formatGender($genderValue)
{
switch ($genderValue) {
case Common::MALE:
return $this->translator->trans('male');
case Common::FEMALE:
return $this->translator->trans('female');
default:
return '';
}
}
public function getFilterFileByType($data, $type)
{
$result = [];
foreach ($data as $v) {
if ($v->type == $type) {
$result[] = $v;
}
}
return $result;
}
public function getContractById($contractId)
{
$contracts = [
1 => $this->translator->trans('Apprentissage'),
2 => $this->translator->trans('Professionnalisation'),
3 => $this->translator->trans('Initiale'),
4 => $this->translator->trans('En recherche'),
];
return $contracts[$contractId] ?? null;
}
public function getPlaceContract($itemEntreprise)
{
if (isset($itemEntreprise['diff']['address']) && $itemEntreprise['diff']['address']) {
return sprintf(
'%s %s %s',
$itemEntreprise['diff']['address'],
isset($itemEntreprise['diff']['cp']) ? $itemEntreprise['diff']['cp'] : null,
isset($itemEntreprise['diff']['city']) ? $itemEntreprise['diff']['city'] : null,
);
} else if (isset($itemEntreprise['headOffice']['street'])) {
return sprintf(
'%s %s %s %s',
isset($itemEntreprise['headOffice']['number']) ? $itemEntreprise['headOffice']['number'] : null,
$itemEntreprise['headOffice']['street'],
isset($itemEntreprise['headOffice']['cp']) ? $itemEntreprise['headOffice']['cp'] : null,
isset($itemEntreprise['headOffice']['city']) ? $itemEntreprise['headOffice']['city'] : null,
);
}
return null;
}
public function getTutor($entreprise, $key)
{
if (isset($entreprise['tutor'])) {
$listLineArr = array_map(function ($item) use ($key) {
return isset($item[$key]) && $item[$key] ? '<li>' . $item[$key] . '</li>' : '';
}, $entreprise['tutor']);
return $listLineArr ? '<ul>' . implode('', $listLineArr) . '</ul>' : '';
}
return null;
}
public function formatCurrency(float $amount)
{
if ($amount == (int)$amount) {
return number_format($amount, 0, '.', ' ') . '€';
}
return str_replace(',00', '', number_format($amount, 2, '.', ' ')) . '€';
}
}