Source of file ArrayIterator.php

Size: 6,461 Bytes - Last Modified: 2016-05-18T03:08:27+02:00

buildproject/core/module_system/system/ArrayIterator.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
<?php
/*"******************************************************************************************************
*   (c) 2004-2006 by MulchProductions, www.mulchprod.de                                                 *
*   (c) 2007-2016 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
********************************************************************************************************/

namespace Kajona\System\System;

use Iterator;


/**
 * Class to iterator over an array.
 * This class is able to create a pageview-mechanism
 *
 * @package module_system
 * @author sidler@mulchprod.de
 */
class ArrayIterator implements IteratorInterface, Iterator
{

    protected $arrElements = array();
    protected $intArrCursor = 0;

    protected $intElementsPerPage = 15;

    /**
     * Constructor
     *
     * @param $arrElements
     *
     * @return \ArrayIterator
     */
    public function __construct($arrElements)
    {

        $objUser = new UserUser(Session::getInstance()->getUserID());

        $this->intElementsPerPage = $objUser->getIntItemsPerPage();
        $this->setArrElements($arrElements);
    }

    /**
     * Sets the ArrElements
     *
     * @param $arrElements
     */
    public function setArrElements($arrElements)
    {
        $this->arrElements = array();

        //Loop over elements to create numeric indices
        if (count($arrElements) > 0) {
            foreach ($arrElements as $objOneElement) {
                $this->arrElements[] = $objOneElement;
            }
        }
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Return the current element
     *
     * @link http://php.net/manual/en/iterator.current.php
     * @return mixed Can return any type.
     */
    public function current()
    {
        return $this->arrElements[$this->intArrCursor];
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Move forward to next element
     *
     * @link http://php.net/manual/en/iterator.next.php
     * @return void Any returned value is ignored.
     */
    public function next()
    {
        $this->intArrCursor++;
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Return the key of the current element
     *
     * @link http://php.net/manual/en/iterator.key.php
     * @return mixed scalar on success, or null on failure.
     */
    public function key()
    {
        return $this->intArrCursor;
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Checks if current position is valid
     *
     * @link http://php.net/manual/en/iterator.valid.php
     * @return boolean The return value will be casted to boolean and then evaluated.
     * Returns true on success or false on failure.
     */
    public function valid()
    {
        return $this->intArrCursor < count($this->arrElements);
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Rewind the Iterator to the first element
     *
     * @link http://php.net/manual/en/iterator.rewind.php
     * @return void Any returned value is ignored.
     */
    public function rewind()
    {
        $this->intArrCursor = 0;
    }


    /**
     * Returns the current element
     *
     * @return mixed
     */
    public function getCurrentElement()
    {
        return $this->current();
    }

    /**
     * Returns the next element, null if no further element available
     *
     * @return mixed
     */
    public function getNextElement()
    {
        if (!$this->isNext()) {
            return null;
        }

        return $this->arrElements[++$this->intArrCursor];
    }

    /**
     * Checks if theres one more element to return
     *
     * @return bool
     */
    public function isNext()
    {
        return ($this->intArrCursor < count($this->arrElements));
    }

    /**
     * Returns the first element of the colection,
     * rewinds the cursor
     *
     * @return mixed
     */
    public function getFirstElement()
    {
        $this->intArrCursor = 0;
        return $this->arrElements[$this->intArrCursor];
    }

    /**
     * Returns the number of elements
     *
     * @return int
     */
    public function getNumberOfElements()
    {
        return count($this->arrElements);
    }

    // --- PageViewStuff ------------------------------------------------------------------------------------

    /**
     * Set the number of elements per page
     *
     * @param int $intElements
     */
    public function setIntElementsPerPage($intElements)
    {
        if ((int)$intElements > 0) {
            $this->intElementsPerPage = (int)$intElements;
        }
        else {
            $this->intElementsPerPage = 100;
        }
    }

    /**
     * @return int|string
     */
    public function getIntElementsPerPage()
    {
        return $this->intElementsPerPage;
    }


    /**
     * Set the cursor to a defined position
     *
     * @param int $intElement
     *
     * @return bool
     */
    public function setCursorPosition($intElement)
    {
        if ($this->getNumberOfElements() > $intElement) {
            $this->intArrCursor = $intElement;
            return true;
        }
        else {
            return false;
        }

    }

    /**
     * Returns the number of pages available
     *
     * @return int
     */
    public function getNrOfPages()
    {
        if ($this->intElementsPerPage == (int)0) {
            return 0;
        }

        return ceil($this->getNumberOfElements() / $this->intElementsPerPage);
    }

    /**
     * Returns the elements placed on the given page
     *
     * @param int $intPageNumber
     *
     * @return array
     */
    public function getElementsOnPage($intPageNumber)
    {
        if ((int)$intPageNumber <= 0) {
            $intPageNumber = 1;
        }

        $arrReturn = array();
        //calc elements to return
        $intStart = ($intPageNumber * $this->intElementsPerPage) - $this->intElementsPerPage;
        $intEnd = $this->intElementsPerPage + $intStart - 1;

        if ($intEnd > $this->getNumberOfElements()) {
            $intEnd = $this->getNumberOfElements() - 1;
        }

        for ($intI = $intStart; $intI <= $intEnd; $intI++) {
            if (!$this->setCursorPosition($intI)) {
                break;
            }
            $arrReturn[] = $this->arrElements[$this->intArrCursor];
        }
        return $arrReturn;
    }
}