| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfOutputEscaperObjectDecorator extends sfOutputEscaperGetterDecorator |
|---|
| 22 |
{ |
|---|
| 23 |
|
|---|
| 24 |
* Magic PHP method that intercepts method calls, calls them on the objects |
|---|
| 25 |
* that is being escaped and escapes the result. |
|---|
| 26 |
* |
|---|
| 27 |
* The calling of the method is changed slightly to accommodate passing a |
|---|
| 28 |
* specific escaping strategy. An additional parameter is appended to the |
|---|
| 29 |
* argument list which is the escaping strategy. The decorator will remove |
|---|
| 30 |
* and use this parameter as the escaping strategy if it begins with 'esc_' |
|---|
| 31 |
* (the prefix all escaping helper functions have). |
|---|
| 32 |
* |
|---|
| 33 |
* For example if an object, $o, implements methods a() and b($arg): |
|---|
| 34 |
* |
|---|
| 35 |
* $o->a() // Escapes the return value of a() |
|---|
| 36 |
* $o->a(ESC_RAW) // Uses the escaping method ESC_RAW with a() |
|---|
| 37 |
* $o->b('a') // Escapes the return value of b('a') |
|---|
| 38 |
* $o->b('a', ESC_RAW); // Uses the escaping method ESC_RAW with b('a') |
|---|
| 39 |
* |
|---|
| 40 |
* @param string The method on the object to be called |
|---|
| 41 |
* @param array An array of arguments to be passed to the method |
|---|
| 42 |
* |
|---|
| 43 |
* @return mixed The escaped value returned by the method |
|---|
| 44 |
*/ |
|---|
| 45 |
public function __call($method, $args) |
|---|
| 46 |
{ |
|---|
| 47 |
if (count($args) > 0) |
|---|
| 48 |
{ |
|---|
| 49 |
$escapingMethod = $args[count($args) - 1]; |
|---|
| 50 |
if (is_string($escapingMethod) && substr($escapingMethod, 0, 4) === 'esc_') |
|---|
| 51 |
{ |
|---|
| 52 |
array_pop($args); |
|---|
| 53 |
} |
|---|
| 54 |
else |
|---|
| 55 |
{ |
|---|
| 56 |
$escapingMethod = $this->escapingMethod; |
|---|
| 57 |
} |
|---|
| 58 |
} |
|---|
| 59 |
else |
|---|
| 60 |
{ |
|---|
| 61 |
$escapingMethod = $this->escapingMethod; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
$value = call_user_func_array(array($this->value, $method), $args); |
|---|
| 65 |
|
|---|
| 66 |
return sfOutputEscaper::escape($escapingMethod, $value); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
* Returns the result of calling the get() method on the object, bypassing |
|---|
| 71 |
* any escaping, if that method exists. |
|---|
| 72 |
* |
|---|
| 73 |
* If there is not a callable get() method this will throw an exception. |
|---|
| 74 |
* |
|---|
| 75 |
* @param string The parameter to be passed to the get() get method |
|---|
| 76 |
* |
|---|
| 77 |
* @return mixed The unescaped value returned |
|---|
| 78 |
* |
|---|
| 79 |
* @throws <b>sfException</b> if the object does not have a callable get() method |
|---|
| 80 |
*/ |
|---|
| 81 |
public function getRaw($key) |
|---|
| 82 |
{ |
|---|
| 83 |
if (!is_callable(array($this->value, 'get'))) |
|---|
| 84 |
{ |
|---|
| 85 |
throw new sfException('Object does not have a callable get() method.'); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
return $this->value->get($key); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
* Try to call decorated object __toString() method if exists. |
|---|
| 93 |
* |
|---|
| 94 |
* @return string |
|---|
| 95 |
*/ |
|---|
| 96 |
public function __toString() |
|---|
| 97 |
{ |
|---|
| 98 |
return $this->escape($this->escapingMethod, $this->value->__toString()); |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|