SimpleFilter.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009-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 filter.
  12. *
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_SimpleFilter
  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. 'pre_escape' => null,
  32. 'preserves_safety' => null,
  33. 'node_class' => 'Twig_Node_Expression_Filter',
  34. 'deprecated' => false,
  35. 'alternative' => null,
  36. ), $options);
  37. }
  38. public function getName()
  39. {
  40. return $this->name;
  41. }
  42. public function getCallable()
  43. {
  44. return $this->callable;
  45. }
  46. public function getNodeClass()
  47. {
  48. return $this->options['node_class'];
  49. }
  50. public function setArguments($arguments)
  51. {
  52. $this->arguments = $arguments;
  53. }
  54. public function getArguments()
  55. {
  56. return $this->arguments;
  57. }
  58. public function needsEnvironment()
  59. {
  60. return $this->options['needs_environment'];
  61. }
  62. public function needsContext()
  63. {
  64. return $this->options['needs_context'];
  65. }
  66. public function getSafe(Twig_Node $filterArgs)
  67. {
  68. if (null !== $this->options['is_safe']) {
  69. return $this->options['is_safe'];
  70. }
  71. if (null !== $this->options['is_safe_callback']) {
  72. return call_user_func($this->options['is_safe_callback'], $filterArgs);
  73. }
  74. }
  75. public function getPreservesSafety()
  76. {
  77. return $this->options['preserves_safety'];
  78. }
  79. public function getPreEscape()
  80. {
  81. return $this->options['pre_escape'];
  82. }
  83. public function isVariadic()
  84. {
  85. return $this->options['is_variadic'];
  86. }
  87. public function isDeprecated()
  88. {
  89. return (bool) $this->options['deprecated'];
  90. }
  91. public function getDeprecatedVersion()
  92. {
  93. return $this->options['deprecated'];
  94. }
  95. public function getAlternative()
  96. {
  97. return $this->options['alternative'];
  98. }
  99. }