comparison vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
24 use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; 24 use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
25 25
26 /** 26 /**
27 * An ArrayCollection is a Collection implementation that wraps a regular PHP array. 27 * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
28 * 28 *
29 * Warning: Using (un-)serialize() on a collection is not a supported use-case
30 * and may break when we change the internals in the future. If you need to
31 * serialize a collection use {@link toArray()} and reconstruct the collection
32 * manually.
33 *
29 * @since 2.0 34 * @since 2.0
30 * @author Guilherme Blanco <guilhermeblanco@hotmail.com> 35 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
31 * @author Jonathan Wage <jonwage@gmail.com> 36 * @author Jonathan Wage <jonwage@gmail.com>
32 * @author Roman Borschel <roman@code-factory.org> 37 * @author Roman Borschel <roman@code-factory.org>
33 */ 38 */
49 { 54 {
50 $this->elements = $elements; 55 $this->elements = $elements;
51 } 56 }
52 57
53 /** 58 /**
59 * Creates a new instance from the specified elements.
60 *
61 * This method is provided for derived classes to specify how a new
62 * instance should be created when constructor semantics have changed.
63 *
64 * @param array $elements Elements.
65 *
66 * @return static
67 */
68 protected function createFrom(array $elements)
69 {
70 return new static($elements);
71 }
72
73 /**
54 * {@inheritDoc} 74 * {@inheritDoc}
55 */ 75 */
56 public function toArray() 76 public function toArray()
57 { 77 {
58 return $this->elements; 78 return $this->elements;
252 } 272 }
253 273
254 /** 274 /**
255 * {@inheritDoc} 275 * {@inheritDoc}
256 */ 276 */
257 public function add($value) 277 public function add($element)
258 { 278 {
259 $this->elements[] = $value; 279 $this->elements[] = $element;
260 280
261 return true; 281 return true;
262 } 282 }
263 283
264 /** 284 /**
282 /** 302 /**
283 * {@inheritDoc} 303 * {@inheritDoc}
284 */ 304 */
285 public function map(Closure $func) 305 public function map(Closure $func)
286 { 306 {
287 return new static(array_map($func, $this->elements)); 307 return $this->createFrom(array_map($func, $this->elements));
288 } 308 }
289 309
290 /** 310 /**
291 * {@inheritDoc} 311 * {@inheritDoc}
292 */ 312 */
293 public function filter(Closure $p) 313 public function filter(Closure $p)
294 { 314 {
295 return new static(array_filter($this->elements, $p)); 315 return $this->createFrom(array_filter($this->elements, $p));
296 } 316 }
297 317
298 /** 318 /**
299 * {@inheritDoc} 319 * {@inheritDoc}
300 */ 320 */
322 } else { 342 } else {
323 $noMatches[$key] = $element; 343 $noMatches[$key] = $element;
324 } 344 }
325 } 345 }
326 346
327 return array(new static($matches), new static($noMatches)); 347 return array($this->createFrom($matches), $this->createFrom($noMatches));
328 } 348 }
329 349
330 /** 350 /**
331 * Returns a string representation of this object. 351 * Returns a string representation of this object.
332 * 352 *
366 $filter = $visitor->dispatch($expr); 386 $filter = $visitor->dispatch($expr);
367 $filtered = array_filter($filtered, $filter); 387 $filtered = array_filter($filtered, $filter);
368 } 388 }
369 389
370 if ($orderings = $criteria->getOrderings()) { 390 if ($orderings = $criteria->getOrderings()) {
391 $next = null;
371 foreach (array_reverse($orderings) as $field => $ordering) { 392 foreach (array_reverse($orderings) as $field => $ordering) {
372 $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1); 393 $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1, $next);
373 } 394 }
374 395
375 uasort($filtered, $next); 396 uasort($filtered, $next);
376 } 397 }
377 398
380 401
381 if ($offset || $length) { 402 if ($offset || $length) {
382 $filtered = array_slice($filtered, (int)$offset, $length); 403 $filtered = array_slice($filtered, (int)$offset, $length);
383 } 404 }
384 405
385 return new static($filtered); 406 return $this->createFrom($filtered);
386 } 407 }
387 } 408 }