/usr/share/php/Horde/Rdo/List.php is in php-horde-rdo 2.0.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | <?php
/**
* @category Horde
* @package Rdo
*/
/**
* Iterator for collections of Rdo objects.
*
* @TODO implement ArrayAccess as well?
*
* @category Horde
* @package Rdo
*/
class Horde_Rdo_List implements ArrayAccess, Iterator, Countable
{
/**
* Rdo Query
* @var mixed
*/
protected $_query;
/**
* Rdo Mapper
* @var Horde_Rdo_Mapper
*/
protected $_mapper;
/**
* SQL query to run
* @var string
*/
protected $_sql;
/**
* Bind parameters
* @var array
*/
protected $_bindParams = array();
/**
* Result resource
* @var Iterator
*/
protected $_result;
/**
* Current object
* @var Horde_Rdo_Base
*/
protected $_current;
/**
* Current list offset.
* @var integer
*/
protected $_index;
/**
* Are we at the end of the list?
* @var boolean
*/
protected $_eof;
/**
* The number of objects in the list.
* @var integer
*/
protected $_count;
/**
* Constructor.
*
* @param mixed $query The query to run when results are
* requested. Can be a Horde_Rdo_Query object, a literal SQL
* query, or a tuple containing an SQL string and an array of bind
* parameters to use.
* @param Horde_Rdo_Mapper $mapper Mapper to create objects for this list from.
*/
public function __construct($query, $mapper = null)
{
if ($query instanceof Horde_Rdo_Query) {
// Make sure we have a Mapper object, which can be passed
// implicitly by being set on the Query.
if (!$mapper) {
if (!$query->mapper) {
throw new Horde_Rdo_Exception('Mapper must be set on the Query object or explicitly passed.');
}
$mapper = $query->mapper;
}
// Convert the query into a SQL statement and an array of
// bind parameters.
list($this->_sql, $this->_bindParams) = $query->getQuery();
} elseif (is_string($query)) {
// Straight SQL query, empty bind parameters array.
$this->_sql = $query;
$this->_bindParams = array();
} else {
// $query is already an array with SQL and bind parameters.
list($this->_sql, $this->_bindParams) = $query;
}
if (!$mapper) {
throw new Horde_Rdo_Exception('Mapper must be provided either explicitly or in a Query object');
}
$this->_query = $query;
$this->_mapper = $mapper;
}
/**
* Destructor - release any resources.
*/
public function __destruct()
{
if ($this->_result) {
unset($this->_result);
}
}
/**
* Implementation of the rewind() method for iterator.
*/
public function rewind()
{
if ($this->_result) {
unset($this->_result);
}
$this->_current = null;
$this->_index = null;
$this->_eof = true;
$this->_result = $this->_mapper->adapter->select($this->_sql, $this->_bindParams);
$this->next();
}
/**
* Implementation of the current() method for iterator.
*
* @return mixed The current row, or null if no rows.
*/
public function current()
{
if (is_null($this->_result)) {
$this->rewind();
}
return $this->_current;
}
/**
* Implementation of the key() method for iterator.
*
* @return mixed The current row number (starts at 0), or NULL if no rows
*/
public function key()
{
if (is_null($this->_result)) {
$this->rewind();
}
return $this->_index;
}
/**
* Implementation of the next() method.
*
* @return Horde_Rdo_Base|null The next Rdo object in the set or
* null if no more results.
*/
public function next()
{
if (is_null($this->_result)) {
$this->rewind();
}
if ($this->_result) {
$row = $this->_result->fetch();
if (!$row) {
$this->_eof = true;
} else {
$this->_eof = false;
if (is_null($this->_index)) {
$this->_index = 0;
} else {
++$this->_index;
}
$this->_current = $this->_mapper->map($row);
}
}
return $this->_current;
}
/**
* Implementation of the offsetExists() method for ArrayAccess
* This method is executed when using isset() or empty() on Horde_Rdo_List objects
* @param integer $offset The offset to check.
*
* @return boolean Whether or not an offset exists.
*/
public function offsetExists($offset)
{
$query = Horde_Rdo_Query::create($this->_query);
$query->limit(1, $offset);
return $this->_mapper->exists($query);
}
/**
* Implementation of the offsetGet() method for ArrayAccess
* This method is executed when using isset() or empty() on Horde_Rdo_List objects
* @param integer $offset The offset to retrieve.
*
* @return Horde_Rdo_Base An entity object at the offset position or null
*/
public function offsetGet($offset)
{
$query = Horde_Rdo_Query::create($this->_query);
$query->limit(1, $offset);
return $this->_mapper->find($query)->current();
}
/**
* Not implemented.
*
* Stub of the offsetSet() method for ArrayAccess
* This method is executed when adding an item to the Horde_Rdo_List
* @param Horde_Rdo_Base $item The item to add to the list.
* @param integer $offset The offset to add or change.
* @param Horde_Rdo_Base $offset The item to add to the list.
*
* @return Horde_Rdo_Base An entity object at the offset position or null
*/
public function offsetSet($offset, $item)
{
throw new Horde_Rdo_Exception('You cannot add objects to a result set');
}
/**
* Not implemented.
*
* Stub of the offsetUnset() method for ArrayAccess
* This method is executed when calling unset on a Horde_Rdo_List index
* @param Horde_Rdo_Base $item The item to add to the list.
* @param integer $offset The offset to unset.
*
* @return Horde_Rdo_Base An entity object at the offset position or null
*/
public function offsetUnset($offset)
{
throw new Horde_Rdo_Exception('You cannot remove objects from a result set');
}
/**
* Implementation of the valid() method for iterator
*
* @return boolean Whether the iteration is valid
*/
public function valid()
{
if (is_null($this->_result)) {
$this->rewind();
}
return !$this->_eof;
}
/**
* Implementation of count() for Countable
*
* @return integer Number of elements in the list
*/
public function count()
{
if (is_null($this->_count)) {
$this->_count = $this->_mapper->count($this->_query);
}
return $this->_count;
}
}
|