sfOutputEscaperIteratorDecorator.class.php 4.6 KB

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