PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / trunk
ShareThis Dashboard for Google Analytics vtrunk
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / lib / analytics-admin / vendor / ramsey / collection / src / Map / NamedParameterMap.php
googleanalytics / lib / analytics-admin / vendor / ramsey / collection / src / Map Last commit date
AbstractMap.php 3 years ago AbstractTypedMap.php 3 years ago AssociativeArrayMap.php 3 years ago MapInterface.php 3 years ago NamedParameterMap.php 3 years ago TypedMap.php 3 years ago TypedMapInterface.php 3 years ago
NamedParameterMap.php
121 lines
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
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<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 protected $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<array-key, 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 /**
66 * @inheritDoc
67 */
68 public function offsetSet($offset, $value): void
69 {
70 if ($offset === null) {
71 throw new InvalidArgumentException(
72 'Map elements are key/value pairs; a key must be provided for '
73 . 'value ' . var_export($value, true)
74 );
75 }
76
77 if (!array_key_exists($offset, $this->namedParameters)) {
78 throw new InvalidArgumentException(
79 'Attempting to set value for unconfigured parameter \''
80 . $offset . '\''
81 );
82 }
83
84 if ($this->checkType($this->namedParameters[$offset], $value) === false) {
85 throw new InvalidArgumentException(
86 'Value for \'' . $offset . '\' must be of type '
87 . $this->namedParameters[$offset] . '; value is '
88 . $this->toolValueToString($value)
89 );
90 }
91
92 $this->data[$offset] = $value;
93 }
94
95 /**
96 * Given an array of named parameters, constructs a proper mapping of
97 * named parameters to types.
98 *
99 * @param array<array-key, string> $namedParameters The named parameters to filter.
100 *
101 * @return array<string, string>
102 */
103 protected function filterNamedParameters(array $namedParameters): array
104 {
105 $names = [];
106 $types = [];
107
108 foreach ($namedParameters as $key => $value) {
109 if (is_int($key)) {
110 $names[] = $value;
111 $types[] = 'mixed';
112 } else {
113 $names[] = $key;
114 $types[] = $value;
115 }
116 }
117
118 return array_combine($names, $types) ?: [];
119 }
120 }
121