aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/ramsey/collection/src/Map
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ramsey/collection/src/Map')
-rw-r--r--vendor/ramsey/collection/src/Map/AbstractMap.php226
-rw-r--r--vendor/ramsey/collection/src/Map/AbstractTypedMap.php57
-rw-r--r--vendor/ramsey/collection/src/Map/AssociativeArrayMap.php22
-rw-r--r--vendor/ramsey/collection/src/Map/MapInterface.php138
-rw-r--r--vendor/ramsey/collection/src/Map/NamedParameterMap.php118
-rw-r--r--vendor/ramsey/collection/src/Map/TypedMap.php137
-rw-r--r--vendor/ramsey/collection/src/Map/TypedMapInterface.php32
7 files changed, 730 insertions, 0 deletions
diff --git a/vendor/ramsey/collection/src/Map/AbstractMap.php b/vendor/ramsey/collection/src/Map/AbstractMap.php
new file mode 100644
index 000000000..6b2e97a08
--- /dev/null
+++ b/vendor/ramsey/collection/src/Map/AbstractMap.php
@@ -0,0 +1,226 @@
+<?php
+
+/**
+ * This file is part of the ramsey/collection library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Collection\Map;
+
+use Ramsey\Collection\AbstractArray;
+use Ramsey\Collection\Exception\InvalidArgumentException;
+
+use function array_key_exists;
+use function array_keys;
+use function in_array;
+
+/**
+ * This class provides a basic implementation of `MapInterface`, to minimize the
+ * effort required to implement this interface.
+ */
+abstract class AbstractMap extends AbstractArray implements MapInterface
+{
+ /**
+ * Sets the given value to the given offset in the map.
+ *
+ * @param mixed $offset The offset to set.
+ * @param mixed $value The value to set at the given offset.
+ *
+ * @throws InvalidArgumentException if the offset provided is `null`.
+ */
+ public function offsetSet($offset, $value): void
+ {
+ if ($offset === null) {
+ throw new InvalidArgumentException(
+ 'Map elements are key/value pairs; a key must be provided for '
+ . 'value ' . $value
+ );
+ }
+
+ $this->data[$offset] = $value;
+ }
+
+ /**
+ * Returns `true` if this map contains a mapping for the specified key.
+ *
+ * @param mixed $key The key to check in the map.
+ */
+ public function containsKey($key): bool
+ {
+ return array_key_exists($key, $this->data);
+ }
+
+ /**
+ * Returns `true` if this map maps one or more keys to the specified value.
+ *
+ * This performs a strict type check on the value.
+ *
+ * @param mixed $value The value to check in the map.
+ */
+ public function containsValue($value): bool
+ {
+ return in_array($value, $this->data, true);
+ }
+
+ /**
+ * Return an array of the keys contained in this map.
+ *
+ * @return mixed[]
+ */
+ public function keys(): array
+ {
+ return array_keys($this->data);
+ }
+
+ /**
+ * Returns the value to which the specified key is mapped, `null` if this
+ * map contains no mapping for the key, or (optionally) `$defaultValue` if
+ * this map contains no mapping for the key.
+ *
+ * @param mixed $key The key to return from the map.
+ * @param mixed $defaultValue The default value to use if `$key` is not found.
+ *
+ * @return mixed|null the value or `null` if the key could not be found.
+ */
+ public function get($key, $defaultValue = null)
+ {
+ if (!$this->containsKey($key)) {
+ return $defaultValue;
+ }
+
+ return $this[$key];
+ }
+
+ /**
+ * Associates the specified value with the specified key in this map.
+ *
+ * If the map previously contained a mapping for the key, the old value is
+ * replaced by the specified value.
+ *
+ * @param mixed $key The key to put or replace in the map.
+ * @param mixed $value The value to store at `$key`.
+ *
+ * @return mixed|null the previous value associated with key, or `null` if
+ * there was no mapping for `$key`.
+ */
+ public function put($key, $value)
+ {
+ $previousValue = $this->get($key);
+ $this[$key] = $value;
+
+ return $previousValue;
+ }
+
+ /**
+ * Associates the specified value with the specified key in this map only if
+ * it is not already set.
+ *
+ * If there is already a value associated with `$key`, this returns that
+ * value without replacing it.
+ *
+ * @param mixed $key The key to put in the map.
+ * @param mixed $value The value to store at `$key`.
+ *
+ * @return mixed|null the previous value associated with key, or `null` if
+ * there was no mapping for `$key`.
+ */
+ public function putIfAbsent($key, $value)
+ {
+ $currentValue = $this->get($key);
+
+ if ($currentValue === null) {
+ $this[$key] = $value;
+ }
+
+ return $currentValue;
+ }
+
+ /**
+ * Removes the mapping for a key from this map if it is present.
+ *
+ * @param mixed $key The key to remove from the map.
+ *
+ * @return mixed|null the previous value associated with key, or `null` if
+ * there was no mapping for `$key`.
+ */
+ public function remove($key)
+ {
+ $previousValue = $this->get($key);
+ unset($this[$key]);
+
+ return $previousValue;
+ }
+
+ /**
+ * Removes the entry for the specified key only if it is currently mapped to
+ * the specified value.
+ *
+ * This performs a strict type check on the value.
+ *
+ * @param mixed $key The key to remove from the map.
+ * @param mixed $value The value to match.
+ *
+ * @return bool true if the value was removed.
+ */
+ public function removeIf($key, $value): bool
+ {
+ if ($this->get($key) === $value) {
+ unset($this[$key]);
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Replaces the entry for the specified key only if it is currently mapped
+ * to some value.
+ *
+ * @param mixed $key The key to replace.
+ * @param mixed $value The value to set at `$key`.
+ *
+ * @return mixed|null the previous value associated with key, or `null` if
+ * there was no mapping for `$key`.
+ */
+ public function replace($key, $value)
+ {
+ $currentValue = $this->get($key);
+
+ if ($this->containsKey($key)) {
+ $this[$key] = $value;
+ }
+
+ return $currentValue;
+ }
+
+ /**
+ * Replaces the entry for the specified key only if currently mapped to the
+ * specified value.
+ *
+ * This performs a strict type check on the value.
+ *
+ * @param mixed $key The key to remove from the map.
+ * @param mixed $oldValue The value to match.
+ * @param mixed $newValue The value to use as a replacement.
+ *
+ * @return bool true if the value was replaced.
+ */
+ public function replaceIf($key, $oldValue, $newValue): bool
+ {
+ if ($this->get($key) === $oldValue) {
+ $this[$key] = $newValue;
+
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/vendor/ramsey/collection/src/Map/AbstractTypedMap.php b/vendor/ramsey/collection/src/Map/AbstractTypedMap.php
new file mode 100644
index 000000000..80cec2e22
--- /dev/null
+++ b/vendor/ramsey/collection/src/Map/AbstractTypedMap.php
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * This file is part of the ramsey/collection library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Collection\Map;
+
+use Ramsey\Collection\Exception\InvalidArgumentException;
+use Ramsey\Collection\Tool\TypeTrait;
+use Ramsey\Collection\Tool\ValueToStringTrait;
+
+/**
+ * This class provides a basic implementation of `TypedMapInterface`, to
+ * minimize the effort required to implement this interface.
+ */
+abstract class AbstractTypedMap extends AbstractMap implements TypedMapInterface
+{
+ use TypeTrait;
+ use ValueToStringTrait;
+
+ /**
+ * Sets the given value to the given offset in the map.
+ *
+ * @param mixed $offset The offset to set.
+ * @param mixed $value The value to set at the given offset.
+ *
+ * @throws InvalidArgumentException if the offset or value do not match the
+ * expected types.
+ */
+ public function offsetSet($offset, $value): void
+ {
+ if ($this->checkType($this->getKeyType(), $offset) === false) {
+ throw new InvalidArgumentException(
+ 'Key must be of type ' . $this->getKeyType() . '; key is '
+ . $this->toolValueToString($offset)
+ );
+ }
+
+ if ($this->checkType($this->getValueType(), $value) === false) {
+ throw new InvalidArgumentException(
+ 'Value must be of type ' . $this->getValueType() . '; value is '
+ . $this->toolValueToString($value)
+ );
+ }
+
+ parent::offsetSet($offset, $value);
+ }
+}
diff --git a/vendor/ramsey/collection/src/Map/AssociativeArrayMap.php b/vendor/ramsey/collection/src/Map/AssociativeArrayMap.php
new file mode 100644
index 000000000..f97e21728
--- /dev/null
+++ b/vendor/ramsey/collection/src/Map/AssociativeArrayMap.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * This file is part of the ramsey/collection library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Collection\Map;
+
+/**
+ * `AssociativeArrayMap` represents a standard associative array object.
+ */
+class AssociativeArrayMap extends AbstractMap
+{
+}
diff --git a/vendor/ramsey/collection/src/Map/MapInterface.php b/vendor/ramsey/collection/src/Map/MapInterface.php
new file mode 100644
index 000000000..500bdb2d0
--- /dev/null
+++ b/vendor/ramsey/collection/src/Map/MapInterface.php
@@ -0,0 +1,138 @@
+<?php
+
+/**
+ * This file is part of the ramsey/collection library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Collection\Map;
+
+use Ramsey\Collection\ArrayInterface;
+
+/**
+ * An object that maps keys to values.
+ *
+ * A map cannot contain duplicate keys; each key can map to at most one value.
+ */
+interface MapInterface extends ArrayInterface
+{
+ /**
+ * Returns `true` if this map contains a mapping for the specified key.
+ *
+ * @param mixed $key The key to check in the map.
+ */
+ public function containsKey($key): bool;
+
+ /**
+ * Returns `true` if this map maps one or more keys to the specified value.
+ *
+ * This performs a strict type check on the value.
+ *
+ * @param mixed $value The value to check in the map.
+ */
+ public function containsValue($value): bool;
+
+ /**
+ * Return an array of the keys contained in this map.
+ *
+ * @return mixed[]
+ */
+ public function keys(): array;
+
+ /**
+ * Returns the value to which the specified key is mapped, `null` if this
+ * map contains no mapping for the key, or (optionally) `$defaultValue` if
+ * this map contains no mapping for the key.
+ *
+ * @param mixed $key The key to return from the map.
+ * @param mixed $defaultValue The default value to use if `$key` is not found.
+ *
+ * @return mixed|null the value or `null` if the key could not be found.
+ */
+ public function get($key, $defaultValue = null);
+
+ /**
+ * Associates the specified value with the specified key in this map.
+ *
+ * If the map previously contained a mapping for the key, the old value is
+ * replaced by the specified value.
+ *
+ * @param mixed $key The key to put or replace in the map.
+ * @param mixed $value The value to store at `$key`.
+ *
+ * @return mixed|null the previous value associated with key, or `null` if
+ * there was no mapping for `$key`.
+ */
+ public function put($key, $value);
+
+ /**
+ * Associates the specified value with the specified key in this map only if
+ * it is not already set.
+ *
+ * If there is already a value associated with `$key`, this returns that
+ * value without replacing it.
+ *
+ * @param mixed $key The key to put in the map.
+ * @param mixed $value The value to store at `$key`.
+ *
+ * @return mixed|null the previous value associated with key, or `null` if
+ * there was no mapping for `$key`.
+ */
+ public function putIfAbsent($key, $value);
+
+ /**
+ * Removes the mapping for a key from this map if it is present.
+ *
+ * @param mixed $key The key to remove from the map.
+ *
+ * @return mixed|null the previous value associated with key, or `null` if
+ * there was no mapping for `$key`.
+ */
+ public function remove($key);
+
+ /**
+ * Removes the entry for the specified key only if it is currently mapped to
+ * the specified value.
+ *
+ * This performs a strict type check on the value.
+ *
+ * @param mixed $key The key to remove from the map.
+ * @param mixed $value The value to match.
+ *
+ * @return bool true if the value was removed.
+ */
+ public function removeIf($key, $value): bool;
+
+ /**
+ * Replaces the entry for the specified key only if it is currently mapped
+ * to some value.
+ *
+ * @param mixed $key The key to replace.
+ * @param mixed $value The value to set at `$key`.
+ *
+ * @return mixed|null the previous value associated with key, or `null` if
+ * there was no mapping for `$key`.
+ */
+ public function replace($key, $value);
+
+ /**
+ * Replaces the entry for the specified key only if currently mapped to the
+ * specified value.
+ *
+ * This performs a strict type check on the value.
+ *
+ * @param mixed $key The key to remove from the map.
+ * @param mixed $oldValue The value to match.
+ * @param mixed $newValue The value to use as a replacement.
+ *
+ * @return bool true if the value was replaced.
+ */
+ public function replaceIf($key, $oldValue, $newValue): bool;
+}
diff --git a/vendor/ramsey/collection/src/Map/NamedParameterMap.php b/vendor/ramsey/collection/src/Map/NamedParameterMap.php
new file mode 100644
index 000000000..7adfa0afd
--- /dev/null
+++ b/vendor/ramsey/collection/src/Map/NamedParameterMap.php
@@ -0,0 +1,118 @@
+<?php
+
+/**
+ * This file is part of the ramsey/collection library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Collection\Map;
+
+use Ramsey\Collection\Exception\InvalidArgumentException;
+use Ramsey\Collection\Tool\TypeTrait;
+use Ramsey\Collection\Tool\ValueToStringTrait;
+
+use function array_combine;
+use function array_key_exists;
+use function is_int;
+
+/**
+ * `NamedParameterMap` represents a mapping of values to a set of named keys
+ * that may optionally be typed
+ */
+class NamedParameterMap extends AbstractMap
+{
+ use TypeTrait;
+ use ValueToStringTrait;
+
+ /**
+ * Named parameters defined for this map.
+ *
+ * @var array<mixed, string>
+ */
+ protected $namedParameters;
+
+ /**
+ * Constructs a new `NamedParameterMap`.
+ *
+ * @param array<mixed, string> $namedParameters The named parameters defined for this map.
+ * @param mixed[] $data An initial set of data to set on this map.
+ */
+ public function __construct(array $namedParameters, array $data = [])
+ {
+ $this->namedParameters = $this->filterNamedParameters($namedParameters);
+ parent::__construct($data);
+ }
+
+ /**
+ * Returns named parameters set for this `NamedParameterMap`.
+ *
+ * @return array<mixed, string>
+ */
+ public function getNamedParameters(): array
+ {
+ return $this->namedParameters;
+ }
+
+ /**
+ * Sets the given value to the given offset in the map.
+ *
+ * @param mixed $offset The offset to set.
+ * @param mixed $value The value to set at the given offset.
+ *
+ * @throws InvalidArgumentException if the offset provided is not a
+ * defined named parameter, or if the value is not of the type defined
+ * for the given named parameter.
+ */
+ public function offsetSet($offset, $value): void
+ {
+ if (!array_key_exists($offset, $this->namedParameters)) {
+ throw new InvalidArgumentException(
+ 'Attempting to set value for unconfigured parameter \''
+ . $offset . '\''
+ );
+ }
+
+ if ($this->checkType($this->namedParameters[$offset], $value) === false) {
+ throw new InvalidArgumentException(
+ 'Value for \'' . $offset . '\' must be of type '
+ . $this->namedParameters[$offset] . '; value is '
+ . $this->toolValueToString($value)
+ );
+ }
+
+ $this->data[$offset] = $value;
+ }
+
+ /**
+ * Given an array of named parameters, constructs a proper mapping of
+ * named parameters to types.
+ *
+ * @param array<mixed, string> $namedParameters The named parameters to filter.
+ *
+ * @return array<mixed, string>
+ */
+ protected function filterNamedParameters(array $namedParameters): array
+ {
+ $names = [];
+ $types = [];
+
+ foreach ($namedParameters as $key => $value) {
+ if (is_int($key)) {
+ $names[] = (string) $value;
+ $types[] = 'mixed';
+ } else {
+ $names[] = $key;
+ $types[] = (string) $value;
+ }
+ }
+
+ return array_combine($names, $types) ?: [];
+ }
+}
diff --git a/vendor/ramsey/collection/src/Map/TypedMap.php b/vendor/ramsey/collection/src/Map/TypedMap.php
new file mode 100644
index 000000000..84d075f80
--- /dev/null
+++ b/vendor/ramsey/collection/src/Map/TypedMap.php
@@ -0,0 +1,137 @@
+<?php
+
+/**
+ * This file is part of the ramsey/collection library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Collection\Map;
+
+use Ramsey\Collection\Tool\TypeTrait;
+
+/**
+ * A `TypedMap` represents a map of elements where key and value are typed.
+ *
+ * Each element is identified by a key with defined type and a value of defined
+ * type. The keys of the map must be unique. The values on the map can be=
+ * repeated but each with its own different key.
+ *
+ * The most common case is to use a string type key, but it's not limited to
+ * this type of keys.
+ *
+ * This is a direct implementation of `TypedMapInterface`, provided for the sake
+ * of convenience.
+ *
+ * Example usage:
+ *
+ * ```php
+ * $map = new TypedMap('string', Foo::class);
+ * $map['x'] = new Foo();
+ * foreach ($map as $key => $value) {
+ * // do something with $key, it will be a Foo::class
+ * }
+ *
+ * // this will throw an exception since key must be string
+ * $map[10] = new Foo();
+ *
+ * // this will throw an exception since value must be a Foo
+ * $map['bar'] = 'bar';
+ *
+ * // initialize map with contents
+ * $map = new TypedMap('string', Foo::class, [
+ * new Foo(), new Foo(), new Foo()
+ * ]);
+ * ```
+ *
+ * It is preferable to subclass `AbstractTypedMap` to create your own typed map
+ * implementation:
+ *
+ * ```php
+ * class FooTypedMap extends AbstractTypedMap
+ * {
+ * public function getKeyType()
+ * {
+ * return 'int';
+ * }
+ *
+ * public function getValueType()
+ * {
+ * return Foo::class;
+ * }
+ * }
+ * ```
+ *
+ * … but you also may use the `TypedMap` class:
+ *
+ * ```php
+ * class FooTypedMap extends TypedMap
+ * {
+ * public function __constructor(array $data = [])
+ * {
+ * parent::__construct('int', Foo::class, $data);
+ * }
+ * }
+ * ```
+ */
+class TypedMap extends AbstractTypedMap
+{
+ use TypeTrait;
+
+ /**
+ * The data type of keys stored in this collection.
+ *
+ * A map key's type is immutable once it is set. For this reason, this
+ * property is set private.
+ *
+ * @var string data type of the map key.
+ */
+ private $keyType;
+
+ /**
+ * The data type of values stored in this collection.
+ *
+ * A map values's type is immutable once it is set. For this reason, this
+ * property is set private.
+ *
+ * @var string data type of the map value.
+ */
+ private $valueType;
+
+ /**
+ * Constructs a map object of the specified key and value types,
+ * optionally with the specified data.
+ *
+ * @param string $keyType The data type of the map's keys.
+ * @param string $valueType The data type of the map's values.
+ * @param mixed[] $data The initial data to set for this map.
+ */
+ public function __construct(string $keyType, string $valueType, array $data = [])
+ {
+ $this->keyType = $keyType;
+ $this->valueType = $valueType;
+ parent::__construct($data);
+ }
+
+ /**
+ * Return the type used on the key.
+ */
+ public function getKeyType(): string
+ {
+ return $this->keyType;
+ }
+
+ /**
+ * Return the type forced on the values.
+ */
+ public function getValueType(): string
+ {
+ return $this->valueType;
+ }
+}
diff --git a/vendor/ramsey/collection/src/Map/TypedMapInterface.php b/vendor/ramsey/collection/src/Map/TypedMapInterface.php
new file mode 100644
index 000000000..54c783695
--- /dev/null
+++ b/vendor/ramsey/collection/src/Map/TypedMapInterface.php
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * This file is part of the ramsey/collection library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Collection\Map;
+
+/**
+ * A `TypedMapInterface` represents a map of elements where key and value are
+ * typed.
+ */
+interface TypedMapInterface extends MapInterface
+{
+ /**
+ * Return the type used on the key.
+ */
+ public function getKeyType(): string;
+
+ /**
+ * Return the type forced on the values.
+ */
+ public function getValueType(): string;
+}