AbstractLazyCollection.php 5.8 KB

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