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