sfOutputEscaperArrayDecorator.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 decorator class for arrays.
  11. *
  12. * @see sfOutputEscaper
  13. * @package symfony
  14. * @subpackage view
  15. * @author Mike Squire <mike@somosis.co.uk>
  16. * @version SVN: $Id: sfOutputEscaperArrayDecorator.class.php 9158 2008-05-21 20:32:00Z FabianLange $
  17. */
  18. class sfOutputEscaperArrayDecorator extends sfOutputEscaperGetterDecorator implements Iterator, ArrayAccess, Countable
  19. {
  20. /**
  21. * Used by the iterator to know if the current element is valid.
  22. *
  23. * @var int
  24. */
  25. private $count;
  26. /**
  27. * Reset the array to the beginning (as required for the Iterator interface).
  28. */
  29. public function rewind()
  30. {
  31. reset($this->value);
  32. $this->count = count($this->value);
  33. }
  34. /**
  35. * Get the key associated with the current value (as required by the Iterator interface).
  36. *
  37. * @return string The key
  38. */
  39. public function key()
  40. {
  41. return key($this->value);
  42. }
  43. /**
  44. * Escapes and return the current value (as required by the Iterator interface).
  45. *
  46. * This escapes the value using {@link sfOutputEscaper::escape()} with
  47. * whatever escaping method is set for this instance.
  48. *
  49. * @return mixed The escaped value
  50. */
  51. public function current()
  52. {
  53. return sfOutputEscaper::escape($this->escapingMethod, current($this->value));
  54. }
  55. /**
  56. * Moves to the next element (as required by the Iterator interface).
  57. */
  58. public function next()
  59. {
  60. next($this->value);
  61. $this->count --;
  62. }
  63. /**
  64. * Returns true if the current element is valid (as required by the Iterator interface).
  65. *
  66. * The current element will not be valid if {@link next()} has fallen off the
  67. * end of the array or if there are no elements in the array and {@link
  68. * rewind()} was called.
  69. *
  70. * @return bool The validity of the current element; true if it is valid
  71. */
  72. public function valid()
  73. {
  74. return $this->count > 0;
  75. }
  76. /**
  77. * Returns true if the supplied offset isset in the array (as required by the ArrayAccess interface).
  78. *
  79. * @param string $offset The offset of the value to check existance of
  80. *
  81. * @return bool true if the offset isset; false otherwise
  82. */
  83. public function offsetExists($offset)
  84. {
  85. return isset($this->value[$offset]);
  86. }
  87. /**
  88. * Returns the element associated with the offset supplied (as required by the ArrayAccess interface).
  89. *
  90. * @param string $offset The offset of the value to get
  91. *
  92. * @return mixed The escaped value
  93. */
  94. public function offsetGet($offset)
  95. {
  96. return sfOutputEscaper::escape($this->escapingMethod, $this->value[$offset]);
  97. }
  98. /**
  99. * Throws an exception saying that values cannot be set (this method is
  100. * required for the ArrayAccess interface).
  101. *
  102. * This (and the other sfOutputEscaper classes) are designed to be read only
  103. * so this is an illegal operation.
  104. *
  105. * @param string $offset (ignored)
  106. * @param string $value (ignored)
  107. *
  108. * @throws sfException
  109. */
  110. public function offsetSet($offset, $value)
  111. {
  112. throw new sfException('Cannot set values.');
  113. }
  114. /**
  115. * Throws an exception saying that values cannot be unset (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. *
  123. * @throws sfException
  124. */
  125. public function offsetUnset($offset)
  126. {
  127. throw new sfException('Cannot unset values.');
  128. }
  129. /**
  130. * Returns the size of the array (are required by the Countable interface).
  131. *
  132. * @return int The size of the array
  133. */
  134. public function count()
  135. {
  136. return count($this->value);
  137. }
  138. /**
  139. * Returns the (unescaped) value from the array associated with the key supplied.
  140. *
  141. * @param string $key The key into the array to use
  142. *
  143. * @return mixed The value
  144. */
  145. public function getRaw($key)
  146. {
  147. return $this->value[$key];
  148. }
  149. }