-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKeyValueCollection.php
More file actions
142 lines (125 loc) · 3.66 KB
/
KeyValueCollection.php
File metadata and controls
142 lines (125 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Enm\JsonApi\Model\Common;
/**
* @author Philipp Marien <marien@eosnewmedia.de>
*/
class KeyValueCollection extends AbstractCollection implements KeyValueCollectionInterface
{
/**
* @var array
*/
private $keyMap = [];
/**
* @param array $data
*/
public function __construct(array $data = [])
{
parent::__construct();
foreach ($data as $key => $value) {
$this->set($key, $value);
}
}
/**
* @param string $key
*
* @return bool
*/
public function has(string $key): bool
{
return array_key_exists(strtolower($key), $this->keyMap) &&
array_key_exists($this->keyMap[strtolower($key)], $this->collection);
}
/**
* @param string $key
*
* @return mixed
* @throws \InvalidArgumentException
*/
public function getRequired(string $key)
{
if (!$this->has($key)) {
throw new \InvalidArgumentException('Element ' . $key . ' does not exist');
}
return $this->collection[$this->keyMap[strtolower($key)]];
}
/**
* @param string $key
* @param mixed $defaultValue
* @return mixed
*/
public function getOptional(string $key, $defaultValue = null)
{
return $this->has($key) ? $this->collection[$this->keyMap[strtolower($key)]] : $defaultValue;
}
/**
* Returns a (sub) collection for an array value from the current collection.
* If the same sub collection is requested multiple times, each time the same object must be returned
*
* @param string $key
* @param bool $required
* @return KeyValueCollectionInterface
* @throws \InvalidArgumentException
*/
public function createSubCollection(string $key, bool $required = true): KeyValueCollectionInterface
{
$data = $required ? $this->getRequired($key) : $this->getOptional($key, []);
if (!\is_array($data)) {
throw new \InvalidArgumentException('Element ' . $key . ' have to be an array to use it as collection.');
}
return new self($data);
}
/**
* @param array $data
* @param bool $overwrite
* @return KeyValueCollectionInterface
*/
public function merge(array $data, bool $overwrite = true): KeyValueCollectionInterface
{
foreach ($data as $key => $value) {
if ($overwrite || $this->getOptional($key) === null) {
$this->set($key, $value);
}
}
return $this;
}
/**
* @param KeyValueCollectionInterface $collection
* @param bool $overwrite
* @return KeyValueCollectionInterface
*/
public function mergeCollection(
KeyValueCollectionInterface $collection,
bool $overwrite = true
): KeyValueCollectionInterface {
$this->merge($collection->all(), $overwrite);
return $this;
}
/**
* @param string $key
* @param mixed $value
*
* @return KeyValueCollectionInterface
*/
public function set(string $key, $value): KeyValueCollectionInterface
{
$this->keyMap[strtolower($key)] = $key;
if ($value instanceof KeyValueCollectionInterface) {
$value = $value->all();
}
$this->collection[$key] = $value;
return $this;
}
/**
* @param string $key
*
* @return KeyValueCollectionInterface
*/
public function remove(string $key): KeyValueCollectionInterface
{
if ($this->has($key)) {
unset($this->collection[$this->keyMap[strtolower($key)]], $this->keyMap[strtolower($key)]);
}
return $this;
}
}