web/lib/Zend/Paginator/SerializableLimitIterator.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Paginator
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: SerializableLimitIterator.php 23189 2010-10-20 18:55:32Z mabe $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @category   Zend
       
    24  * @package    Zend_Paginator
       
    25  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    26  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    27  */
       
    28 class Zend_Paginator_SerializableLimitIterator extends LimitIterator implements Serializable, ArrayAccess
       
    29 {
       
    30 
       
    31     /**
       
    32      * Offset to first element
       
    33      *
       
    34      * @var int
       
    35      */
       
    36     private $_offset;
       
    37 
       
    38     /**
       
    39      * Maximum number of elements to show or -1 for all
       
    40      *
       
    41      * @var int
       
    42      */
       
    43     private $_count;
       
    44 
       
    45     /**
       
    46      * Construct a Zend_Paginator_SerializableLimitIterator
       
    47      *
       
    48      * @param Iterator $it Iterator to limit (must be serializable by un-/serialize)
       
    49      * @param int $offset Offset to first element
       
    50      * @param int $count Maximum number of elements to show or -1 for all
       
    51      * @see LimitIterator::__construct
       
    52      */
       
    53     public function __construct (Iterator $it, $offset=0, $count=-1)
       
    54     {
       
    55         parent::__construct($it, $offset, $count);
       
    56         $this->_offset = $offset;
       
    57         $this->_count = $count;
       
    58     }
       
    59 
       
    60     /**
       
    61      * @return string representation of the instance
       
    62      */
       
    63     public function serialize()
       
    64     {
       
    65         return serialize(array(
       
    66             'it'     => $this->getInnerIterator(),
       
    67             'offset' => $this->_offset,
       
    68             'count'  => $this->_count,
       
    69             'pos'    => $this->getPosition(),
       
    70         ));
       
    71     }
       
    72 
       
    73     /**
       
    74      * @param string $data representation of the instance
       
    75      */
       
    76     public function unserialize($data)
       
    77     {
       
    78         $dataArr = unserialize($data);
       
    79         $this->__construct($dataArr['it'], $dataArr['offset'], $dataArr['count']);
       
    80         $this->seek($dataArr['pos']+$dataArr['offset']);
       
    81     }
       
    82 
       
    83     /**
       
    84      * Returns value of the Iterator
       
    85      *
       
    86      * @param int $offset
       
    87      * @return mixed
       
    88      */
       
    89     public function offsetGet($offset)
       
    90     {
       
    91         $currentOffset = $this->key();
       
    92         $this->seek($offset);
       
    93         $current = $this->current();
       
    94         $this->seek($currentOffset);
       
    95         return $current;
       
    96     }
       
    97 
       
    98     /**
       
    99      * Does nothing
       
   100      * Required by the ArrayAccess implementation
       
   101      *
       
   102      * @param int $offset
       
   103      * @param mixed $value
       
   104      */
       
   105     public function offsetSet($offset, $value)
       
   106     {
       
   107     }
       
   108 
       
   109     /**
       
   110      * Determine if a value of Iterator is set and is not NULL
       
   111      *
       
   112      * @param int $offset
       
   113      */
       
   114     public function offsetExists($offset)
       
   115     {
       
   116         if ($offset > 0 && $offset < $this->_count) {
       
   117             try {
       
   118                 $currentOffset = $this->key();
       
   119                 $this->seek($offset);
       
   120                 $current = $this->current();
       
   121                 $this->seek($currentOffset);
       
   122                 return null !== $current;
       
   123             } catch (OutOfBoundsException $e) {
       
   124                 // reset position in case of exception is assigned null
       
   125                 $this->rewind();
       
   126                 $this->seek($currentOffset);
       
   127                 return false;
       
   128             }
       
   129         }
       
   130         return false;
       
   131     }
       
   132 
       
   133     /**
       
   134      * Does nothing
       
   135      * Required by the ArrayAccess implementation
       
   136      *
       
   137      * @param int $offset
       
   138      */
       
   139     public function offsetUnset($offset)
       
   140     {
       
   141     }
       
   142 }