LazyCollection.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Gedmo\References;
  3. use Doctrine\Common\Collections\Collection;
  4. /**
  5. * Lazy collection for loading reference many associations.
  6. *
  7. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  8. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  9. * @author Jonathan H. Wage <jonwage@gmail.com>
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class LazyCollection implements Collection
  13. {
  14. private $results;
  15. private $callback;
  16. public function __construct($callback)
  17. {
  18. $this->callback = $callback;
  19. }
  20. public function add($element)
  21. {
  22. $this->initialize();
  23. return $this->results->add($element);
  24. }
  25. public function clear()
  26. {
  27. $this->initialize();
  28. return $this->results->clear();
  29. }
  30. public function contains($element)
  31. {
  32. $this->initialize();
  33. return $this->results->contains($element);
  34. }
  35. public function containsKey($key)
  36. {
  37. $this->initialize();
  38. return $this->results->containsKey($key);
  39. }
  40. public function current()
  41. {
  42. $this->initialize();
  43. return $this->results->current();
  44. }
  45. public function exists(\Closure $p)
  46. {
  47. $this->initialize();
  48. return $this->results->exists($p);
  49. }
  50. public function filter(\Closure $p)
  51. {
  52. $this->initialize();
  53. return $this->results->filter($p);
  54. }
  55. public function first()
  56. {
  57. $this->initialize();
  58. return $this->results->first();
  59. }
  60. public function forAll(\Closure $p)
  61. {
  62. $this->initialize();
  63. return $this->results->forAll($p);
  64. }
  65. public function get($key)
  66. {
  67. $this->initialize();
  68. return $this->results->get($key);
  69. }
  70. public function getKeys()
  71. {
  72. $this->initialize();
  73. return $this->results->getKeys();
  74. }
  75. public function getValues()
  76. {
  77. $this->initialize();
  78. return $this->results->getValues();
  79. }
  80. public function indexOf($element)
  81. {
  82. $this->initialize();
  83. return $this->results->indexOf($element);
  84. }
  85. public function isEmpty()
  86. {
  87. $this->initialize();
  88. return $this->results->isEmpty();
  89. }
  90. public function key()
  91. {
  92. $this->initialize();
  93. return $this->results->key();
  94. }
  95. public function last()
  96. {
  97. $this->initialize();
  98. return $this->results->last();
  99. }
  100. public function map(\Closure $func)
  101. {
  102. $this->initialize();
  103. return $this->results->map($func);
  104. }
  105. public function next()
  106. {
  107. $this->initialize();
  108. return $this->results->next();
  109. }
  110. public function partition(\Closure $p)
  111. {
  112. $this->initialize();
  113. return $this->results->partition($p);
  114. }
  115. public function remove($key)
  116. {
  117. $this->initialize();
  118. return $this->results->remove($key);
  119. }
  120. public function removeElement($element)
  121. {
  122. $this->initialize();
  123. return $this->results->removeElement($element);
  124. }
  125. public function set($key, $value)
  126. {
  127. $this->initialize();
  128. return $this->results->set($key, $value);
  129. }
  130. public function slice($offset, $length = null)
  131. {
  132. $this->initialize();
  133. return $this->results->slice($offset, $length);
  134. }
  135. public function toArray()
  136. {
  137. $this->initialize();
  138. return $this->results->toArray();
  139. }
  140. public function offsetExists($offset)
  141. {
  142. $this->initialize();
  143. return $this->results->offsetExists($offset);
  144. }
  145. public function offsetGet($offset)
  146. {
  147. $this->initialize();
  148. return $this->results->offsetGet($offset);
  149. }
  150. public function offsetSet($offset, $value)
  151. {
  152. $this->initialize();
  153. return $this->results->offsetSet($offset, $value);
  154. }
  155. public function offsetUnset($offset)
  156. {
  157. $this->initialize();
  158. return $this->results->offsetUnset($offset);
  159. }
  160. public function getIterator()
  161. {
  162. $this->initialize();
  163. return $this->results->getIterator();
  164. }
  165. public function count()
  166. {
  167. $this->initialize();
  168. return $this->results->count();
  169. }
  170. private function initialize()
  171. {
  172. if (null === $this->results) {
  173. $this->results = call_user_func($this->callback);
  174. }
  175. }
  176. }