Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AbstractCollection.php

function AbstractCollection::merge

Parameters

CollectionInterface<T> ...$collections The collections to merge.:

Return value

CollectionInterface<T>

Throws

CollectionMismatchException if unable to merge any of the given collections or items within the given collections due to type mismatch errors.

Overrides CollectionInterface::merge

File

vendor/ramsey/collection/src/AbstractCollection.php, line 307

Class

AbstractCollection
This class provides a basic implementation of `CollectionInterface`, to minimize the effort required to implement this interface

Namespace

Ramsey\Collection

Code

public function merge(CollectionInterface ...$collections) : CollectionInterface {
    $mergedCollection = clone $this;
    foreach ($collections as $index => $collection) {
        if (!$collection instanceof static) {
            throw new CollectionMismatchException(sprintf('Collection with index %d must be of type %s', $index, static::class));
        }
        // When using generics (Collection.php, Set.php, etc),
        // we also need to make sure that the internal types match each other
        if ($this->getUniformType($collection) !== $this->getUniformType($this)) {
            throw new CollectionMismatchException(sprintf('Collection items in collection with index %d must be of type %s', $index, $this->getType()));
        }
        foreach ($collection as $key => $value) {
            if (is_int($key)) {
                $mergedCollection[] = $value;
            }
            else {
                $mergedCollection[$key] = $value;
            }
        }
    }
    return $mergedCollection;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal