PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/OutputEscaper/ObjectDecorator.php

https://github.com/thewiredman/symfony
PHP | 119 lines | 40 code | 11 blank | 68 comment | 6 complexity | 86844c73d990c02bcac793a44cf05692 MD5 | raw file
  1. <?php
  2. namespace Symfony\Component\OutputEscaper;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Output escaping object decorator that intercepts all method calls and escapes
  13. * their return values.
  14. *
  15. * @see Escaper
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @author Mike Squire <mike@somosis.co.uk>
  18. */
  19. class ObjectDecorator extends GetterDecorator
  20. {
  21. /**
  22. * Magic PHP method that intercepts method calls, calls them on the objects
  23. * that is being escaped and escapes the result.
  24. *
  25. * The calling of the method is changed slightly to accommodate passing a
  26. * specific escaping strategy. An additional parameter is appended to the
  27. * argument list which is the escaping strategy. The decorator will remove
  28. * and use this parameter as the escaping strategy if it begins with 'esc_'.
  29. *
  30. * For example if an object, $o, implements methods a() and b($arg):
  31. *
  32. * $o->a() // Escapes the return value of a()
  33. * $o->a('esc_raw') // Uses the escaping strategy 'raw' with a()
  34. * $o->b('a') // Escapes the return value of b('a')
  35. * $o->b('a', 'esc_raw'); // Uses the escaping strategy 'raw' with b('a')
  36. *
  37. * @param string $method The method on the object to be called
  38. * @param array $args An array of arguments to be passed to the method
  39. *
  40. * @return mixed The escaped value returned by the method
  41. */
  42. public function __call($method, $args)
  43. {
  44. if (count($args) > 0) {
  45. $escaper = $args[count($args) - 1];
  46. if (is_string($escaper) && 'esc_' === substr($escaper, 0, 4)) {
  47. $escaper = substr($escaper, 4);
  48. array_pop($args);
  49. } else {
  50. $escaper = $this->escaper;
  51. }
  52. } else {
  53. $escaper = $this->escaper;
  54. }
  55. $value = call_user_func_array(array($this->value, $method), $args);
  56. return Escaper::escape($escaper, $value);
  57. }
  58. /**
  59. * Returns the result of calling the get() method on the object, bypassing
  60. * any escaping, if that method exists.
  61. *
  62. * If there is not a callable get() method this will throw an exception.
  63. *
  64. * @param string $key The parameter to be passed to the get() get method
  65. *
  66. * @return mixed The unescaped value returned
  67. *
  68. * @throws \LogicException if the object does not have a callable get() method
  69. */
  70. public function getRaw($key)
  71. {
  72. if (!is_callable(array($this->value, 'get'))) {
  73. throw new \LogicException('Object does not have a callable get() method.');
  74. }
  75. return $this->value->get($key);
  76. }
  77. /**
  78. * Try to call decorated object __toString() method if exists.
  79. *
  80. * @return string
  81. */
  82. public function __toString()
  83. {
  84. return $this->escape($this->escaper, (string) $this->value);
  85. }
  86. /**
  87. * Gets a value from the escaper.
  88. *
  89. * @param string $key The name of the value to get
  90. *
  91. * @return mixed The value from the wrapped object
  92. */
  93. public function __get($key)
  94. {
  95. return $this->escape($this->escaper, $this->value->$key);
  96. }
  97. /**
  98. * Checks whether a value is set on the wrapped object.
  99. *
  100. * @param string $key The name of the value to check
  101. *
  102. * @return boolean Returns true if the value is set
  103. */
  104. public function __isset($key)
  105. {
  106. return isset($this->value->$key);
  107. }
  108. }