SimpleFunction.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010-2012 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. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_SimpleFunction
  16. {
  17. protected $name;
  18. protected $callable;
  19. protected $options;
  20. protected $arguments = array();
  21. public function __construct($name, $callable, array $options = array())
  22. {
  23. $this->name = $name;
  24. $this->callable = $callable;
  25. $this->options = array_merge(array(
  26. 'needs_environment' => false,
  27. 'needs_context' => false,
  28. 'is_variadic' => false,
  29. 'is_safe' => null,
  30. 'is_safe_callback' => null,
  31. 'node_class' => 'Twig_Node_Expression_Function',
  32. 'deprecated' => false,
  33. 'alternative' => null,
  34. ), $options);
  35. }
  36. public function getName()
  37. {
  38. return $this->name;
  39. }
  40. public function getCallable()
  41. {
  42. return $this->callable;
  43. }
  44. public function getNodeClass()
  45. {
  46. return $this->options['node_class'];
  47. }
  48. public function setArguments($arguments)
  49. {
  50. $this->arguments = $arguments;
  51. }
  52. public function getArguments()
  53. {
  54. return $this->arguments;
  55. }
  56. public function needsEnvironment()
  57. {
  58. return $this->options['needs_environment'];
  59. }
  60. public function needsContext()
  61. {
  62. return $this->options['needs_context'];
  63. }
  64. public function getSafe(Twig_Node $functionArgs)
  65. {
  66. if (null !== $this->options['is_safe']) {
  67. return $this->options['is_safe'];
  68. }
  69. if (null !== $this->options['is_safe_callback']) {
  70. return call_user_func($this->options['is_safe_callback'], $functionArgs);
  71. }
  72. return array();
  73. }
  74. public function isVariadic()
  75. {
  76. return $this->options['is_variadic'];
  77. }
  78. public function isDeprecated()
  79. {
  80. return (bool) $this->options['deprecated'];
  81. }
  82. public function getDeprecatedVersion()
  83. {
  84. return $this->options['deprecated'];
  85. }
  86. public function getAlternative()
  87. {
  88. return $this->options['alternative'];
  89. }
  90. }