ArrayStatement.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Doctrine\DBAL\Cache;
  3. use ArrayIterator;
  4. use Doctrine\DBAL\Driver\ResultStatement;
  5. use Doctrine\DBAL\FetchMode;
  6. use InvalidArgumentException;
  7. use IteratorAggregate;
  8. use PDO;
  9. use function array_merge;
  10. use function array_values;
  11. use function count;
  12. use function reset;
  13. class ArrayStatement implements IteratorAggregate, ResultStatement
  14. {
  15. /** @var mixed[] */
  16. private $data;
  17. /** @var int */
  18. private $columnCount = 0;
  19. /** @var int */
  20. private $num = 0;
  21. /** @var int */
  22. private $defaultFetchMode = FetchMode::MIXED;
  23. /**
  24. * @param mixed[] $data
  25. */
  26. public function __construct(array $data)
  27. {
  28. $this->data = $data;
  29. if (! count($data)) {
  30. return;
  31. }
  32. $this->columnCount = count($data[0]);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function closeCursor()
  38. {
  39. unset($this->data);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function columnCount()
  45. {
  46. return $this->columnCount;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  52. {
  53. if ($arg2 !== null || $arg3 !== null) {
  54. throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
  55. }
  56. $this->defaultFetchMode = $fetchMode;
  57. return true;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getIterator()
  63. {
  64. $data = $this->fetchAll();
  65. return new ArrayIterator($data);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
  71. {
  72. if (! isset($this->data[$this->num])) {
  73. return false;
  74. }
  75. $row = $this->data[$this->num++];
  76. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  77. if ($fetchMode === FetchMode::ASSOCIATIVE) {
  78. return $row;
  79. }
  80. if ($fetchMode === FetchMode::NUMERIC) {
  81. return array_values($row);
  82. }
  83. if ($fetchMode === FetchMode::MIXED) {
  84. return array_merge($row, array_values($row));
  85. }
  86. if ($fetchMode === FetchMode::COLUMN) {
  87. return reset($row);
  88. }
  89. throw new InvalidArgumentException('Invalid fetch-style given for fetching result.');
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
  95. {
  96. $rows = [];
  97. while ($row = $this->fetch($fetchMode)) {
  98. $rows[] = $row;
  99. }
  100. return $rows;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function fetchColumn($columnIndex = 0)
  106. {
  107. $row = $this->fetch(FetchMode::NUMERIC);
  108. // TODO: verify that return false is the correct behavior
  109. return $row[$columnIndex] ?? false;
  110. }
  111. }