- md5
- 7b63259bccb75c89b1452ee164beaf2b
- sha1
- 70a6791cdce3c7e84cf57435b51e8496a2effade
- sha256
- 12c4c864daf8603daeaf64afd553a39b6d3232024d6a265a4105969b9f85d836
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
<?php
/**
* @package FrameworkOnFramework
* @subpackage database
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning FOFTable objects
* instead of plain stdClass objects
*/
// Protect from unauthorized access
defined('FOF_INCLUDED') or die;
/**
* Database iterator
*/
abstract class FOFDatabaseIterator implements Iterator
{
/**
* The database cursor.
*
* @var mixed
*/
protected $cursor;
/**
* The class of object to create.
*
* @var string
*/
protected $class;
/**
* The name of the column to use for the key of the database record.
*
* @var mixed
*/
private $_column;
/**
* The current database record.
*
* @var mixed
*/
private $_current;
/**
* The current database record as a FOFTable object.
*
* @var FOFTable
*/
private $_currentTable;
/**
* A numeric or string key for the current database record.
*
* @var scalar
*/
private $_key;
/**
* The number of fetched records.
*
* @var integer
*/
private $_fetched = 0;
/**
* A FOFTable object created using the class type $class, used by getTable
*
* @var FOFTable
*/
private $_tableObject = null;
/**
* Returns an iterator object for a specific database type
*
* @param string $dbName The database type, e.g. mysql, mysqli, sqlazure etc.
* @param mixed $cursor The database cursor
* @param string $column An option column to use as the iterator key
* @param string $class The table class of the returned objects
* @param array $config Configuration parameters to push to the table class
*
* @return FOFDatabaseIterator
*
* @throws InvalidArgumentException
*/
public static function &getIterator($dbName, $cursor, $column = null, $class, $config = array())
{
$className = 'FOFDatabaseIterator' . ucfirst($dbName);
$object = new $className($cursor, $column, $class, $config);
return $object;
}
/**
* Database iterator constructor.
*
* @param mixed $cursor The database cursor.
* @param string $column An option column to use as the iterator key.
* @param string $class The table class of the returned objects.
* @param array $config Configuration parameters to push to the table class
*
* @throws InvalidArgumentException
*/
public function __construct($cursor, $column = null, $class, $config = array())
{
// Figure out the type and prefix of the class by the class name
$parts = FOFInflector::explode($class);
if(count($parts) != 3)
{
throw new InvalidArgumentException('Invalid table name, expected a pattern like ComponentTableFoobar got '.$class);
}
$this->_tableObject = FOFTable::getInstance($parts[2], ucfirst($parts[0]) . ucfirst($parts[1]))->getClone();
$this->cursor = $cursor;
$this->class = 'stdClass';
$this->_column = $column;
$this->_fetched = 0;
$this->next();
}
/**
* Database iterator destructor.
*/
public function __destruct()
{
if ($this->cursor)
{
$this->freeResult($this->cursor);
}
}
/**
* The current element in the iterator.
*
* @return object
*
* @see Iterator::current()
*/
public function current()
{
return $this->_currentTable;
}
/**
* The key of the current element in the iterator.
*
* @return scalar
*
* @see Iterator::key()
*/
public function key()
{
return $this->_key;
}
/**
* Moves forward to the next result from the SQL query.
*
* @return void
*
* @see Iterator::next()
*/
public function next()
{
// Set the default key as being the number of fetched object
$this->_key = $this->_fetched;
// Try to get an object
$this->_current = $this->fetchObject();
// If an object has been found
if ($this->_current)
{
$this->_currentTable = $this->getTable();
// Set the key as being the indexed column (if it exists)
if (isset($this->_current->{$this->_column}))
{
$this->_key = $this->_current->{$this->_column};
}
// Update the number of fetched object
$this->_fetched++;
}
}
/**
* Rewinds the iterator.
*
* This iterator cannot be rewound.
*
* @return void
*
* @see Iterator::rewind()
*/
public function rewind()
{
}
/**
* Checks if the current position of the iterator is valid.
*
* @return boolean
*
* @see Iterator::valid()
*/
public function valid()
{
return (boolean) $this->_current;
}
/**
* Method to fetch a row from the result set cursor as an object.
*
* @return mixed Either the next row from the result set or false if there are no more rows.
*/
abstract protected function fetchObject();
/**
* Method to free up the memory used for the result set.
*
* @return void
*/
abstract protected function freeResult();
/**
* Returns the data in $this->_current as a FOFTable instance
*
* @return FOFTable
*
* @throws OutOfBoundsException
*/
protected function getTable()
{
if (!$this->valid())
{
throw new OutOfBoundsException('Cannot get item past iterator\'s bounds', 500);
}
$this->_tableObject->bind($this->_current);
return $this->_tableObject;
}
}
The same content is in 114 releases, Joomla 3.6.3-rc1 to 3.10.0-alpha3. See them all, or every version of this file.