Function.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Represents a template function.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. abstract class Twig_Function implements Twig_FunctionInterface
  17. {
  18. protected $options;
  19. protected $arguments = array();
  20. public function __construct(array $options = array())
  21. {
  22. $this->options = array_merge(array(
  23. 'needs_environment' => false,
  24. 'needs_context' => false,
  25. ), $options);
  26. }
  27. public function setArguments($arguments)
  28. {
  29. $this->arguments = $arguments;
  30. }
  31. public function getArguments()
  32. {
  33. return $this->arguments;
  34. }
  35. public function needsEnvironment()
  36. {
  37. return $this->options['needs_environment'];
  38. }
  39. public function needsContext()
  40. {
  41. return $this->options['needs_context'];
  42. }
  43. public function getSafe(Twig_Node $functionArgs)
  44. {
  45. if (isset($this->options['is_safe'])) {
  46. return $this->options['is_safe'];
  47. }
  48. if (isset($this->options['is_safe_callback'])) {
  49. return call_user_func($this->options['is_safe_callback'], $functionArgs);
  50. }
  51. return array();
  52. }
  53. }