-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMap.php
81 lines (64 loc) · 1.46 KB
/
Map.php
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
<?php
declare(strict_types=1);
namespace Fschmtt\Keycloak\Type;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use OutOfBoundsException;
use Traversable;
/**
* @template T
*
* @implements IteratorAggregate<string, T>
*/
class Map extends Type implements Countable, IteratorAggregate
{
/**
* @param array<string, T> $map
*/
public function __construct(
private array $map = [],
) {}
public function jsonSerialize(): object
{
return (object) $this->map;
}
public function getIterator(): Traversable
{
return new ArrayIterator($this->map);
}
public function count(): int
{
return count($this->map);
}
public function contains(string $key): bool
{
return array_key_exists($key, $this->map);
}
public function get(string $key): mixed
{
if (!$this->contains($key)) {
throw new OutOfBoundsException(sprintf('Key "%s" does not exist in map', $key));
}
return $this->map[$key];
}
public function with(string $key, mixed $value): self
{
$clone = clone $this;
$clone->map[$key] = $value;
return $clone;
}
public function without(string $key): self
{
$clone = clone $this;
unset($clone->map[$key]);
return $clone;
}
/**
* @return array<string, T>
*/
public function getMap(): array
{
return $this->map;
}
}