ArrayStatement.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. 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 $defaultFetchMode = 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($fetchMode, $arg2 = null, $arg3 = null)
  44. {
  45. if ($arg2 !== null || $arg3 !== null) {
  46. throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
  47. }
  48. $this->defaultFetchMode = $fetchMode;
  49. }
  50. public function getIterator()
  51. {
  52. $data = $this->fetchAll();
  53. return new \ArrayIterator($data);
  54. }
  55. public function fetch($fetchMode = null)
  56. {
  57. if (isset($this->data[$this->num])) {
  58. $row = $this->data[$this->num++];
  59. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  60. if ($fetchMode === PDO::FETCH_ASSOC) {
  61. return $row;
  62. } else if ($fetchMode === PDO::FETCH_NUM) {
  63. return array_values($row);
  64. } else if ($fetchMode === PDO::FETCH_BOTH) {
  65. return array_merge($row, array_values($row));
  66. } else if ($fetchMode === PDO::FETCH_COLUMN) {
  67. return reset($row);
  68. } else {
  69. throw new \InvalidArgumentException("Invalid fetch-style given for fetching result.");
  70. }
  71. }
  72. return false;
  73. }
  74. public function fetchAll($fetchMode = null)
  75. {
  76. $rows = array();
  77. while ($row = $this->fetch($fetchMode)) {
  78. $rows[] = $row;
  79. }
  80. return $rows;
  81. }
  82. public function fetchColumn($columnIndex = 0)
  83. {
  84. $row = $this->fetch(PDO::FETCH_NUM);
  85. if (!isset($row[$columnIndex])) {
  86. // TODO: verify this is correct behavior
  87. return false;
  88. }
  89. return $row[$columnIndex];
  90. }
  91. }