1 | <?php |
||
11 | class ResultSet implements Iterator, Countable |
||
12 | { |
||
13 | /** |
||
14 | * |
||
15 | * @var \Zend\Db\ResultSet\ResultSet |
||
16 | */ |
||
17 | protected $dataSource; |
||
18 | |||
19 | /** |
||
20 | * @var Table |
||
21 | */ |
||
22 | protected $table; |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * @var Select |
||
27 | */ |
||
28 | protected $select; |
||
29 | |||
30 | /** |
||
31 | * @var null|int |
||
32 | */ |
||
33 | protected $count = null; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $position = 0; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * |
||
43 | * @var boolean |
||
44 | */ |
||
45 | protected $has_complete_record_definition; |
||
46 | |||
47 | /** |
||
48 | * |
||
49 | * @param Select $select Originating select object |
||
50 | * @param Table $table Originating table |
||
51 | * @param boolean $has_complete_record_definition |
||
52 | */ |
||
53 | 14 | public function __construct(Select $select, Table $table, $has_complete_record_definition = false) |
|
54 | { |
||
55 | 14 | $this->select = $select; |
|
56 | 14 | $this->table = $table; |
|
57 | 14 | $this->has_complete_record_definition = $has_complete_record_definition; |
|
58 | 14 | $this->dataSource = $select->execute(); |
|
59 | 14 | } |
|
60 | |||
61 | /** |
||
62 | * This method allows the results to be iterable multiple times |
||
63 | * for database drivers that does not support rewind() method. |
||
64 | * PDO_Mysql for example does not provide backward scrolling resultset, |
||
65 | * They are forward only. MySQLi provides backward scrolling so this method |
||
66 | * should not be used. |
||
67 | * |
||
68 | * @throws Zend\Db\ResultSet\Exception\RuntimeException Buffering must be enabled before iteration is started |
||
69 | * |
||
70 | * @return ResultSet |
||
71 | */ |
||
72 | 1 | public function buffer() |
|
73 | { |
||
74 | 1 | $this->dataSource->buffer(); |
|
75 | 1 | return $this; |
|
76 | } |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Return an array version of the resultset |
||
81 | * @return array |
||
82 | */ |
||
83 | 5 | public function toArray() |
|
87 | |||
88 | /** |
||
89 | * Return an json version of the resultset |
||
90 | * @return string Json encoded version |
||
91 | */ |
||
92 | 1 | public function toJson() |
|
96 | |||
97 | |||
98 | /** |
||
99 | * Iterator: move pointer to next item |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | 3 | public function next() |
|
104 | { |
||
105 | 3 | $this->dataSource->next(); |
|
106 | 3 | $this->position++; |
|
107 | 3 | } |
|
108 | |||
109 | /** |
||
110 | * Iterator: retrieve current key |
||
111 | * |
||
112 | * @return mixed |
||
113 | */ |
||
114 | 1 | public function key() |
|
118 | |||
119 | /** |
||
120 | * Iterator: get current item |
||
121 | * |
||
122 | * @throws Exception\LogicException whenever a record cannot be instanciated, due to missing column specs |
||
123 | * @return Record |
||
124 | */ |
||
125 | 10 | public function current() |
|
126 | { |
||
127 | 10 | $data = $this->dataSource->current(); |
|
128 | |||
129 | 10 | if (!$this->has_complete_record_definition) { |
|
130 | 7 | $data_columns = array_keys($this->table->getColumnsInformation()); |
|
131 | 7 | $record_columns = array_keys((array) $data); |
|
132 | 7 | $matches = array_intersect($data_columns, $record_columns); |
|
133 | 7 | if (count($matches) != count($data_columns)) { |
|
134 | 5 | $missings = implode(',', array_diff($data_columns, $record_columns)); |
|
135 | 5 | $msg = __METHOD__ . ": Cannot create a Record due to incomplete or aliased column definition (missing: $missings)."; |
|
136 | 5 | $msg .= "Check whether columns have been modified in TableSearch::columns() method, or use an toArray(), toJson()... version of the ResultSet."; |
|
137 | 5 | throw new Exception\LogicException($msg); |
|
138 | } |
||
139 | 2 | $this->has_complete_record_definition = true; |
|
140 | 2 | } |
|
141 | |||
142 | 5 | $record = $this->table->record($data, $ignore = true); |
|
143 | 5 | $record->setState(Record::STATE_CLEAN); |
|
144 | 5 | return $record; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Iterator: is pointer valid? |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | 4 | public function valid() |
|
156 | |||
157 | /** |
||
158 | * Iterator: rewind |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | 4 | public function rewind() |
|
166 | |||
167 | /** |
||
168 | * Countable: return count of rows |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | 3 | public function count() |
|
179 | } |
||
180 |