Source of file TICTwigExtension.php

Size: 3,475 Bytes - Last Modified: 2023-11-16T22:56:02+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/twig-bundle/src/Base/TICTwigExtension.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
<?php
namespace TIC\TwigBundle\Base;

use Twig\Extension\AbstractExtension;
use TIC\CoreBundle\Util\ConvertHelper;
use TIC\CoreBundle\Util\IconHelper;

/**
 * Extension Twig de base contenant des méthodes utilitaires communes.
 */
abstract class TICTwigExtension extends AbstractExtension
{
#	protected $packages;    // Symfony\Component\Asset\Packages
	protected $urlHelper;   // Symfony\Component\HttpFoundation\UrlHelper
	protected $translator;  // Symfony\Contracts\Translation\TranslatorInterface

	protected static $strings = [
		'true'  => 'ext.boolean.true',
		'false' => 'ext.boolean.false',
		'null'  => null,
	];

	/**
	 * Recherche la traduction d'un texte (si le service $translator est disponible).
	 *
	 * @param   string  $token      Chaine de texte à traduire
	 * @return  string              Chaine de texte traduite
	 */
	protected function trans(string $token): string
	{
		if ($token === '') return '';
		if (! isset($this->translator)) return $token;
		return $this->translator->trans($token);
	}

	/**
	 * Retourne une chaine texte à partir d'une variable (avec traduction et échappements HTML possibles).
	 *
	 * @param   mixed   $value      Variable à convertir en texte
	 * @param   bool    $trans      Recherche d'une traduction (si c'est une chaine valide)
	 * @param   bool    $escape     Appliquer les échappements HTML
	 * @return  string              Chaine de texte correspondante
	 */
	protected function getText($value, bool $trans = false, bool $escape = false): string
	{
		if (\is_string($value)) {
			if ($trans) $value = $this->trans(\trim($value));
		}
		return ($escape) ? \htmlspecialchars($value) : (string)$value;
	}

	/**
	 * Retourne l'équivalent booléen (ou null) d'une variable quelconque.
	 *
	 * @param   mixed   $value      Variable à convertir en booléen
	 * @param   mixed   $default    Valeur à retourner si la conversion échoue
	 * @return  mixed
	 */
	protected function getBool($value, $default = null)
	{
		try {
			return ConvertHelper::boolean($value);
		} catch (\UnexpectedValueException $e) {
			return $default;
		}
	}

	/**
	 * Génération d'un tag HTML pour afficher un icone FontAwesome ou Bootstrap (avec quelques alias utiles).
	 *
	 * @param   string  $name       Classe Bootstrap ou FontAwesome (ex 'fas-user', 'fab-user', 'envelope', 'ban-circle'...)
	 * @param   string  $state      Ajout d'une classe de type 'text-<state>' (ex: primary, info, success, warning, danger)
	 * @return  string
	 */
	protected function getIcon(string $name, string $state=''): string
	{
		if ($name === null || $name == '') return '';
		$class = ($state === '') ? "" : " text-{$state}";

		// TODO: utiliser un mapping défini dans la configuration (pouvent être étendu et modifié)
		if (\substr($name, 0, 2) === 'fa')
			return IconHelper::faTag($name);
		else
			return IconHelper::biTag($name);
	}

	/**
	 * Conversion d'un hachage en une chaine d'attributs insérable dans un tag HTML.
	 *
	 * @param   array   $attr       Liste d'attributs HTML en clé/valeur
	 * @return  string              Code HTML contenant les attributs d'un tag
	 */
	protected function htmlAttr(array $attr=array()): string
	{
		if (! \is_array($attr)) return (string)$attr;
		$html = '';
		foreach ($attr as $key => $val) {
			if ($val === NULL || $val === FALSE) continue;
			if ($val === TRUE) $val = $key;
			$html.= \sprintf(' %s="%s"', $key, \strtr($val, array('"'=>'&quot;', '&'=>'&amp;')));
		}
		return $html;
	}

}