|
26 | 26 | * @author Mike Squire <[email protected]> |
27 | 27 | * @version SVN: $Id: sfOutputEscaperIteratorDecorator.class.php 3232 2007-01-11 20:51:54Z fabien $ |
28 | 28 | */ |
29 | | -class sfOutputEscaperIteratorDecorator extends sfOutputEscaperObjectDecorator implements Iterator, Countable, ArrayAccess |
| 29 | +class sfOutputEscaperIteratorDecorator extends sfOutputEscaperGetterDecorator implements Iterator, Countable, ArrayAccess |
30 | 30 | { |
31 | 31 | /** |
32 | 32 | * The iterator to be used. |
@@ -99,7 +99,7 @@ public function valid() |
99 | 99 | { |
100 | 100 | return $this->iterator->valid(); |
101 | 101 | } |
102 | | - |
| 102 | + |
103 | 103 | /** |
104 | 104 | * Returns true if the supplied offset is set in the array (as required by |
105 | 105 | * the ArrayAccess interface). |
@@ -167,4 +167,82 @@ public function count() |
167 | 167 | { |
168 | 168 | return count($this->value); |
169 | 169 | } |
| 170 | + |
| 171 | + /** |
| 172 | + * Returns the result of calling the get() method on the object, bypassing |
| 173 | + * any escaping, if that method exists. |
| 174 | + * |
| 175 | + * If there is not a callable get() method this will throw an exception. |
| 176 | + * |
| 177 | + * @param string The parameter to be passed to the get() get method |
| 178 | + * |
| 179 | + * @return mixed The unescaped value returned |
| 180 | + * |
| 181 | + * @throws <b>sfException</b> if the object does not have a callable get() method |
| 182 | + */ |
| 183 | + public function getRaw($key) |
| 184 | + { |
| 185 | + if (!is_callable(array($this->value, 'get'))) |
| 186 | + { |
| 187 | + throw new sfException('Object does not have a callable get() method.'); |
| 188 | + } |
| 189 | + |
| 190 | + return $this->value->get($key); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Magic PHP method that intercepts method calls, calls them on the objects |
| 195 | + * that is being escaped and escapes the result. |
| 196 | + * |
| 197 | + * The calling of the method is changed slightly to accommodate passing a |
| 198 | + * specific escaping strategy. An additional parameter is appended to the |
| 199 | + * argument list which is the escaping strategy. The decorator will remove |
| 200 | + * and use this parameter as the escaping strategy if it begins with 'esc_' |
| 201 | + * (the prefix all escaping helper functions have). |
| 202 | + * |
| 203 | + * For example if an object, $o, implements methods a() and b($arg): |
| 204 | + * |
| 205 | + * $o->a() // Escapes the return value of a() |
| 206 | + * $o->a(ESC_RAW) // Uses the escaping method ESC_RAW with a() |
| 207 | + * $o->b('a') // Escapes the return value of b('a') |
| 208 | + * $o->b('a', ESC_RAW); // Uses the escaping method ESC_RAW with b('a') |
| 209 | + * |
| 210 | + * @param string The method on the object to be called |
| 211 | + * @param array An array of arguments to be passed to the method |
| 212 | + * |
| 213 | + * @return mixed The escaped value returned by the method |
| 214 | + */ |
| 215 | + public function __call($method, $args) |
| 216 | + { |
| 217 | + if (count($args) > 0) |
| 218 | + { |
| 219 | + $escapingMethod = $args[count($args) - 1]; |
| 220 | + if (is_string($escapingMethod) && substr($escapingMethod, 0, 4) === 'esc_') |
| 221 | + { |
| 222 | + array_pop($args); |
| 223 | + } |
| 224 | + else |
| 225 | + { |
| 226 | + $escapingMethod = $this->escapingMethod; |
| 227 | + } |
| 228 | + } |
| 229 | + else |
| 230 | + { |
| 231 | + $escapingMethod = $this->escapingMethod; |
| 232 | + } |
| 233 | + |
| 234 | + $value = call_user_func_array(array($this->value, $method), $args); |
| 235 | + |
| 236 | + return sfOutputEscaper::escape($escapingMethod, $value); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Try to call decorated object __toString() method if exists. |
| 241 | + * |
| 242 | + * @return string |
| 243 | + */ |
| 244 | + public function __toString() |
| 245 | + { |
| 246 | + return $this->escape($this->escapingMethod, $this->value->__toString()); |
| 247 | + } |
170 | 248 | } |
0 commit comments