| 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: | |
| 10: | |
| 11: | abstract class TICTwigExtension extends AbstractExtension |
| 12: | { |
| 13: | |
| 14: | protected $urlHelper; |
| 15: | protected $translator; |
| 16: | |
| 17: | protected static $strings = [ |
| 18: | 'true' => 'ext.boolean.true', |
| 19: | 'false' => 'ext.boolean.false', |
| 20: | 'null' => null, |
| 21: | ]; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 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: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 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: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 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: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | protected function getIcon(string $name, string $state=''): string |
| 76: | { |
| 77: | if ($name === null || $name == '') return ''; |
| 78: | $class = ($state === '') ? "" : " text-{$state}"; |
| 79: | |
| 80: | |
| 81: | if (\substr($name, 0, 2) === 'fa') |
| 82: | return IconHelper::faTag($name); |
| 83: | else |
| 84: | return IconHelper::biTag($name); |
| 85: | } |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 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('"'=>'"', '&'=>'&'))); |
| 101: | } |
| 102: | return $html; |
| 103: | } |
| 104: | |
| 105: | } |
| 106: | |