ArrayCollection.php 8.0 KB

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