ArrayCollection.php 8.6 KB

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