1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\DBAL\Cache; |
---|
21 | |
---|
22 | use Doctrine\DBAL\Driver\ResultStatement; |
---|
23 | use PDO; |
---|
24 | |
---|
25 | class ArrayStatement implements \IteratorAggregate, ResultStatement |
---|
26 | { |
---|
27 | private $data; |
---|
28 | private $columnCount = 0; |
---|
29 | private $num = 0; |
---|
30 | private $defaultFetchStyle = PDO::FETCH_BOTH; |
---|
31 | |
---|
32 | public function __construct(array $data) |
---|
33 | { |
---|
34 | $this->data = $data; |
---|
35 | if (count($data)) { |
---|
36 | $this->columnCount = count($data[0]); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | public function closeCursor() |
---|
41 | { |
---|
42 | unset ($this->data); |
---|
43 | } |
---|
44 | |
---|
45 | public function columnCount() |
---|
46 | { |
---|
47 | return $this->columnCount; |
---|
48 | } |
---|
49 | |
---|
50 | public function setFetchMode($fetchStyle) |
---|
51 | { |
---|
52 | $this->defaultFetchStyle = $fetchStyle; |
---|
53 | } |
---|
54 | |
---|
55 | public function getIterator() |
---|
56 | { |
---|
57 | $data = $this->fetchAll($this->defaultFetchStyle); |
---|
58 | return new \ArrayIterator($data); |
---|
59 | } |
---|
60 | |
---|
61 | public function fetch($fetchStyle = PDO::FETCH_BOTH) |
---|
62 | { |
---|
63 | if (isset($this->data[$this->num])) { |
---|
64 | $row = $this->data[$this->num++]; |
---|
65 | if ($fetchStyle === PDO::FETCH_ASSOC) { |
---|
66 | return $row; |
---|
67 | } else if ($fetchStyle === PDO::FETCH_NUM) { |
---|
68 | return array_values($row); |
---|
69 | } else if ($fetchStyle === PDO::FETCH_BOTH) { |
---|
70 | return array_merge($row, array_values($row)); |
---|
71 | } |
---|
72 | } |
---|
73 | return false; |
---|
74 | } |
---|
75 | |
---|
76 | public function fetchAll($fetchStyle = PDO::FETCH_BOTH) |
---|
77 | { |
---|
78 | $rows = array(); |
---|
79 | while ($row = $this->fetch($fetchStyle)) { |
---|
80 | $rows[] = $row; |
---|
81 | } |
---|
82 | return $rows; |
---|
83 | } |
---|
84 | |
---|
85 | public function fetchColumn($columnIndex = 0) |
---|
86 | { |
---|
87 | $row = $this->fetch(PDO::FETCH_NUM); |
---|
88 | if (!isset($row[$columnIndex])) { |
---|
89 | // TODO: verify this is correct behavior |
---|
90 | return false; |
---|
91 | } |
---|
92 | return $row[$columnIndex]; |
---|
93 | } |
---|
94 | } |
---|