PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.14
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.14
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor / ramsey / collection / src / Map / NamedParameterMap.php

NamedParameterMap.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.14, at vendor/ramsey/collection/src/Map/NamedParameterMap.php

122 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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