1. sfOutputEscaperIteratorDecorator.class.php
  2. /** * Output escaping iterator decorator. * * This takes an object that implements the Traversable interface and turns it * into an iterator with each value escaped. * * Note: Prior to PHP 5.1, the IteratorIterator class was not implemented in the * core of PHP. This means that although it will still work with classes that * implement Iterator or IteratorAggregate, internal PHP classes that only * implement the Traversable interface will cause the constructor to throw an * exception. * * @see sfOutputEscaper * @package symfony * @subpackage view * @author Mike Squire * @version SVN: $Id: sfOutputEscaperIteratorDecorator.class.php 23436 2009-10-29 16:10:39Z fabien $ */
  3. class sfOutputEscaperIteratorDecorator extends sfOutputEscaperObjectDecorator implements Iterator, ArrayAccess
  4. {
  5. /**
  6. * The iterator to be used.
  7. *
  8. * @var IteratorIterator
  9. */
  10. private $iterator;
  11. /**
  12. * Constructs a new escaping iteratoror using the escaping method and value supplied.
  13. *
  14. * @param string $escapingMethod The escaping method to use
  15. * @param Traversable $value The iterator to escape
  16. */
  17. public function __construct($escapingMethod, Traversable $value)
  18. {
  19. // Set the original value for __call(). Set our own iterator because passing
  20. // it to IteratorIterator will lose any other method calls.
  21. parent::__construct($escapingMethod, $value);
  22. $this->iterator = new IteratorIterator($value);
  23. }
  24. /**
  25. * Resets the iterator (as required by the Iterator interface).
  26. *
  27. * @return bool true, if the iterator rewinds successfully otherwise false
  28. */
  29. public function rewind()
  30. {
  31. return $this->iterator->rewind();
  32. }
  33. /**
  34. * Escapes and gets the current element (as required by the Iterator interface).
  35. *
  36. * @return mixed The escaped value
  37. */
  38. public function current()
  39. {
  40. return sfOutputEscaper::escape($this->escapingMethod, $this->iterator->current());
  41. }
  42. /**
  43. * Gets the current key (as required by the Iterator interface).
  44. *
  45. * @return string Iterator key
  46. */
  47. public function key()
  48. {
  49. return $this->iterator->key();
  50. }
  51. /**
  52. * Moves to the next element in the iterator (as required by the Iterator interface).
  53. */
  54. public function next()
  55. {
  56. return $this->iterator->next();
  57. }
  58. /**
  59. * Returns whether the current element is valid or not (as required by the
  60. * Iterator interface).
  61. *
  62. * @return bool true if the current element is valid; false otherwise
  63. */
  64. public function valid()
  65. {
  66. return $this->iterator->valid();
  67. }
  68. /**
  69. * Returns true if the supplied offset isset in the array (as required by the ArrayAccess interface).
  70. *
  71. * @param string $offset The offset of the value to check existance of
  72. *
  73. * @return bool true if the offset isset; false otherwise
  74. */
  75. public function offsetExists($offset)
  76. {
  77. return isset($this->value[$offset]);
  78. }
  79. /**
  80. * Returns the element associated with the offset supplied (as required by the ArrayAccess interface).
  81. *
  82. * @param string $offset The offset of the value to get
  83. *
  84. * @return mixed The escaped value
  85. */
  86. public function offsetGet($offset)
  87. {
  88. return sfOutputEscaper::escape($this->escapingMethod, $this->value[$offset]);
  89. }
  90. /**
  91. * Throws an exception saying that values cannot be set (this method is
  92. * required for the ArrayAccess interface).
  93. *
  94. * This (and the other sfOutputEscaper classes) are designed to be read only
  95. * so this is an illegal operation.
  96. *
  97. * @param string $offset (ignored)
  98. * @param string $value (ignored)
  99. *
  100. * @throws sfException
  101. */
  102. public function offsetSet($offset, $value)
  103. {
  104. throw new sfException('Cannot set values.');
  105. }
  106. /**
  107. * Throws an exception saying that values cannot be unset (this method is
  108. * required for the ArrayAccess interface).
  109. *
  110. * This (and the other sfOutputEscaper classes) are designed to be read only
  111. * so this is an illegal operation.
  112. *
  113. * @param string $offset (ignored)
  114. *
  115. * @throws sfException
  116. */
  117. public function offsetUnset($offset)
  118. {
  119. throw new sfException('Cannot unset values.');
  120. }
  121. }

Debug toolbar