media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
collection
/
src
/
Map
/
NamedParameterMap.php
NamedParameterMap.php in Media Cloud Sync 1.0.2, at includes/sdk/google/ramsey/collection/src/Map/NamedParameterMap.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the ramsey/collection library |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | * |
| 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\Ramsey\Collection\Map; |
| 14 | |
| 15 | use Dudlewebs\WPMCS\Ramsey\Collection\Exception\InvalidArgumentException; |
| 16 | use Dudlewebs\WPMCS\Ramsey\Collection\Tool\TypeTrait; |
| 17 | use Dudlewebs\WPMCS\Ramsey\Collection\Tool\ValueToStringTrait; |
| 18 | use function array_combine; |
| 19 | use function array_key_exists; |
| 20 | use function is_int; |
| 21 | use function var_export; |
| 22 | /** |
| 23 | * `NamedParameterMap` represents a mapping of values to a set of named keys |
| 24 | * that may optionally be typed |
| 25 | * |
| 26 | * @extends AbstractMap<mixed> |
| 27 | */ |
| 28 | class NamedParameterMap extends AbstractMap |
| 29 | { |
| 30 | use TypeTrait; |
| 31 | use ValueToStringTrait; |
| 32 | /** |
| 33 | * Named parameters defined for this map. |
| 34 | * |
| 35 | * @var array<string, string> |
| 36 | */ |
| 37 | protected array $namedParameters; |
| 38 | /** |
| 39 | * Constructs a new `NamedParameterMap`. |
| 40 | * |
| 41 | * @param array<array-key, string> $namedParameters The named parameters defined for this map. |
| 42 | * @param array<array-key, mixed> $data An initial set of data to set on this map. |
| 43 | */ |
| 44 | public function __construct(array $namedParameters, array $data = []) |
| 45 | { |
| 46 | $this->namedParameters = $this->filterNamedParameters($namedParameters); |
| 47 | parent::__construct($data); |
| 48 | } |
| 49 | /** |
| 50 | * Returns named parameters set for this `NamedParameterMap`. |
| 51 | * |
| 52 | * @return array<string, string> |
| 53 | */ |
| 54 | public function getNamedParameters(): array |
| 55 | { |
| 56 | return $this->namedParameters; |
| 57 | } |
| 58 | /** |
| 59 | * @inheritDoc |
| 60 | */ |
| 61 | public function offsetSet($offset, $value): void |
| 62 | { |
| 63 | if ($offset === null) { |
| 64 | throw new InvalidArgumentException('Map elements are key/value pairs; a key must be provided for ' . 'value ' . var_export($value, \true)); |
| 65 | } |
| 66 | if (!array_key_exists($offset, $this->namedParameters)) { |
| 67 | throw new InvalidArgumentException('Attempting to set value for unconfigured parameter \'' . $offset . '\''); |
| 68 | } |
| 69 | if ($this->checkType($this->namedParameters[$offset], $value) === \false) { |
| 70 | throw new InvalidArgumentException('Value for \'' . $offset . '\' must be of type ' . $this->namedParameters[$offset] . '; value is ' . $this->toolValueToString($value)); |
| 71 | } |
| 72 | $this->data[$offset] = $value; |
| 73 | } |
| 74 | /** |
| 75 | * Given an array of named parameters, constructs a proper mapping of |
| 76 | * named parameters to types. |
| 77 | * |
| 78 | * @param array<array-key, string> $namedParameters The named parameters to filter. |
| 79 | * |
| 80 | * @return array<string, string> |
| 81 | */ |
| 82 | protected function filterNamedParameters(array $namedParameters): array |
| 83 | { |
| 84 | $names = []; |
| 85 | $types = []; |
| 86 | foreach ($namedParameters as $key => $value) { |
| 87 | if (is_int($key)) { |
| 88 | $names[] = $value; |
| 89 | $types[] = 'mixed'; |
| 90 | } else { |
| 91 | $names[] = $key; |
| 92 | $types[] = $value; |
| 93 | } |
| 94 | } |
| 95 | return array_combine($names, $types) ?: []; |
| 96 | } |
| 97 | } |
| 98 |