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

Breadcrumb

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

class AbstractMap

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

@template K of array-key @template T @extends AbstractArray<T> @implements MapInterface<K, T>

Hierarchy

  • class \Ramsey\Collection\AbstractArray implements \Ramsey\Collection\ArrayInterface
    • class \Ramsey\Collection\Map\AbstractMap extends \Ramsey\Collection\AbstractArray implements \Ramsey\Collection\Map\MapInterface

Expanded class hierarchy of AbstractMap

File

vendor/ramsey/collection/src/Map/AbstractMap.php, line 35

Namespace

Ramsey\Collection\Map
View source
abstract class AbstractMap extends AbstractArray implements MapInterface {
    
    /**
     * @param array<K, T> $data The initial items to add to this map.
     */
    public function __construct(array $data = []) {
        parent::__construct($data);
    }
    
    /**
     * @return Traversable<K, T>
     */
    public function getIterator() : Traversable {
        return parent::getIterator();
    }
    
    /**
     * @param K $offset The offset to set
     * @param T $value The value to set at the given offset.
     *
     * @inheritDoc
     * @psalm-suppress MoreSpecificImplementedParamType,DocblockTypeContradiction
     */
    public function offsetSet(mixed $offset, mixed $value) : void {
        if ($offset === null) {
            throw new InvalidArgumentException('Map elements are key/value pairs; a key must be provided for ' . 'value ' . var_export($value, true));
        }
        $this->data[$offset] = $value;
    }
    public function containsKey(int|string $key) : bool {
        return array_key_exists($key, $this->data);
    }
    public function containsValue(mixed $value) : bool {
        return in_array($value, $this->data, true);
    }
    
    /**
     * @inheritDoc
     */
    public function keys() : array {
        return array_keys($this->data);
    }
    
    /**
     * @param K $key The key to return from the map.
     * @param T | null $defaultValue The default value to use if `$key` is not found.
     *
     * @return T | null the value or `null` if the key could not be found.
     */
    public function get(int|string $key, mixed $defaultValue = null) : mixed {
        return $this[$key] ?? $defaultValue;
    }
    
    /**
     * @param K $key The key to put or replace in the map.
     * @param T $value The value to store at `$key`.
     *
     * @return T | null the previous value associated with key, or `null` if
     *     there was no mapping for `$key`.
     */
    public function put(int|string $key, mixed $value) : mixed {
        $previousValue = $this->get($key);
        $this[$key] = $value;
        return $previousValue;
    }
    
    /**
     * @param K $key The key to put in the map.
     * @param T $value The value to store at `$key`.
     *
     * @return T | null the previous value associated with key, or `null` if
     *     there was no mapping for `$key`.
     */
    public function putIfAbsent(int|string $key, mixed $value) : mixed {
        $currentValue = $this->get($key);
        if ($currentValue === null) {
            $this[$key] = $value;
        }
        return $currentValue;
    }
    
    /**
     * @param K $key The key to remove from the map.
     *
     * @return T | null the previous value associated with key, or `null` if
     *     there was no mapping for `$key`.
     */
    public function remove(int|string $key) : mixed {
        $previousValue = $this->get($key);
        unset($this[$key]);
        return $previousValue;
    }
    public function removeIf(int|string $key, mixed $value) : bool {
        if ($this->get($key) === $value) {
            unset($this[$key]);
            return true;
        }
        return false;
    }
    
    /**
     * @param K $key The key to replace.
     * @param T $value The value to set at `$key`.
     *
     * @return T | null the previous value associated with key, or `null` if
     *     there was no mapping for `$key`.
     */
    public function replace(int|string $key, mixed $value) : mixed {
        $currentValue = $this->get($key);
        if ($this->containsKey($key)) {
            $this[$key] = $value;
        }
        return $currentValue;
    }
    public function replaceIf(int|string $key, mixed $oldValue, mixed $newValue) : bool {
        if ($this->get($key) === $oldValue) {
            $this[$key] = $newValue;
            return true;
        }
        return false;
    }
    
    /**
     * @return array<K, T>
     */
    public function __serialize() : array {
        return parent::__serialize();
    }
    
    /**
     * @return array<K, T>
     */
    public function toArray() : array {
        return parent::toArray();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractArray::$data protected property The items of this array.
AbstractArray::clear public function Removes all items from this array. Overrides ArrayInterface::clear
AbstractArray::count public function Returns the number of items in this array.
AbstractArray::isEmpty public function Returns `true` if this array is empty. Overrides ArrayInterface::isEmpty
AbstractArray::offsetExists public function Returns `true` if the given offset exists in this array.
AbstractArray::offsetGet public function Returns the value at the specified offset.
AbstractArray::offsetUnset public function Removes the given offset and its value from the array.
AbstractArray::__unserialize public function Adds unserialized data to the object.
AbstractMap::containsKey public function Returns `true` if this map contains a mapping for the specified key. Overrides MapInterface::containsKey
AbstractMap::containsValue public function Returns `true` if this map maps one or more keys to the specified value. Overrides MapInterface::containsValue
AbstractMap::get public function Overrides MapInterface::get
AbstractMap::getIterator public function Overrides AbstractArray::getIterator
AbstractMap::keys public function @inheritDoc Overrides MapInterface::keys
AbstractMap::offsetSet public function @inheritDoc
@psalm-suppress MoreSpecificImplementedParamType,DocblockTypeContradiction
Overrides AbstractArray::offsetSet 2
AbstractMap::put public function Overrides MapInterface::put
AbstractMap::putIfAbsent public function Overrides MapInterface::putIfAbsent
AbstractMap::remove public function Overrides MapInterface::remove
AbstractMap::removeIf public function Removes the entry for the specified key only if it is currently mapped to
the specified value.
Overrides MapInterface::removeIf
AbstractMap::replace public function Overrides MapInterface::replace
AbstractMap::replaceIf public function Replaces the entry for the specified key only if currently mapped to the
specified value.
Overrides MapInterface::replaceIf
AbstractMap::toArray public function Overrides AbstractArray::toArray
AbstractMap::__construct public function Overrides AbstractArray::__construct 2
AbstractMap::__serialize public function Overrides AbstractArray::__serialize
RSS feed
Powered by Drupal