ArrayStatement.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  25. * @var array
  26. */
  27. private $data;
  28. /**
  29. * @var integer
  30. */
  31. private $columnCount = 0;
  32. /**
  33. * @var integer
  34. */
  35. private $num = 0;
  36. /**
  37. * @var integer
  38. */
  39. private $defaultFetchMode = PDO::FETCH_BOTH;
  40. /**
  41. * @param array $data
  42. */
  43. public function __construct(array $data)
  44. {
  45. $this->data = $data;
  46. if (count($data)) {
  47. $this->columnCount = count($data[0]);
  48. }
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function closeCursor()
  54. {
  55. unset ($this->data);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function columnCount()
  61. {
  62. return $this->columnCount;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  68. {
  69. if ($arg2 !== null || $arg3 !== null) {
  70. throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
  71. }
  72. $this->defaultFetchMode = $fetchMode;
  73. return true;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getIterator()
  79. {
  80. $data = $this->fetchAll();
  81. return new \ArrayIterator($data);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function fetch($fetchMode = null)
  87. {
  88. if (isset($this->data[$this->num])) {
  89. $row = $this->data[$this->num++];
  90. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  91. if ($fetchMode === PDO::FETCH_ASSOC) {
  92. return $row;
  93. } else if ($fetchMode === PDO::FETCH_NUM) {
  94. return array_values($row);
  95. } else if ($fetchMode === PDO::FETCH_BOTH) {
  96. return array_merge($row, array_values($row));
  97. } else if ($fetchMode === PDO::FETCH_COLUMN) {
  98. return reset($row);
  99. } else {
  100. throw new \InvalidArgumentException("Invalid fetch-style given for fetching result.");
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function fetchAll($fetchMode = null)
  109. {
  110. $rows = array();
  111. while ($row = $this->fetch($fetchMode)) {
  112. $rows[] = $row;
  113. }
  114. return $rows;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function fetchColumn($columnIndex = 0)
  120. {
  121. $row = $this->fetch(PDO::FETCH_NUM);
  122. if (!isset($row[$columnIndex])) {
  123. // TODO: verify this is correct behavior
  124. return false;
  125. }
  126. return $row[$columnIndex];
  127. }
  128. }