diff --git a/conlite/classes/class.iterator.php b/conlite/classes/class.iterator.php index 25e1831..8aad55f 100644 --- a/conlite/classes/class.iterator.php +++ b/conlite/classes/class.iterator.php @@ -1,123 +1,86 @@ - * @license http://www.contenido.org/license/LIZENZ.txt - * @link http://www.4fb.de - * @link http://www.contenido.org - * @since file available since contenido release <= 4.6 - * - * {@internal - * created unknown - * modified 2008-06-30, Dominik Ziegler, add security fix - * - * $Id: class.iterator.php 2 2011-07-20 12:00:48Z oldperl $: - * }} - * - */ - -if(!defined('CON_FRAMEWORK')) { - die('Illegal call'); -} /** - * cIterator: A class which represents the C/C++/JAVA Iterator support. - * - * Iterating items is a mechanism to "step" trough a list of defined items. - * Basically, the iterator is similar to an array, but provides easy functions - * to step trough the list. - * - * An instance of an iterator is usually created by a class returning multiple - * items and automatically filled using the $aItems parameter of the constructor, - * and then returned to the caller. + * @package Core + * @subpackage Util + * @version $Rev$ + * @since 2.1 + * @author Ortwin Pinke + * @copyright (c) 2019, conlite.org + * @license http://www.gnu.de/documents/gpl.en.html GPL v3 (english version) + * @license http://www.gnu.de/documents/gpl.de.html GPL v3 (deutsche Version) + * @link http://www.conlite.org ConLite.org * - * The caller receives the iterator object and can step trough all items using - * the "next" method. - * - * @todo Add more stepping methods, as well as retrieving items - * + * $Id:$ */ -class cIterator -{ +// security check +defined('CON_FRAMEWORK') or die('Illegal call'); + +class cIterator { + /** * Holds the items which should be iterated * @var array - */ - var $_aIteratorItems; - - /** - * Iterator constructor - * - * This function initializes the constructor, adds the passed items - * and moves the iterator to the first element. - * - * @param $aItems array Items to add - * @return none - */ - function __construct ($aItems) - { - if (is_array($aItems)) - { - $this->_aIteratorItems = $aItems; - } else { - $this->_aIteratorItems = array(); - } - - $this->reset(); - } + */ + var $_aIteratorItems; - /** + /** + * Holds the keys of the array which should be iterated + * + * @var array + */ + protected $_aKeys; + + /** + * + * @param array $aItems items to add + */ + public function __construct($aItems) { + if (is_array($aItems)) { + $this->_aIteratorItems = $aItems; + } else { + $this->_aIteratorItems = array(); + } + + $this->reset(); + } + + /** * reset: Resets the iterator to the first element * * This function moves the iterator to the first element - * + * * @return none - */ - function reset () - { - reset($this->_aIteratorItems); - } + */ + function reset() { + $this->_aKeys = array_keys($this->_aIteratorItems); + } - /** + /** * next: Returns the next item in the iterator * * This function returns the item, or false if no * items are left. - * + * * @return mixed item or false if nothing was found - */ - function next () - { - $item = each($this->_aIteratorItems); + */ + function next() { + $key = array_shift($this->_aKeys); + return isset($this->_aIteratorItems[$key]) ? $this->_aIteratorItems[$key] : false; + } - if ($item === false) - { - return false; - } else { - return $item["value"]; - } - } - - /** + /** * count: Returns the number of items in the iterator - * + * * @return int Number of items - */ - function count () - { - return count($this->_aIteratorItems); - } + */ + function count() { + if(is_countable($this->_aIteratorItems)) { + return count($this->_aIteratorItems); + } + return 0; + } + } + ?> \ No newline at end of file