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 MIT license. For more information, see 17 * <http://www.doctrine-project.org>. 18 */ 19 20namespace Doctrine\DBAL\Cache; 21 22use Doctrine\DBAL\Driver\ResultStatement; 23use PDO; 24 25class ArrayStatement implements \IteratorAggregate, ResultStatement 26{ 27 private $data; 28 private $columnCount = 0; 29 private $num = 0; 30 private $defaultFetchMode = 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($fetchMode, $arg2 = null, $arg3 = null) 51 { 52 if ($arg2 !== null || $arg3 !== null) { 53 throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()"); 54 } 55 56 $this->defaultFetchMode = $fetchMode; 57 } 58 59 public function getIterator() 60 { 61 $data = $this->fetchAll(); 62 return new \ArrayIterator($data); 63 } 64 65 public function fetch($fetchMode = null) 66 { 67 if (isset($this->data[$this->num])) { 68 $row = $this->data[$this->num++]; 69 $fetchMode = $fetchMode ?: $this->defaultFetchMode; 70 if ($fetchMode === PDO::FETCH_ASSOC) { 71 return $row; 72 } else if ($fetchMode === PDO::FETCH_NUM) { 73 return array_values($row); 74 } else if ($fetchMode === PDO::FETCH_BOTH) { 75 return array_merge($row, array_values($row)); 76 } else if ($fetchMode === PDO::FETCH_COLUMN) { 77 return reset($row); 78 } else { 79 throw new \InvalidArgumentException("Invalid fetch-style given for fetching result."); 80 } 81 } 82 return false; 83 } 84 85 public function fetchAll($fetchMode = null) 86 { 87 $rows = array(); 88 while ($row = $this->fetch($fetchMode)) { 89 $rows[] = $row; 90 } 91 return $rows; 92 } 93 94 public function fetchColumn($columnIndex = 0) 95 { 96 $row = $this->fetch(PDO::FETCH_NUM); 97 if (!isset($row[$columnIndex])) { 98 // TODO: verify this is correct behavior 99 return false; 100 } 101 return $row[$columnIndex]; 102 } 103} 104