ArrayStatement.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. namespace Doctrine\DBAL\Cache;
  20. use Doctrine\DBAL\Driver\ResultStatement;
  21. use PDO;
  22. class ArrayStatement implements \IteratorAggregate, ResultStatement
  23. {
  24. private $data;
  25. private $columnCount = 0;
  26. private $num = 0;
  27. private $defaultFetchStyle = PDO::FETCH_BOTH;
  28. public function __construct(array $data)
  29. {
  30. $this->data = $data;
  31. if (count($data)) {
  32. $this->columnCount = count($data[0]);
  33. }
  34. }
  35. public function closeCursor()
  36. {
  37. unset ($this->data);
  38. }
  39. public function columnCount()
  40. {
  41. return $this->columnCount;
  42. }
  43. public function setFetchMode($fetchStyle)
  44. {
  45. $this->defaultFetchStyle = $fetchStyle;
  46. }
  47. public function getIterator()
  48. {
  49. $data = $this->fetchAll($this->defaultFetchStyle);
  50. return new \ArrayIterator($data);
  51. }
  52. public function fetch($fetchStyle = PDO::FETCH_BOTH)
  53. {
  54. if (isset($this->data[$this->num])) {
  55. $row = $this->data[$this->num++];
  56. if ($fetchStyle === PDO::FETCH_ASSOC) {
  57. return $row;
  58. } else if ($fetchStyle === PDO::FETCH_NUM) {
  59. return array_values($row);
  60. } else if ($fetchStyle === PDO::FETCH_BOTH) {
  61. return array_merge($row, array_values($row));
  62. }
  63. }
  64. return false;
  65. }
  66. public function fetchAll($fetchStyle = PDO::FETCH_BOTH)
  67. {
  68. $rows = array();
  69. while ($row = $this->fetch($fetchStyle)) {
  70. $rows[] = $row;
  71. }
  72. return $rows;
  73. }
  74. public function fetchColumn($columnIndex = 0)
  75. {
  76. $row = $this->fetch(PDO::FETCH_NUM);
  77. if (!isset($row[$columnIndex])) {
  78. // TODO: verify this is correct behavior
  79. return false;
  80. }
  81. return $row[$columnIndex];
  82. }
  83. }