AbstractLazyCollection.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace Doctrine\Common\Collections;
  3. use Closure;
  4. /**
  5. * Lazy collection that is backed by a concrete collection
  6. *
  7. * @phpstan-template TKey
  8. * @psalm-template TKey of array-key
  9. * @psalm-template T
  10. * @template-implements Collection<TKey,T>
  11. */
  12. abstract class AbstractLazyCollection implements Collection
  13. {
  14. /**
  15. * The backed collection to use
  16. *
  17. * @psalm-var Collection<TKey,T>
  18. * @var Collection
  19. */
  20. protected $collection;
  21. /** @var bool */
  22. protected $initialized = false;
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function count()
  27. {
  28. $this->initialize();
  29. return $this->collection->count();
  30. }
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function add($element)
  35. {
  36. $this->initialize();
  37. return $this->collection->add($element);
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function clear()
  43. {
  44. $this->initialize();
  45. $this->collection->clear();
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function contains($element)
  51. {
  52. $this->initialize();
  53. return $this->collection->contains($element);
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function isEmpty()
  59. {
  60. $this->initialize();
  61. return $this->collection->isEmpty();
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function remove($key)
  67. {
  68. $this->initialize();
  69. return $this->collection->remove($key);
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function removeElement($element)
  75. {
  76. $this->initialize();
  77. return $this->collection->removeElement($element);
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function containsKey($key)
  83. {
  84. $this->initialize();
  85. return $this->collection->containsKey($key);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function get($key)
  91. {
  92. $this->initialize();
  93. return $this->collection->get($key);
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function getKeys()
  99. {
  100. $this->initialize();
  101. return $this->collection->getKeys();
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function getValues()
  107. {
  108. $this->initialize();
  109. return $this->collection->getValues();
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. public function set($key, $value)
  115. {
  116. $this->initialize();
  117. $this->collection->set($key, $value);
  118. }
  119. /**
  120. * {@inheritDoc}
  121. */
  122. public function toArray()
  123. {
  124. $this->initialize();
  125. return $this->collection->toArray();
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public function first()
  131. {
  132. $this->initialize();
  133. return $this->collection->first();
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function last()
  139. {
  140. $this->initialize();
  141. return $this->collection->last();
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public function key()
  147. {
  148. $this->initialize();
  149. return $this->collection->key();
  150. }
  151. /**
  152. * {@inheritDoc}
  153. */
  154. public function current()
  155. {
  156. $this->initialize();
  157. return $this->collection->current();
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. public function next()
  163. {
  164. $this->initialize();
  165. return $this->collection->next();
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. public function exists(Closure $p)
  171. {
  172. $this->initialize();
  173. return $this->collection->exists($p);
  174. }
  175. /**
  176. * {@inheritDoc}
  177. */
  178. public function filter(Closure $p)
  179. {
  180. $this->initialize();
  181. return $this->collection->filter($p);
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public function forAll(Closure $p)
  187. {
  188. $this->initialize();
  189. return $this->collection->forAll($p);
  190. }
  191. /**
  192. * {@inheritDoc}
  193. */
  194. public function map(Closure $func)
  195. {
  196. $this->initialize();
  197. return $this->collection->map($func);
  198. }
  199. /**
  200. * {@inheritDoc}
  201. */
  202. public function partition(Closure $p)
  203. {
  204. $this->initialize();
  205. return $this->collection->partition($p);
  206. }
  207. /**
  208. * {@inheritDoc}
  209. */
  210. public function indexOf($element)
  211. {
  212. $this->initialize();
  213. return $this->collection->indexOf($element);
  214. }
  215. /**
  216. * {@inheritDoc}
  217. */
  218. public function slice($offset, $length = null)
  219. {
  220. $this->initialize();
  221. return $this->collection->slice($offset, $length);
  222. }
  223. /**
  224. * {@inheritDoc}
  225. */
  226. public function getIterator()
  227. {
  228. $this->initialize();
  229. return $this->collection->getIterator();
  230. }
  231. /**
  232. * {@inheritDoc}
  233. *
  234. * @psalm-param TKey $offset
  235. */
  236. public function offsetExists($offset)
  237. {
  238. $this->initialize();
  239. return $this->collection->offsetExists($offset);
  240. }
  241. /**
  242. * {@inheritDoc}
  243. *
  244. * @param int|string $offset
  245. *
  246. * @return mixed
  247. *
  248. * @psalm-param TKey $offset
  249. */
  250. public function offsetGet($offset)
  251. {
  252. $this->initialize();
  253. return $this->collection->offsetGet($offset);
  254. }
  255. /**
  256. * {@inheritDoc}
  257. *
  258. * @param mixed $value
  259. *
  260. * @psalm-param TKey $offset
  261. */
  262. public function offsetSet($offset, $value)
  263. {
  264. $this->initialize();
  265. $this->collection->offsetSet($offset, $value);
  266. }
  267. /**
  268. * {@inheritDoc}
  269. *
  270. * @psalm-param TKey $offset
  271. */
  272. public function offsetUnset($offset)
  273. {
  274. $this->initialize();
  275. $this->collection->offsetUnset($offset);
  276. }
  277. /**
  278. * Is the lazy collection already initialized?
  279. *
  280. * @return bool
  281. */
  282. public function isInitialized()
  283. {
  284. return $this->initialized;
  285. }
  286. /**
  287. * Initialize the collection
  288. *
  289. * @return void
  290. */
  291. protected function initialize()
  292. {
  293. if ($this->initialized) {
  294. return;
  295. }
  296. $this->doInitialize();
  297. $this->initialized = true;
  298. }
  299. /**
  300. * Do the initialization logic
  301. *
  302. * @return void
  303. */
  304. abstract protected function doInitialize();
  305. }