1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Daikon\DataStructure; |
5
|
|
|
|
6
|
|
|
use Ds\Map; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Iterator; |
9
|
|
|
|
10
|
|
|
trait TypedMapTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Map internal map to store items |
14
|
|
|
*/ |
15
|
|
|
private $compositeMap; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string[] fully qualified class name of acceptable types |
19
|
|
|
*/ |
20
|
|
|
private $itemFqcns; |
21
|
|
|
|
22
|
2 |
|
public function has(string $key): bool |
23
|
|
|
{ |
24
|
2 |
|
return $this->compositeMap->hasKey($key); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @throws \OutOfBoundsException |
29
|
|
|
*/ |
30
|
6 |
|
public function get(string $key) |
31
|
|
|
{ |
32
|
6 |
|
return $this->compositeMap->get($key); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws InvalidArgumentException |
37
|
|
|
*/ |
38
|
2 |
|
public function set($key, $item): self |
39
|
|
|
{ |
40
|
2 |
|
$this->assertItemType($item); |
41
|
1 |
|
$copy = clone $this; |
42
|
1 |
|
$copy->compositeMap->put($key, $item); |
43
|
1 |
|
return $copy; |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
public function count(): int |
47
|
|
|
{ |
48
|
3 |
|
return count($this->compositeMap); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function toArray(): array |
52
|
|
|
{ |
53
|
1 |
|
return $this->compositeMap->toArray(); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function isEmpty(): bool |
57
|
|
|
{ |
58
|
1 |
|
return $this->compositeMap->isEmpty(); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function getIterator(): Iterator |
62
|
|
|
{ |
63
|
1 |
|
return $this->compositeMap->getIterator(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function getItemFqcn(): array |
67
|
|
|
{ |
68
|
1 |
|
return $this->itemFqcns; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws \OutOfBoundsException |
73
|
|
|
*/ |
74
|
3 |
|
public function __get(string $key) |
75
|
|
|
{ |
76
|
3 |
|
return $this->get($key); |
77
|
|
|
} |
78
|
|
|
|
79
|
18 |
|
private function init(iterable $items, $itemFqcns): void |
|
|
|
|
80
|
|
|
{ |
81
|
18 |
|
$this->itemFqcns = (array)$itemFqcns; |
82
|
18 |
|
foreach ($items as $key => $item) { |
83
|
16 |
|
$this->assertItemKey($key); |
84
|
14 |
|
$this->assertItemType($item); |
85
|
|
|
} |
86
|
16 |
|
$this->compositeMap = new Map($items); |
87
|
16 |
|
} |
88
|
|
|
|
89
|
16 |
View Code Duplication |
private function assertItemKey($key): void |
|
|
|
|
90
|
|
|
{ |
91
|
16 |
|
if (!is_string($key)) { |
92
|
2 |
|
throw new InvalidArgumentException(sprintf( |
93
|
2 |
|
'Invalid item key given to %s. Expected string but was given %s.', |
94
|
2 |
|
static::CLASS, |
95
|
2 |
|
is_object($key) ? get_class($key) : @gettype($key) |
96
|
|
|
)); |
97
|
|
|
} |
98
|
14 |
|
} |
99
|
|
|
|
100
|
14 |
|
private function assertItemType($item): void |
101
|
|
|
{ |
102
|
14 |
|
$itemIsValid = false; |
103
|
14 |
|
foreach ($this->itemFqcns as $fqcn) { |
104
|
14 |
|
if (is_a($item, $fqcn)) { |
105
|
14 |
|
$itemIsValid = true; |
106
|
14 |
|
break; |
107
|
|
|
} |
108
|
|
|
} |
109
|
14 |
|
if (!$itemIsValid) { |
110
|
1 |
|
throw new InvalidArgumentException(sprintf( |
111
|
1 |
|
'Invalid item type given to %s. Expected one of %s but was given %s.', |
112
|
1 |
|
static::class, |
113
|
1 |
|
implode(', ', $this->itemFqcns), |
114
|
1 |
|
is_object($item) ? get_class($item) : @gettype($item) |
115
|
|
|
)); |
116
|
|
|
} |
117
|
14 |
|
} |
118
|
|
|
|
119
|
1 |
|
public function __clone() |
120
|
|
|
{ |
121
|
1 |
|
$this->compositeMap = new Map($this->compositeMap->toArray()); |
122
|
1 |
|
} |
123
|
|
|
} |
124
|
|
|
|