1 <?php
2 /**
3 * Neevo - Tiny open-source database abstraction layer for PHP
4 *
5 * Copyright 2010-2011 Martin Srank (http://smasty.net)
6 *
7 * This source file is subject to the MIT license that is bundled
8 * with this package in the file license.txt.
9 *
10 * @author Martin Srank (http://smasty.net)
11 * @license http://neevo.smasty.net/license MIT license
12 * @link http://neevo.smasty.net/
13 *
14 */
15
16
17 /**
18 * Result set iterator.
19 * @author Martin Srank
20 * @package Neevo
21 */
22 class NeevoResultIterator implements Iterator, Countable, SeekableIterator {
23
24
25 /** @var int */
26 private $pointer;
27
28 /** @var NeevoResult */
29 private $result;
30
31 /** @var NeevoRow */
32 private $row;
33
34
35 public function __construct(NeevoResult $result){
36 $this->result = $result;
37 }
38
39
40 /**
41 * Rewind the iterator.
42 * Force execution for future iterations.
43 * @return void
44 */
45 public function rewind(){
46 if($this->row !== null)
47 $this->result = clone $this->result;
48 $this->pointer = 0;
49 }
50
51
52 /**
53 * Move to next row.
54 * @return void
55 */
56 public function next(){
57 ++$this->pointer;
58 }
59
60
61 /**
62 * Check for valid current row.
63 * @return bool
64 */
65 public function valid(){
66 return ($this->row = $this->result->fetch()) !== false;
67 }
68
69
70 /**
71 * Return the current row.
72 * @return NeevoRow
73 */
74 public function current(){
75 return $this->row;
76 }
77
78
79 /**
80 * Return the key of current row.
81 * @return int
82 */
83 public function key(){
84 return $this->pointer;
85 }
86
87
88 /**
89 * Implementation of Countable.
90 * @return int
91 * @throws NeevoDriverException on unbuffered result.
92 */
93 public function count(){
94 return $this->result->count();
95 }
96
97
98 /**
99 * Implementation of SeekableIterator.
100 * @param int $offset
101 * @throws OutOfRangeException|NeevoDriverException
102 */
103 public function seek($offset){
104 try{
105 $this->result->seek($offset);
106 } catch(NeevoDriverException $e){
107 throw $e;
108 } catch(NeevoException $e){
109 throw new OutOfRangeException("Cannot seek to offset $offset.", null, $e);
110 }
111 $this->pointer = $offset;
112 }
113
114
115 }
116