sfViewParameterHolder.class.php 5.5 KB

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