| 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 |
|
| 25 |
/** |
| 26 |
* `NamedParameterMap` represents a mapping of values to a set of named keys |
| 27 |
* that may optionally be typed |
| 28 |
* |
| 29 |
* @extends AbstractMap<string, mixed> |
| 30 |
*/ |
| 31 |
class NamedParameterMap extends AbstractMap |
| 32 |
{ |
| 33 |
use TypeTrait; |
| 34 |
use ValueToStringTrait; |
| 35 |
|
| 36 |
/** |
| 37 |
* Named parameters defined for this map. |
| 38 |
* |
| 39 |
* @var array<string, string> |
| 40 |
*/ |
| 41 |
private readonly array $namedParameters; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructs a new `NamedParameterMap`. |
| 45 |
* |
| 46 |
* @param array<array-key, string> $namedParameters The named parameters defined for this map. |
| 47 |
* @param array<string, mixed> $data An initial set of data to set on this map. |
| 48 |
*/ |
| 49 |
public function __construct(array $namedParameters, array $data = []) |
| 50 |
{ |
| 51 |
$this->namedParameters = $this->filterNamedParameters($namedParameters); |
| 52 |
parent::__construct($data); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns named parameters set for this `NamedParameterMap`. |
| 57 |
* |
| 58 |
* @return array<string, string> |
| 59 |
*/ |
| 60 |
public function getNamedParameters(): array |
| 61 |
{ |
| 62 |
return $this->namedParameters; |
| 63 |
} |
| 64 |
|
| 65 |
public function offsetSet(mixed $offset, mixed $value): void |
| 66 |
{ |
| 67 |
if (!array_key_exists($offset, $this->namedParameters)) { |
| 68 |
throw new InvalidArgumentException( |
| 69 |
'Attempting to set value for unconfigured parameter \'' |
| 70 |
. $this->toolValueToString($offset) . '\'', |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
if ($this->checkType($this->namedParameters[$offset], $value) === false) { |
| 75 |
throw new InvalidArgumentException( |
| 76 |
'Value for \'' . $offset . '\' must be of type ' |
| 77 |
. $this->namedParameters[$offset] . '; value is ' |
| 78 |
. $this->toolValueToString($value), |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
$this->data[$offset] = $value; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Given an array of named parameters, constructs a proper mapping of |
| 87 |
* named parameters to types. |
| 88 |
* |
| 89 |
* @param array<array-key, string> $namedParameters The named parameters to filter. |
| 90 |
* |
| 91 |
* @return array<string, string> |
| 92 |
*/ |
| 93 |
protected function filterNamedParameters(array $namedParameters): array |
| 94 |
{ |
| 95 |
$names = []; |
| 96 |
$types = []; |
| 97 |
|
| 98 |
foreach ($namedParameters as $key => $value) { |
| 99 |
if (is_int($key)) { |
| 100 |
$names[] = $value; |
| 101 |
$types[] = 'mixed'; |
| 102 |
} else { |
| 103 |
$names[] = $key; |
| 104 |
$types[] = $value; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return array_combine($names, $types) ?: []; |
| 109 |
} |
| 110 |
} |