Development

/branches/1.3/lib/escaper/sfOutputEscaperArrayDecorator.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/escaper/sfOutputEscaperArrayDecorator.class.php

Revision 27752, 4.3 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] fixed initializion of output escaper array iterator (closes #8202)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Output escaping decorator class for arrays.
13  *
14  * @see        sfOutputEscaper
15  * @package    symfony
16  * @subpackage view
17  * @author     Mike Squire <mike@somosis.co.uk>
18  * @version    SVN: $Id$
19  */
20 class sfOutputEscaperArrayDecorator extends sfOutputEscaperGetterDecorator implements Iterator, ArrayAccess, Countable
21 {
22   /**
23    * Used by the iterator to know if the current element is valid.
24    *
25    * @var int
26    */
27   private $count;
28
29   /**
30    * Constructor.
31    *
32    * @see sfOutputEscaper
33    */
34   public function __construct($escapingMethod, $value)
35   {
36     parent::__construct($escapingMethod, $value);
37
38     $this->count = count($this->value);
39   }
40
41   /**
42    * Reset the array to the beginning (as required for the Iterator interface).
43    */
44   public function rewind()
45   {
46     reset($this->value);
47
48     $this->count = count($this->value);
49   }
50
51   /**
52    * Get the key associated with the current value (as required by the Iterator interface).
53    *
54    * @return string The key
55    */
56   public function key()
57   {
58     return key($this->value);
59   }
60
61   /**
62    * Escapes and return the current value (as required by the Iterator interface).
63    *
64    * This escapes the value using {@link sfOutputEscaper::escape()} with
65    * whatever escaping method is set for this instance.
66    *
67    * @return mixed The escaped value
68    */
69   public function current()
70   {
71     return sfOutputEscaper::escape($this->escapingMethod, current($this->value));
72   }
73
74   /**
75    * Moves to the next element (as required by the Iterator interface).
76    */
77   public function next()
78   {
79     next($this->value);
80
81     $this->count--;
82   }
83
84   /**
85    * Returns true if the current element is valid (as required by the Iterator interface).
86    *
87    * The current element will not be valid if {@link next()} has fallen off the
88    * end of the array or if there are no elements in the array and {@link
89    * rewind()} was called.
90    *
91    * @return bool The validity of the current element; true if it is valid
92    */
93   public function valid()
94   {
95     return $this->count > 0;
96   }
97
98   /**
99    * Returns true if the supplied offset isset in the array (as required by the ArrayAccess interface).
100    *
101    * @param  string $offset  The offset of the value to check existance of
102    *
103    * @return bool true if the offset isset; false otherwise
104    */
105   public function offsetExists($offset)
106   {
107     return isset($this->value[$offset]);
108   }
109
110   /**
111    * Returns the element associated with the offset supplied (as required by the ArrayAccess interface).
112    *
113    * @param  string $offset  The offset of the value to get
114    *
115    * @return mixed The escaped value
116    */
117   public function offsetGet($offset)
118   {
119     return sfOutputEscaper::escape($this->escapingMethod, $this->value[$offset]);
120   }
121
122   /**
123    * Throws an exception saying that values cannot be set (this method is
124    * required for the ArrayAccess interface).
125    *
126    * This (and the other sfOutputEscaper classes) are designed to be read only
127    * so this is an illegal operation.
128    *
129    * @param  string $offset  (ignored)
130    * @param  string $value   (ignored)
131    *
132    * @throws sfException
133    */
134   public function offsetSet($offset, $value)
135   {
136     throw new sfException('Cannot set values.');
137   }
138
139   /**
140    * Throws an exception saying that values cannot be unset (this method is
141    * required for the ArrayAccess interface).
142    *
143    * This (and the other sfOutputEscaper classes) are designed to be read only
144    * so this is an illegal operation.
145    *
146    * @param  string $offset  (ignored)
147    *
148    * @throws sfException
149    */
150   public function offsetUnset($offset)
151   {
152     throw new sfException('Cannot unset values.');
153   }
154
155   /**
156    * Returns the size of the array (are required by the Countable interface).
157    *
158    * @return int The size of the array
159    */
160   public function count()
161   {
162     return count($this->value);
163   }
164
165   /**
166    * Returns the (unescaped) value from the array associated with the key supplied.
167    *
168    * @param  string $key  The key into the array to use
169    *
170    * @return mixed The value
171    */
172   public function getRaw($key)
173   {
174     return $this->value[$key];
175   }
176 }
177
Note: See TracBrowser for help on using the browser.