vendor/tgalopin/html-sanitizer-bundle/src/Twig/TwigExtension.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the HTML sanitizer project.
  4. *
  5. * (c) Titouan Galopin <galopintitouan@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace HtmlSanitizer\Bundle\Twig;
  11. use Psr\Container\ContainerInterface;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFilter;
  14. /**
  15. * @final
  16. */
  17. class TwigExtension extends AbstractExtension
  18. {
  19. private $sanitizers;
  20. private $default;
  21. public function __construct(ContainerInterface $sanitizers, string $default)
  22. {
  23. $this->sanitizers = $sanitizers;
  24. $this->default = $default;
  25. }
  26. public function getFilters(): array
  27. {
  28. return [
  29. new TwigFilter('sanitize_html', [$this, 'sanitize'], ['is_safe' => ['html']]),
  30. ];
  31. }
  32. public function sanitize(string $html, string $sanitizer = null): string
  33. {
  34. return $this->sanitizers->get($sanitizer ?: $this->default)->sanitize($html);
  35. }
  36. }