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

Debug toolbar