| 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 <[email protected]> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace Ramsey\Collection\Map; |
| 16 |
|
| 17 |
use Ramsey\Collection\Exception\InvalidArgumentException; |
| 18 |
use Ramsey\Collection\Tool\TypeTrait; |
| 19 |
use Ramsey\Collection\Tool\ValueToStringTrait; |
| 20 |
|
| 21 |
use function array_combine; |
| 22 |
use function array_key_exists; |
| 23 |
use function is_int; |
| 24 |
use function var_export; |
| 25 |
|
| 26 |
/** |
| 27 |
* `NamedParameterMap` represents a mapping of values to a set of named keys |
| 28 |
* that may optionally be typed |
| 29 |
* |
| 30 |
* @extends AbstractMap<mixed> |
| 31 |
*/ |
| 32 |
class NamedParameterMap extends AbstractMap |
| 33 |
{ |
| 34 |
use TypeTrait; |
| 35 |
use ValueToStringTrait; |
| 36 |
|
| 37 |
/** |
| 38 |
* Named parameters defined for this map. |
| 39 |
* |
| 40 |
* @var array<string, string> |
| 41 |
*/ |
| 42 |
protected array $namedParameters; |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructs a new `NamedParameterMap`. |
| 46 |
* |
| 47 |
* @param array<array-key, string> $namedParameters The named parameters defined for this map. |
| 48 |
* @param array<array-key, mixed> $data An initial set of data to set on this map. |
| 49 |
*/ |
| 50 |
public function __construct(array $namedParameters, array $data = []) |
| 51 |
{ |
| 52 |
$this->namedParameters = $this->filterNamedParameters($namedParameters); |
| 53 |
parent::__construct($data); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns named parameters set for this `NamedParameterMap`. |
| 58 |
* |
| 59 |
* @return array<string, string> |
| 60 |
*/ |
| 61 |
public function getNamedParameters(): array |
| 62 |
{ |
| 63 |
return $this->namedParameters; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @inheritDoc |
| 68 |
*/ |
| 69 |
public function offsetSet($offset, $value): void |
| 70 |
{ |
| 71 |
if ($offset === null) { |
| 72 |
throw new InvalidArgumentException( |
| 73 |
'Map elements are key/value pairs; a key must be provided for ' |
| 74 |
. 'value ' . var_export($value, true), |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
if (!array_key_exists($offset, $this->namedParameters)) { |
| 79 |
throw new InvalidArgumentException( |
| 80 |
'Attempting to set value for unconfigured parameter \'' |
| 81 |
. $offset . '\'', |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
if ($this->checkType($this->namedParameters[$offset], $value) === false) { |
| 86 |
throw new InvalidArgumentException( |
| 87 |
'Value for \'' . $offset . '\' must be of type ' |
| 88 |
. $this->namedParameters[$offset] . '; value is ' |
| 89 |
. $this->toolValueToString($value), |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
$this->data[$offset] = $value; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Given an array of named parameters, constructs a proper mapping of |
| 98 |
* named parameters to types. |
| 99 |
* |
| 100 |
* @param array<array-key, string> $namedParameters The named parameters to filter. |
| 101 |
* |
| 102 |
* @return array<string, string> |
| 103 |
*/ |
| 104 |
protected function filterNamedParameters(array $namedParameters): array |
| 105 |
{ |
| 106 |
$names = []; |
| 107 |
$types = []; |
| 108 |
|
| 109 |
foreach ($namedParameters as $key => $value) { |
| 110 |
if (is_int($key)) { |
| 111 |
$names[] = $value; |
| 112 |
$types[] = 'mixed'; |
| 113 |
} else { |
| 114 |
$names[] = $key; |
| 115 |
$types[] = $value; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return array_combine($names, $types) ?: []; |
| 120 |
} |
| 121 |
} |
| 122 |
|