Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
iterator.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Database
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Joomla Platform Database Driver Class
14  *
15  * @package Joomla.Platform
16  * @subpackage Database
17  * @since 12.1
18  */
19 abstract class JDatabaseIterator implements Countable, Iterator
20 {
21  /**
22  * The database cursor.
23  *
24  * @var mixed
25  * @since 12.1
26  */
27  protected $cursor;
28 
29  /**
30  * The class of object to create.
31  *
32  * @var string
33  * @since 12.1
34  */
35  protected $class;
36 
37  /**
38  * The name of the column to use for the key of the database record.
39  *
40  * @var mixed
41  * @since 12.1
42  */
43  private $_column;
44 
45  /**
46  * The current database record.
47  *
48  * @var mixed
49  * @since 12.1
50  */
51  private $_current;
52 
53  /**
54  * A numeric or string key for the current database record.
55  *
56  * @var scalar
57  * @since 12.1
58  */
59  private $_key;
60 
61  /**
62  * The number of fetched records.
63  *
64  * @var integer
65  * @since 12.1
66  */
67  private $_fetched = 0;
68 
69  /**
70  * Database iterator constructor.
71  *
72  * @param mixed $cursor The database cursor.
73  * @param string $column An option column to use as the iterator key.
74  * @param string $class The class of object that is returned.
75  *
76  * @throws InvalidArgumentException
77  */
78  public function __construct($cursor, $column = null, $class = 'stdClass')
79  {
80  if (!class_exists($class))
81  {
82  throw new InvalidArgumentException(sprintf('new %s(*%s*, cursor)', get_class($this), gettype($class)));
83  }
84 
85  $this->cursor = $cursor;
86  $this->class = $class;
87  $this->_column = $column;
88  $this->_fetched = 0;
89  $this->next();
90  }
91 
92  /**
93  * Database iterator destructor.
94  *
95  * @since 12.1
96  */
97  public function __destruct()
98  {
99  if ($this->cursor)
100  {
101  $this->freeResult($this->cursor);
102  }
103  }
104 
105  /**
106  * The current element in the iterator.
107  *
108  * @return object
109  *
110  * @see Iterator::current()
111  * @since 12.1
112  */
113  public function current()
114  {
115  return $this->_current;
116  }
117 
118  /**
119  * The key of the current element in the iterator.
120  *
121  * @return scalar
122  *
123  * @see Iterator::key()
124  * @since 12.1
125  */
126  public function key()
127  {
128  return $this->_key;
129  }
130 
131  /**
132  * Moves forward to the next result from the SQL query.
133  *
134  * @return void
135  *
136  * @see Iterator::next()
137  * @since 12.1
138  */
139  public function next()
140  {
141  // Set the default key as being the number of fetched object
142  $this->_key = $this->_fetched;
143 
144  // Try to get an object
145  $this->_current = $this->fetchObject();
146 
147  // If an object has been found
148  if ($this->_current)
149  {
150  // Set the key as being the indexed column (if it exists)
151  if (isset($this->_current->{$this->_column}))
152  {
153  $this->_key = $this->_current->{$this->_column};
154  }
155 
156  // Update the number of fetched object
157  $this->_fetched++;
158  }
159  }
160 
161  /**
162  * Rewinds the iterator.
163  *
164  * This iterator cannot be rewound.
165  *
166  * @return void
167  *
168  * @see Iterator::rewind()
169  * @since 12.1
170  */
171  public function rewind()
172  {
173  }
174 
175  /**
176  * Checks if the current position of the iterator is valid.
177  *
178  * @return boolean
179  *
180  * @see Iterator::valid()
181  * @since 12.1
182  */
183  public function valid()
184  {
185  return (boolean) $this->_current;
186  }
187 
188  /**
189  * Method to fetch a row from the result set cursor as an object.
190  *
191  * @return mixed Either the next row from the result set or false if there are no more rows.
192  *
193  * @since 12.1
194  */
195  abstract protected function fetchObject();
196 
197  /**
198  * Method to free up the memory used for the result set.
199  *
200  * @return void
201  *
202  * @since 12.1
203  */
204  abstract protected function freeResult();
205 }