Esempio n. 1
0
 /**
  * Set the data source for the result set
  *
  * @param  Iterator|IteratorAggregate|ResultInterface $dataSource
  * @return ResultSet
  * @throws Exception\InvalidArgumentException
  */
 public function initialize($dataSource)
 {
     if ($dataSource instanceof ResultInterface) {
         $this->count = $dataSource->count();
         $this->fieldCount = $dataSource->getFieldCount();
         $this->dataSource = $dataSource;
         return $this;
     }
     if (is_array($dataSource)) {
         // its safe to get numbers from an array
         $first = current($dataSource);
         reset($dataSource);
         $this->count = count($dataSource);
         $this->fieldCount = count($first);
         $this->dataSource = new ArrayIterator($dataSource);
     } elseif ($dataSource instanceof IteratorAggregate) {
         $this->dataSource = $dataSource->getIterator();
     } elseif ($dataSource instanceof Iterator) {
         $this->dataSource = $dataSource;
     } else {
         throw new Exception\InvalidArgumentException('DataSource provided is not an array, nor does it implement Iterator or IteratorAggregate');
     }
     if ($this->count == null && $this->dataSource instanceof Countable) {
         $this->count = $this->dataSource->count();
     }
     return $this;
 }
 /**
  * Set the data source for the result set
  *
  * @param  array|Iterator|IteratorAggregate|ResultInterface $dataSource
  * @return ResultSet
  * @throws Exception\InvalidArgumentException
  */
 public function initialize($dataSource)
 {
     // reset buffering
     if (is_array($this->buffer)) {
         $this->buffer = [];
     }
     if ($dataSource instanceof ResultInterface) {
         $this->fieldCount = $dataSource->getFieldCount();
         $this->dataSource = $dataSource;
         if ($dataSource->isBuffered()) {
             $this->buffer = -1;
         }
         if (is_array($this->buffer)) {
             $this->dataSource->rewind();
         }
         return $this;
     }
     if (is_array($dataSource)) {
         // its safe to get numbers from an array
         $first = current($dataSource);
         reset($dataSource);
         $this->fieldCount = count($first);
         $this->dataSource = new ArrayIterator($dataSource);
         $this->buffer = -1;
         // array's are a natural buffer
     } elseif ($dataSource instanceof IteratorAggregate) {
         $this->dataSource = $dataSource->getIterator();
     } elseif ($dataSource instanceof Iterator) {
         $this->dataSource = $dataSource;
     } else {
         throw new Exception\InvalidArgumentException('DataSource provided is not an array, nor does it implement Iterator or IteratorAggregate');
     }
     return $this;
 }