LazyCollection.php 4.5KB

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