ArrayStatement.php 4.8 KB

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