<?php
declare(strict_types=1);
/*
* Copyright (C) Office National de Publication et de Communication - All Rights Reserved
*
* Unauthorized copying of this file, via any medium is strictly prohibited by law
* This file is proprietary and confidential
*
* Renvoi le nom final d'un item : libelleSpecifique sinon libelleMEP sinon libelle
*/
namespace App\Twig;
use Twig\TwigFilter;
use App\Entity\Items;
use App\Entity\ItemsInsertion;
use Twig\Extension\AbstractExtension;
use Doctrine\Persistence\ManagerRegistry;
final class ItemsInsertionExtension extends AbstractExtension
{
private $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}
public function getFilters(): array
{
return [
new TwigFilter('itemLibelle', [$this, 'itemLibelle']),
new TwigFilter('itemsLibelle', [$this, 'itemsLibelle']),
new TwigFilter('itemOptions', [$this, 'itemOptions']),
new TwigFilter('itemIdLibelle', [$this, 'itemIdLibelle']),
];
}
public function itemLibelle(ItemsInsertion $ii): ?string
{
$libelle = $ii->getLibelleSpecifique();
if (empty($libelle) ) {
$libelle = $ii->getLibelleMEP();
}
if (empty($libelle) ) {
$libelle = $ii->getLibelle();
}
if (strpos($libelle, 'VIDE') !== false)
return "";
return trim($libelle);
}
public function itemsLibelle(Items $ii): ?string
{
$libelle = $ii->getLibelleWeb() ?? $ii->getLibelleMEP();
if (empty($libelle) ) {
$libelle = $ii->getLibelle();
}
if (strpos($libelle, 'VIDE') !== false)
return "";
return trim($libelle);
}
public function itemOptions(ItemsInsertion $ii): ?string
{
$libelle = $ii->getOptionsSpecifique();
if (empty($libelle) )
$libelle = $ii->getOptions();
return trim($libelle);
}
public function itemIdLibelle(string $id): ?string
{
if ($id == "-3") {
return "Moins de 3 ans";
}
/*
$dpt = $this->doctrine->getRepository(Items::class)->findOneById($id);
return $dpt ? $dpt->getLibelle() : "";
*/
$dpt = $this->doctrine->getRepository(Items::class)->findOneById($id);
// return $dpt ? $dpt->getLibelle() : "";
if ($dpt === null) {
return "";
}
$libelle = $dpt->getLibelleMEP() ?? $dpt->getLibelle() ?? "";
$option = $dpt->getOptions() ?? "";
return trim($libelle . " " . $option);
}
}