ArrayCollection.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 ArrayIterator;
  21. use Closure;
  22. use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
  23. /**
  24. * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
  25. *
  26. * @since 2.0
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. */
  31. class ArrayCollection implements Collection, Selectable
  32. {
  33. /**
  34. * An array containing the entries of this collection.
  35. *
  36. * @var array
  37. */
  38. private $elements;
  39. /**
  40. * Initializes a new ArrayCollection.
  41. *
  42. * @param array $elements
  43. */
  44. public function __construct(array $elements = array())
  45. {
  46. $this->elements = $elements;
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function toArray()
  52. {
  53. return $this->elements;
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function first()
  59. {
  60. return reset($this->elements);
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function last()
  66. {
  67. return end($this->elements);
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function key()
  73. {
  74. return key($this->elements);
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function next()
  80. {
  81. return next($this->elements);
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public function current()
  87. {
  88. return current($this->elements);
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. public function remove($key)
  94. {
  95. if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
  96. return null;
  97. }
  98. $removed = $this->elements[$key];
  99. unset($this->elements[$key]);
  100. return $removed;
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public function removeElement($element)
  106. {
  107. $key = array_search($element, $this->elements, true);
  108. if ($key === false) {
  109. return false;
  110. }
  111. unset($this->elements[$key]);
  112. return true;
  113. }
  114. /**
  115. * Required by interface ArrayAccess.
  116. *
  117. * {@inheritDoc}
  118. */
  119. public function offsetExists($offset)
  120. {
  121. return $this->containsKey($offset);
  122. }
  123. /**
  124. * Required by interface ArrayAccess.
  125. *
  126. * {@inheritDoc}
  127. */
  128. public function offsetGet($offset)
  129. {
  130. return $this->get($offset);
  131. }
  132. /**
  133. * Required by interface ArrayAccess.
  134. *
  135. * {@inheritDoc}
  136. */
  137. public function offsetSet($offset, $value)
  138. {
  139. if ( ! isset($offset)) {
  140. return $this->add($value);
  141. }
  142. $this->set($offset, $value);
  143. }
  144. /**
  145. * Required by interface ArrayAccess.
  146. *
  147. * {@inheritDoc}
  148. */
  149. public function offsetUnset($offset)
  150. {
  151. return $this->remove($offset);
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public function containsKey($key)
  157. {
  158. return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. public function contains($element)
  164. {
  165. return in_array($element, $this->elements, true);
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. public function exists(Closure $p)
  171. {
  172. foreach ($this->elements as $key => $element) {
  173. if ($p($key, $element)) {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. /**
  180. * {@inheritDoc}
  181. */
  182. public function indexOf($element)
  183. {
  184. return array_search($element, $this->elements, true);
  185. }
  186. /**
  187. * {@inheritDoc}
  188. */
  189. public function get($key)
  190. {
  191. return isset($this->elements[$key]) ? $this->elements[$key] : null;
  192. }
  193. /**
  194. * {@inheritDoc}
  195. */
  196. public function getKeys()
  197. {
  198. return array_keys($this->elements);
  199. }
  200. /**
  201. * {@inheritDoc}
  202. */
  203. public function getValues()
  204. {
  205. return array_values($this->elements);
  206. }
  207. /**
  208. * {@inheritDoc}
  209. */
  210. public function count()
  211. {
  212. return count($this->elements);
  213. }
  214. /**
  215. * {@inheritDoc}
  216. */
  217. public function set($key, $value)
  218. {
  219. $this->elements[$key] = $value;
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. public function add($value)
  225. {
  226. $this->elements[] = $value;
  227. return true;
  228. }
  229. /**
  230. * {@inheritDoc}
  231. */
  232. public function isEmpty()
  233. {
  234. return empty($this->elements);
  235. }
  236. /**
  237. * Required by interface IteratorAggregate.
  238. *
  239. * {@inheritDoc}
  240. */
  241. public function getIterator()
  242. {
  243. return new ArrayIterator($this->elements);
  244. }
  245. /**
  246. * {@inheritDoc}
  247. */
  248. public function map(Closure $func)
  249. {
  250. return new static(array_map($func, $this->elements));
  251. }
  252. /**
  253. * {@inheritDoc}
  254. */
  255. public function filter(Closure $p)
  256. {
  257. return new static(array_filter($this->elements, $p));
  258. }
  259. /**
  260. * {@inheritDoc}
  261. */
  262. public function forAll(Closure $p)
  263. {
  264. foreach ($this->elements as $key => $element) {
  265. if ( ! $p($key, $element)) {
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. /**
  272. * {@inheritDoc}
  273. */
  274. public function partition(Closure $p)
  275. {
  276. $matches = $noMatches = array();
  277. foreach ($this->elements as $key => $element) {
  278. if ($p($key, $element)) {
  279. $matches[$key] = $element;
  280. } else {
  281. $noMatches[$key] = $element;
  282. }
  283. }
  284. return array(new static($matches), new static($noMatches));
  285. }
  286. /**
  287. * Returns a string representation of this object.
  288. *
  289. * @return string
  290. */
  291. public function __toString()
  292. {
  293. return __CLASS__ . '@' . spl_object_hash($this);
  294. }
  295. /**
  296. * {@inheritDoc}
  297. */
  298. public function clear()
  299. {
  300. $this->elements = array();
  301. }
  302. /**
  303. * {@inheritDoc}
  304. */
  305. public function slice($offset, $length = null)
  306. {
  307. return array_slice($this->elements, $offset, $length, true);
  308. }
  309. /**
  310. * {@inheritDoc}
  311. */
  312. public function matching(Criteria $criteria)
  313. {
  314. $expr = $criteria->getWhereExpression();
  315. $filtered = $this->elements;
  316. if ($expr) {
  317. $visitor = new ClosureExpressionVisitor();
  318. $filter = $visitor->dispatch($expr);
  319. $filtered = array_filter($filtered, $filter);
  320. }
  321. if ($orderings = $criteria->getOrderings()) {
  322. foreach (array_reverse($orderings) as $field => $ordering) {
  323. $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1);
  324. }
  325. uasort($filtered, $next);
  326. }
  327. $offset = $criteria->getFirstResult();
  328. $length = $criteria->getMaxResults();
  329. if ($offset || $length) {
  330. $filtered = array_slice($filtered, (int)$offset, $length);
  331. }
  332. return new static($filtered);
  333. }
  334. }