media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
collection
/
src
/
Map
/
NamedParameterMap.php
NamedParameterMap.php in Media Cloud Sync 1.4.1, 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\GCP\Ramsey\Collection\Map; |
| 14 | |
| 15 | use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\InvalidArgumentException; |
| 16 | use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Tool\TypeTrait; |
| 17 | use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Tool\ValueToStringTrait; |
| 18 | use function array_combine; |
| 19 | use function array_key_exists; |
| 20 | use function is_int; |
| 21 | /** |
| 22 | * `NamedParameterMap` represents a mapping of values to a set of named keys |
| 23 | * that may optionally be typed |
| 24 | * |
| 25 | * @extends AbstractMap<string, mixed> |
| 26 | */ |
| 27 | class NamedParameterMap extends AbstractMap |
| 28 | { |
| 29 | use TypeTrait; |
| 30 | use ValueToStringTrait; |
| 31 | /** |
| 32 | * Named parameters defined for this map. |
| 33 | * |
| 34 | * @var array<string, string> |
| 35 | */ |
| 36 | private readonly array $namedParameters; |
| 37 | /** |
| 38 | * Constructs a new `NamedParameterMap`. |
| 39 | * |
| 40 | * @param array<array-key, string> $namedParameters The named parameters defined for this map. |
| 41 | * @param array<string, mixed> $data An initial set of data to set on this map. |
| 42 | */ |
| 43 | public function __construct(array $namedParameters, array $data = []) |
| 44 | { |
| 45 | $this->namedParameters = $this->filterNamedParameters($namedParameters); |
| 46 | parent::__construct($data); |
| 47 | } |
| 48 | /** |
| 49 | * Returns named parameters set for this `NamedParameterMap`. |
| 50 | * |
| 51 | * @return array<string, string> |
| 52 | */ |
| 53 | public function getNamedParameters() : array |
| 54 | { |
| 55 | return $this->namedParameters; |
| 56 | } |
| 57 | public function offsetSet(mixed $offset, mixed $value) : void |
| 58 | { |
| 59 | if (!array_key_exists($offset, $this->namedParameters)) { |
| 60 | throw new InvalidArgumentException('Attempting to set value for unconfigured parameter \'' . $this->toolValueToString($offset) . '\''); |
| 61 | } |
| 62 | if ($this->checkType($this->namedParameters[$offset], $value) === \false) { |
| 63 | throw new InvalidArgumentException('Value for \'' . $offset . '\' must be of type ' . $this->namedParameters[$offset] . '; value is ' . $this->toolValueToString($value)); |
| 64 | } |
| 65 | $this->data[$offset] = $value; |
| 66 | } |
| 67 | /** |
| 68 | * Given an array of named parameters, constructs a proper mapping of |
| 69 | * named parameters to types. |
| 70 | * |
| 71 | * @param array<array-key, string> $namedParameters The named parameters to filter. |
| 72 | * |
| 73 | * @return array<string, string> |
| 74 | */ |
| 75 | protected function filterNamedParameters(array $namedParameters) : array |
| 76 | { |
| 77 | $names = []; |
| 78 | $types = []; |
| 79 | foreach ($namedParameters as $key => $value) { |
| 80 | if (is_int($key)) { |
| 81 | $names[] = $value; |
| 82 | $types[] = 'mixed'; |
| 83 | } else { |
| 84 | $names[] = $key; |
| 85 | $types[] = $value; |
| 86 | } |
| 87 | } |
| 88 | return array_combine($names, $types) ?: []; |
| 89 | } |
| 90 | } |
| 91 |