PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php

https://gitlab.com/mario.uriarte/doctrine2.5-tutorial
PHP | 387 lines | 191 code | 53 blank | 143 comment | 14 complexity | 09b2d66c94acc690ee4fc11af85b6e60 MD5 | raw file
  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. }