1. sfViewParameterHolder.class.php
  2. /** * sfViewParameterHolder stores all variables that will be available to the template. * * It can also escape variables with an escaping method. * * @package symfony * @subpackage view * @author Fabien Potencier * @version SVN: $Id: sfViewParameterHolder.class.php 21884 2009-09-11 07:38:24Z fabien $ */
  3. class sfViewParameterHolder extends sfParameterHolder
  4. {
  5. protected
  6. $dispatcher = null,
  7. $escaping = null,
  8. $escapingMethod = null;
  9. /**
  10. * Constructor.
  11. */
  12. public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $options = array())
  13. {
  14. $this->initialize($dispatcher, $parameters, $options);
  15. }
  16. /**
  17. * Initializes this view parameter holder.
  18. *
  19. * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance.
  20. * @param array $parameters An associative array of initialization parameters.
  21. * @param array $options An associative array of options.
  22. *
  23. * <b>Options:</b>
  24. *
  25. * # <b>escaping_strategy</b> - [off] - The escaping strategy (on or off)
  26. * # <b>escaping_method</b> - [ESC_SPECIALCHARS] - The escaping method (ESC_RAW, ESC_ENTITIES, ESC_JS, ESC_JS_NO_ENTITIES, or ESC_SPECIALCHARS)
  27. *
  28. * @return bool true, if initialization completes successfully, otherwise false.
  29. *
  30. * @throws sfInitializationException If an error occurs while initializing this view parameter holder.
  31. */
  32. public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $options = array())
  33. {
  34. $this->dispatcher = $dispatcher;
  35. $this->add($parameters);
  36. $this->setEscaping(isset($options['escaping_strategy']) ? $options['escaping_strategy'] : false);
  37. $this->setEscapingMethod(isset($options['escaping_method']) ? $options['escaping_method'] : 'ESC_SPECIALCHARS');
  38. }
  39. /**
  40. * Returns true if the current object acts as an escaper.
  41. *
  42. * @return bool true if the current object acts as an escaper, false otherwise
  43. */
  44. public function isEscaped()
  45. {
  46. return in_array($this->getEscaping(), array('on', 'true', true), true);
  47. }
  48. /**
  49. * Returns an array representation of the view parameters.
  50. *
  51. * @return array An array of view parameters
  52. *
  53. * @throws InvalidArgumentException
  54. */
  55. public function toArray()
  56. {
  57. $event = $this->dispatcher->filter(new sfEvent($this, 'template.filter_parameters'), $this->getAll());
  58. $parameters = $event->getReturnValue();
  59. $attributes = array();
  60. if ($this->isEscaped())
  61. {
  62. $attributes['sf_data'] = sfOutputEscaper::escape($this->getEscapingMethod(), $parameters);
  63. foreach ($attributes['sf_data'] as $key => $value)
  64. {
  65. $attributes[$key] = $value;
  66. }
  67. }
  68. else if (in_array($this->getEscaping(), array('off', false), true))
  69. {
  70. $attributes = $parameters;
  71. $attributes['sf_data'] = sfOutputEscaper::escape(ESC_RAW, $parameters);
  72. }
  73. else
  74. {
  75. throw new InvalidArgumentException(sprintf('Unknown strategy "%s".', $this->getEscaping()));
  76. }
  77. return $attributes;
  78. }
  79. /**
  80. * Gets the default escaping strategy associated with this view.
  81. *
  82. * The escaping strategy specifies how the variables get passed to the view.
  83. *
  84. * @return string the escaping strategy
  85. */
  86. public function getEscaping()
  87. {
  88. return $this->escaping;
  89. }
  90. /**
  91. * Sets the escape character strategy.
  92. *
  93. * @param string $escaping Escape code
  94. */
  95. public function setEscaping($escaping)
  96. {
  97. $this->escaping = $escaping;
  98. }
  99. /**
  100. * Returns the name of the function that is to be used as the escaping method.
  101. *
  102. * If the escaping method is empty, then that is returned. The default value
  103. * specified by the sub-class will be used. If the method does not exist (in
  104. * the sense there is no define associated with the method), an exception is
  105. * thrown.
  106. *
  107. * @return string The escaping method as the name of the function to use
  108. *
  109. * @throws InvalidArgumentException If the method does not exist
  110. */
  111. public function getEscapingMethod()
  112. {
  113. if (empty($this->escapingMethod))
  114. {
  115. return $this->escapingMethod;
  116. }
  117. if (!defined($this->escapingMethod))
  118. {
  119. throw new InvalidArgumentException(sprintf('The escaping method "%s" is not available.', $this->escapingMethod));
  120. }
  121. return constant($this->escapingMethod);
  122. }
  123. /**
  124. * Sets the escaping method for the current view.
  125. *
  126. * @param string $method Method for escaping
  127. */
  128. public function setEscapingMethod($method)
  129. {
  130. $this->escapingMethod = $method;
  131. }
  132. /**
  133. * Serializes the current instance.
  134. *
  135. * @return array Objects instance
  136. */
  137. public function serialize()
  138. {
  139. return serialize(array($this->getAll(), $this->escapingMethod, $this->escaping));
  140. }
  141. /**
  142. * Unserializes a sfViewParameterHolder instance.
  143. *
  144. * @param string $serialized The serialized instance data
  145. */
  146. public function unserialize($serialized)
  147. {
  148. list($this->parameters, $escapingMethod, $escaping) = unserialize($serialized);
  149. $this->initialize(sfContext::hasInstance() ? sfContext::getInstance()->getEventDispatcher() : new sfEventDispatcher());
  150. $this->setEscapingMethod($escapingMethod);
  151. $this->setEscaping($escaping);
  152. }
  153. }

Debug toolbar