1: <?php
2: namespace TIC\TwigBundle\Base;
3:
4: use Twig\Extension\AbstractExtension;
5: use TIC\CoreBundle\Util\ConvertHelper;
6: use TIC\CoreBundle\Util\IconHelper;
7:
8: /**
9: * Extension Twig de base contenant des méthodes utilitaires communes.
10: */
11: abstract class TICTwigExtension extends AbstractExtension
12: {
13: # protected $packages; // Symfony\Component\Asset\Packages
14: protected $urlHelper; // Symfony\Component\HttpFoundation\UrlHelper
15: protected $translator; // Symfony\Contracts\Translation\TranslatorInterface
16:
17: protected static $strings = [
18: 'true' => 'ext.boolean.true',
19: 'false' => 'ext.boolean.false',
20: 'null' => null,
21: ];
22:
23: /**
24: * Recherche la traduction d'un texte (si le service $translator est disponible).
25: *
26: * @param string $token Chaine de texte à traduire
27: * @return string Chaine de texte traduite
28: */
29: protected function trans(string $token): string
30: {
31: if ($token === '') return '';
32: if (! isset($this->translator)) return $token;
33: return $this->translator->trans($token);
34: }
35:
36: /**
37: * Retourne une chaine texte à partir d'une variable (avec traduction et échappements HTML possibles).
38: *
39: * @param mixed $value Variable à convertir en texte
40: * @param bool $trans Recherche d'une traduction (si c'est une chaine valide)
41: * @param bool $escape Appliquer les échappements HTML
42: * @return string Chaine de texte correspondante
43: */
44: protected function getText($value, bool $trans = false, bool $escape = false): string
45: {
46: if (\is_string($value)) {
47: if ($trans) $value = $this->trans(\trim($value));
48: }
49: return ($escape) ? \htmlspecialchars($value) : (string)$value;
50: }
51:
52: /**
53: * Retourne l'équivalent booléen (ou null) d'une variable quelconque.
54: *
55: * @param mixed $value Variable à convertir en booléen
56: * @param mixed $default Valeur à retourner si la conversion échoue
57: * @return mixed
58: */
59: protected function getBool($value, $default = null)
60: {
61: try {
62: return ConvertHelper::boolean($value);
63: } catch (\UnexpectedValueException $e) {
64: return $default;
65: }
66: }
67:
68: /**
69: * Génération d'un tag HTML pour afficher un icone FontAwesome ou Bootstrap (avec quelques alias utiles).
70: *
71: * @param string $name Classe Bootstrap ou FontAwesome (ex 'fas-user', 'fab-user', 'envelope', 'ban-circle'...)
72: * @param string $state Ajout d'une classe de type 'text-<state>' (ex: primary, info, success, warning, danger)
73: * @return string
74: */
75: protected function getIcon(string $name, string $state=''): string
76: {
77: if ($name === null || $name == '') return '';
78: $class = ($state === '') ? "" : " text-{$state}";
79:
80: // TODO: utiliser un mapping défini dans la configuration (pouvent être étendu et modifié)
81: if (\substr($name, 0, 2) === 'fa')
82: return IconHelper::faTag($name);
83: else
84: return IconHelper::biTag($name);
85: }
86:
87: /**
88: * Conversion d'un hachage en une chaine d'attributs insérable dans un tag HTML.
89: *
90: * @param array $attr Liste d'attributs HTML en clé/valeur
91: * @return string Code HTML contenant les attributs d'un tag
92: */
93: protected function htmlAttr(array $attr=array()): string
94: {
95: if (! \is_array($attr)) return (string)$attr;
96: $html = '';
97: foreach ($attr as $key => $val) {
98: if ($val === NULL || $val === FALSE) continue;
99: if ($val === TRUE) $val = $key;
100: $html.= \sprintf(' %s="%s"', $key, \strtr($val, array('"'=>'&quot;', '&'=>'&amp;')));
101: }
102: return $html;
103: }
104:
105: }
106: