| 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 |
namespace ElementorDeps\Twig; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Node\Expression\FunctionExpression; |
| 14 |
use ElementorDeps\Twig\Node\Node; |
| 15 |
/** |
| 16 |
* Represents a template function. |
| 17 |
* |
| 18 |
* @author Fabien Potencier <fabien@symfony.com> |
| 19 |
* |
| 20 |
* @see https://twig.symfony.com/doc/templates.html#functions |
| 21 |
*/ |
| 22 |
final class TwigFunction |
| 23 |
{ |
| 24 |
private $name; |
| 25 |
private $callable; |
| 26 |
private $options; |
| 27 |
private $arguments = []; |
| 28 |
/** |
| 29 |
* @param callable|array{class-string, string}|null $callable A callable implementing the function. If null, you need to overwrite the "node_class" option to customize compilation. |
| 30 |
*/ |
| 31 |
public function __construct(string $name, $callable = null, array $options = []) |
| 32 |
{ |
| 33 |
$this->name = $name; |
| 34 |
$this->callable = $callable; |
| 35 |
$this->options = \array_merge(['needs_environment' => \false, 'needs_context' => \false, 'needs_charset' => \false, 'is_variadic' => \false, 'is_safe' => null, 'is_safe_callback' => null, 'node_class' => FunctionExpression::class, 'deprecated' => \false, 'deprecating_package' => '', 'alternative' => null], $options); |
| 36 |
} |
| 37 |
public function getName() : string |
| 38 |
{ |
| 39 |
return $this->name; |
| 40 |
} |
| 41 |
/** |
| 42 |
* Returns the callable to execute for this function. |
| 43 |
* |
| 44 |
* @return callable|array{class-string, string}|null |
| 45 |
*/ |
| 46 |
public function getCallable() |
| 47 |
{ |
| 48 |
return $this->callable; |
| 49 |
} |
| 50 |
public function getNodeClass() : string |
| 51 |
{ |
| 52 |
return $this->options['node_class']; |
| 53 |
} |
| 54 |
public function setArguments(array $arguments) : void |
| 55 |
{ |
| 56 |
$this->arguments = $arguments; |
| 57 |
} |
| 58 |
public function getArguments() : array |
| 59 |
{ |
| 60 |
return $this->arguments; |
| 61 |
} |
| 62 |
public function needsCharset() : bool |
| 63 |
{ |
| 64 |
return $this->options['needs_charset']; |
| 65 |
} |
| 66 |
public function needsEnvironment() : bool |
| 67 |
{ |
| 68 |
return $this->options['needs_environment']; |
| 69 |
} |
| 70 |
public function needsContext() : bool |
| 71 |
{ |
| 72 |
return $this->options['needs_context']; |
| 73 |
} |
| 74 |
public function getSafe(Node $functionArgs) : ?array |
| 75 |
{ |
| 76 |
if (null !== $this->options['is_safe']) { |
| 77 |
return $this->options['is_safe']; |
| 78 |
} |
| 79 |
if (null !== $this->options['is_safe_callback']) { |
| 80 |
return $this->options['is_safe_callback']($functionArgs); |
| 81 |
} |
| 82 |
return []; |
| 83 |
} |
| 84 |
public function isVariadic() : bool |
| 85 |
{ |
| 86 |
return (bool) $this->options['is_variadic']; |
| 87 |
} |
| 88 |
public function isDeprecated() : bool |
| 89 |
{ |
| 90 |
return (bool) $this->options['deprecated']; |
| 91 |
} |
| 92 |
public function getDeprecatingPackage() : string |
| 93 |
{ |
| 94 |
return $this->options['deprecating_package']; |
| 95 |
} |
| 96 |
public function getDeprecatedVersion() : string |
| 97 |
{ |
| 98 |
return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated']; |
| 99 |
} |
| 100 |
public function getAlternative() : ?string |
| 101 |
{ |
| 102 |
return $this->options['alternative']; |
| 103 |
} |
| 104 |
} |
| 105 |
|