ArrayStatement.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Doctrine\DBAL\Cache;
  3. use ArrayIterator;
  4. use Doctrine\DBAL\Driver\FetchUtils;
  5. use Doctrine\DBAL\Driver\Result;
  6. use Doctrine\DBAL\Driver\ResultStatement;
  7. use Doctrine\DBAL\FetchMode;
  8. use InvalidArgumentException;
  9. use IteratorAggregate;
  10. use PDO;
  11. use function array_merge;
  12. use function array_values;
  13. use function count;
  14. use function reset;
  15. /**
  16. * @deprecated
  17. */
  18. class ArrayStatement implements IteratorAggregate, ResultStatement, Result
  19. {
  20. /** @var mixed[] */
  21. private $data;
  22. /** @var int */
  23. private $columnCount = 0;
  24. /** @var int */
  25. private $num = 0;
  26. /** @var int */
  27. private $defaultFetchMode = FetchMode::MIXED;
  28. /**
  29. * @param mixed[] $data
  30. */
  31. public function __construct(array $data)
  32. {
  33. $this->data = $data;
  34. if (! count($data)) {
  35. return;
  36. }
  37. $this->columnCount = count($data[0]);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. *
  42. * @deprecated Use free() instead.
  43. */
  44. public function closeCursor()
  45. {
  46. $this->free();
  47. return true;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function rowCount()
  53. {
  54. return count($this->data);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function columnCount()
  60. {
  61. return $this->columnCount;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. *
  66. * @deprecated Use one of the fetch- or iterate-related methods.
  67. */
  68. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  69. {
  70. if ($arg2 !== null || $arg3 !== null) {
  71. throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
  72. }
  73. $this->defaultFetchMode = $fetchMode;
  74. return true;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. *
  79. * @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
  80. */
  81. public function getIterator()
  82. {
  83. $data = $this->fetchAll();
  84. return new ArrayIterator($data);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. *
  89. * @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
  90. */
  91. public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
  92. {
  93. if (! isset($this->data[$this->num])) {
  94. return false;
  95. }
  96. $row = $this->data[$this->num++];
  97. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  98. if ($fetchMode === FetchMode::ASSOCIATIVE) {
  99. return $row;
  100. }
  101. if ($fetchMode === FetchMode::NUMERIC) {
  102. return array_values($row);
  103. }
  104. if ($fetchMode === FetchMode::MIXED) {
  105. return array_merge($row, array_values($row));
  106. }
  107. if ($fetchMode === FetchMode::COLUMN) {
  108. return reset($row);
  109. }
  110. throw new InvalidArgumentException('Invalid fetch-style given for fetching result.');
  111. }
  112. /**
  113. * {@inheritdoc}
  114. *
  115. * @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
  116. */
  117. public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
  118. {
  119. $rows = [];
  120. while ($row = $this->fetch($fetchMode)) {
  121. $rows[] = $row;
  122. }
  123. return $rows;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. *
  128. * @deprecated Use fetchOne() instead.
  129. */
  130. public function fetchColumn($columnIndex = 0)
  131. {
  132. $row = $this->fetch(FetchMode::NUMERIC);
  133. // TODO: verify that return false is the correct behavior
  134. return $row[$columnIndex] ?? false;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function fetchNumeric()
  140. {
  141. $row = $this->doFetch();
  142. if ($row === false) {
  143. return false;
  144. }
  145. return array_values($row);
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function fetchAssociative()
  151. {
  152. return $this->doFetch();
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function fetchOne()
  158. {
  159. $row = $this->doFetch();
  160. if ($row === false) {
  161. return false;
  162. }
  163. return reset($row);
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function fetchAllNumeric(): array
  169. {
  170. return FetchUtils::fetchAllNumeric($this);
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function fetchAllAssociative(): array
  176. {
  177. return FetchUtils::fetchAllAssociative($this);
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function fetchFirstColumn(): array
  183. {
  184. return FetchUtils::fetchFirstColumn($this);
  185. }
  186. public function free(): void
  187. {
  188. $this->data = [];
  189. }
  190. /**
  191. * @return mixed|false
  192. */
  193. private function doFetch()
  194. {
  195. if (! isset($this->data[$this->num])) {
  196. return false;
  197. }
  198. return $this->data[$this->num++];
  199. }
  200. }