AbstractLazyCollection.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Collections;
  20. use Closure;
  21. /**
  22. * Lazy collection that is backed by a concrete collection
  23. *
  24. * @author Michaël Gallego <mic.gallego@gmail.com>
  25. * @since 1.2
  26. */
  27. abstract class AbstractLazyCollection implements Collection
  28. {
  29. /**
  30. * The backed collection to use
  31. *
  32. * @var Collection
  33. */
  34. protected $collection;
  35. /**
  36. * @var boolean
  37. */
  38. protected $initialized = false;
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function count()
  43. {
  44. $this->initialize();
  45. return $this->collection->count();
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function add($element)
  51. {
  52. $this->initialize();
  53. return $this->collection->add($element);
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function clear()
  59. {
  60. $this->initialize();
  61. $this->collection->clear();
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function contains($element)
  67. {
  68. $this->initialize();
  69. return $this->collection->contains($element);
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function isEmpty()
  75. {
  76. $this->initialize();
  77. return $this->collection->isEmpty();
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function remove($key)
  83. {
  84. $this->initialize();
  85. return $this->collection->remove($key);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function removeElement($element)
  91. {
  92. $this->initialize();
  93. return $this->collection->removeElement($element);
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function containsKey($key)
  99. {
  100. $this->initialize();
  101. return $this->collection->containsKey($key);
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function get($key)
  107. {
  108. $this->initialize();
  109. return $this->collection->get($key);
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. public function getKeys()
  115. {
  116. $this->initialize();
  117. return $this->collection->getKeys();
  118. }
  119. /**
  120. * {@inheritDoc}
  121. */
  122. public function getValues()
  123. {
  124. $this->initialize();
  125. return $this->collection->getValues();
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public function set($key, $value)
  131. {
  132. $this->initialize();
  133. $this->collection->set($key, $value);
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function toArray()
  139. {
  140. $this->initialize();
  141. return $this->collection->toArray();
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public function first()
  147. {
  148. $this->initialize();
  149. return $this->collection->first();
  150. }
  151. /**
  152. * {@inheritDoc}
  153. */
  154. public function last()
  155. {
  156. $this->initialize();
  157. return $this->collection->last();
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. public function key()
  163. {
  164. $this->initialize();
  165. return $this->collection->key();
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. public function current()
  171. {
  172. $this->initialize();
  173. return $this->collection->current();
  174. }
  175. /**
  176. * {@inheritDoc}
  177. */
  178. public function next()
  179. {
  180. $this->initialize();
  181. return $this->collection->next();
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public function exists(Closure $p)
  187. {
  188. $this->initialize();
  189. return $this->collection->exists($p);
  190. }
  191. /**
  192. * {@inheritDoc}
  193. */
  194. public function filter(Closure $p)
  195. {
  196. $this->initialize();
  197. return $this->collection->filter($p);
  198. }
  199. /**
  200. * {@inheritDoc}
  201. */
  202. public function forAll(Closure $p)
  203. {
  204. $this->initialize();
  205. return $this->collection->forAll($p);
  206. }
  207. /**
  208. * {@inheritDoc}
  209. */
  210. public function map(Closure $func)
  211. {
  212. $this->initialize();
  213. return $this->collection->map($func);
  214. }
  215. /**
  216. * {@inheritDoc}
  217. */
  218. public function partition(Closure $p)
  219. {
  220. $this->initialize();
  221. return $this->collection->partition($p);
  222. }
  223. /**
  224. * {@inheritDoc}
  225. */
  226. public function indexOf($element)
  227. {
  228. $this->initialize();
  229. return $this->collection->indexOf($element);
  230. }
  231. /**
  232. * {@inheritDoc}
  233. */
  234. public function slice($offset, $length = null)
  235. {
  236. $this->initialize();
  237. return $this->collection->slice($offset, $length);
  238. }
  239. /**
  240. * {@inheritDoc}
  241. */
  242. public function getIterator()
  243. {
  244. $this->initialize();
  245. return $this->collection->getIterator();
  246. }
  247. /**
  248. * {@inheritDoc}
  249. */
  250. public function offsetExists($offset)
  251. {
  252. $this->initialize();
  253. return $this->collection->offsetExists($offset);
  254. }
  255. /**
  256. * {@inheritDoc}
  257. */
  258. public function offsetGet($offset)
  259. {
  260. $this->initialize();
  261. return $this->collection->offsetGet($offset);
  262. }
  263. /**
  264. * {@inheritDoc}
  265. */
  266. public function offsetSet($offset, $value)
  267. {
  268. $this->initialize();
  269. $this->collection->offsetSet($offset, $value);
  270. }
  271. /**
  272. * {@inheritDoc}
  273. */
  274. public function offsetUnset($offset)
  275. {
  276. $this->initialize();
  277. $this->collection->offsetUnset($offset);
  278. }
  279. /**
  280. * Is the lazy collection already initialized?
  281. *
  282. * @return bool
  283. */
  284. public function isInitialized()
  285. {
  286. return $this->initialized;
  287. }
  288. /**
  289. * Initialize the collection
  290. *
  291. * @return void
  292. */
  293. protected function initialize()
  294. {
  295. if ( ! $this->initialized) {
  296. $this->doInitialize();
  297. $this->initialized = true;
  298. }
  299. }
  300. /**
  301. * Do the initialization logic
  302. *
  303. * @return void
  304. */
  305. abstract protected function doInitialize();
  306. }