Development

/branches/1.2/lib/view/escaper/sfOutputEscaperIteratorDecorator.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/view/escaper/sfOutputEscaperIteratorDecorator.class.php

Revision 23436, 4.3 kB (checked in by fabien, 5 years ago)

[1.2, 1.3] fixed count() on escaped objects that implements the Countable interface

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