| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 | <?php/** * * Cube Framework $Id$ vOOg3PYitcU95OjDsns66VwbooLGASuhP70wdbJuvgE= * * @link        http://codecu.be/framework * @copyright   Copyright (c) 2015 CodeCube SRL * @license     http://codecu.be/framework/license Commercial License * * @version     1.4 *//** * abstract db table rowset class */namespace Cube\Db\Table\Rowset;use Cube\Db\Table\AbstractTable,    Cube\Controller\Front,    Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,    Cube\Translate;class AbstractRowset implements \SeekableIterator, \Countable, \ArrayAccess{    /**     *     * data for each row     *     * @var array     */    protected $_data = array();    /**     *     * table class the row belongs to     *     * @var \Cube\Db\Table\AbstractTable     */    protected $_table = null;    /**     *     * translate adapter     *     * @var \Cube\Translate\Adapter\AbstractAdapter     */    protected $_translate;    /**     *     * iterator pointer     *     * @var integer     */    protected $_pointer = 0;    /**     *     * number of rows in the rowset     *     * @var integer     */    protected $_count;    /**     * array of \Cube\Db\Table\Row\AbstractRow objects     *     * @var array     */    protected $_rows = array();    /**     *     * row object class     *     * @var string     */    protected $_rowClass = '\Cube\Db\Table\Row';    /**     *     * class constructor     */    public function __construct(array $data = array())    {        if (isset($data['table']) && $data['table'] instanceof AbstractTable) {            $this->_table = $data['table'];        }        else {            throw new \InvalidArgumentException("The 'table' key must be set when creating a Rowset object");        }        if (isset($data['data'])) {            if (!is_array($data['data'])) {                throw new \InvalidArgumentException('The "data" key must be an array');            }            $this->_data = $data['data'];            $this->_count = count($this->_data);        }        if (isset($data['rowClass'])) {            $this->_rowClass = $data['rowClass'];        }    }    /**     *     * get table object     *     * @return \Cube\Db\Table\AbstractTable     */    public function getTable()    {        return $this->_table;    }    /**     *     * set translate adapter     *     * @param \Cube\Translate\Adapter\AbstractAdapter $translate     *     * @return $this     */    public function setTranslate(TranslateAdapter $translate)    {        $this->_translate = $translate;        return $this;    }    /**     *     * get translate adapter     *     * @return \Cube\Translate\Adapter\AbstractAdapter     */    public function getTranslate()    {        if (!$this->_translate instanceof TranslateAdapter) {            $translate = Front::getInstance()->getBootstrap()->getResource('translate');            if ($translate instanceof Translate) {                $this->setTranslate(                    $translate->getAdapter());            }        }        return $this->_translate;    }    /**     * Returns a \Cube\Db\Table\Row\AbstractRow from a known position into the Iterator     *     * @param int  $position the position of the row expected     * @param bool $seek     whether or not seek the iterator to that position after     *     * @return \Cube\Db\Table\Row\AbstractRow|null     */    public function getRow($position, $seek = false)    {        try {            $row = $this->_loadAndReturnRow($position);        } catch (\Exception $e) {            return null;        }        if ($seek == true) {            $this->seek($position);        }        return $row;    }    /**     *     * proxy for the save() method for each row in the rowset     *     * @param array $data   partial data to be saved     *                      the complete row is saved if this parameter is null     *     * @return $this     */    public function save(array $data = null)    {        /** @var \Cube\Db\Table\Row\AbstractRow $row */        foreach ($this as $row) {            $row->save($data);        }        return $this;    }    /**     *     * delete all rows from the corresponding rowset individually     *     * @return $this     */    public function delete()    {        /** @var \Cube\Db\Table\Row\AbstractRow $row */        foreach ($this as $row) {            $row->delete();        }        return $this;    }    /**     *     * returns all data as an array.     *     * @return array     */    public function toArray()    {        /** @var \Cube\Db\Table\Row\AbstractRow $row */        foreach ($this->_rows as $i => $row) {            $this->_data[$i] = $row->toArray();        }        return $this->_data;    }    /*     * methods needed to implement the \SeekableIterator, \Countable and \ArrayAccess interfaces     */    /**     *     * check whether an offset exists     *     * @param mixed $offset     *     * @return bool     */    public function offsetExists($offset)    {        return isset($this->_data[(int)$offset]);    }    /**     *     * offset to retrieve     *     * @param mixed $offset     *     * @return mixed|null     * @throws \RuntimeException     */    public function offsetGet($offset)    {        $offset = (int)$offset;        if ($offset < 0 || $offset >= $this->_count) {            throw new \RuntimeException("Illegal index $offset");        }        $this->_pointer = $offset;        return $this->current();    }    /**     *     * offset to set     *     * @param mixed $offset     * @param mixed $value     */    public function offsetSet($offset, $value)    {        $this->_data[(int)$offset] = $value;    }    /**     *     * unset offset     *     * @param mixed $offset     */    public function offsetUnset($offset)    {        unset($this->_data[(int)$offset]);    }    /**     *     * count elements of an object     *     * @return int     */    public function count()    {        return $this->_count;    }    /**     *     * return the current element     *     * @return mixed|null     */    public function current()    {        if ($this->valid() === false) {            return null;        }        // return the row object        return $this->_loadAndReturnRow($this->_pointer);    }    /**     *     * return the key of the current element     *     * @return int|mixed     */    public function key()    {        return $this->_pointer;    }    public function next()    {        ++$this->_pointer;    }    /**     *     * rewind the Iterator to the first element     *     * @return $this|void     */    public function rewind()    {        $this->_pointer = 0;        return $this;    }    /**     *     * seek to a position     *     * @param int $position     *     * @return $this|void     * @throws \RuntimeException     */    public function seek($position)    {        $position = (int)$position;        if ($position < 0 || $position >= $this->_count) {            throw new \RuntimeException(                sprintf("Illegal index %s", $position));        }        $this->_pointer = $position;        return $this;    }    /**     *     * checks if current position is valid     *     * @return bool     */    public function valid()    {        return $this->_pointer >= 0 && $this->_pointer < $this->_count;    }    /**     *     * return the object row from a selected position     *     * @param $position     *     * @return mixed     * @throws \InvalidArgumentException     */    protected function _loadAndReturnRow($position)    {        if (!isset($this->_data[$position])) {            throw new \InvalidArgumentException("Data for provided position does not exist");        }        if (empty($this->_rows[$position])) {            $this->_rows[$position] = new $this->_rowClass(                array(                    'table' => $this->_table,                    'data'  => $this->_data[$position],                )            );        }        // return the row object        return $this->_rows[$position];    }}
 |