1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig; 13 14use Twig\Node\Expression\FilterExpression; 15use Twig\Node\Node; 16 17/** 18 * Represents a template filter. 19 * 20 * @author Fabien Potencier <fabien@symfony.com> 21 * 22 * @see https://twig.symfony.com/doc/templates.html#filters 23 */ 24final class TwigFilter extends AbstractTwigCallable 25{ 26 /** 27 * @param callable|array{class-string, string}|null $callable A callable implementing the filter. If null, you need to overwrite the "node_class" option to customize compilation. 28 */ 29 public function __construct(string $name, $callable = null, array $options = []) 30 { 31 parent::__construct($name, $callable, $options); 32 33 $this->options = array_merge([ 34 'is_safe' => null, 35 'is_safe_callback' => null, 36 'pre_escape' => null, 37 'preserves_safety' => null, 38 'node_class' => FilterExpression::class, 39 ], $this->options); 40 } 41 42 public function getType(): string 43 { 44 return 'filter'; 45 } 46 47 public function getSafe(Node $filterArgs): ?array 48 { 49 if (null !== $this->options['is_safe']) { 50 return $this->options['is_safe']; 51 } 52 53 if (null !== $this->options['is_safe_callback']) { 54 return $this->options['is_safe_callback']($filterArgs); 55 } 56 57 return []; 58 } 59 60 public function getPreservesSafety(): array 61 { 62 return $this->options['preserves_safety'] ?? []; 63 } 64 65 public function getPreEscape(): ?string 66 { 67 return $this->options['pre_escape']; 68 } 69 70 public function getMinimalNumberOfRequiredArguments(): int 71 { 72 return parent::getMinimalNumberOfRequiredArguments() + 1; 73 } 74} 75