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

Debug toolbar