1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Stratadox\IdentityMap; |
5
|
|
|
|
6
|
|
|
use function get_class; |
7
|
|
|
use function in_array; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Whitelist for the Identity Map. |
11
|
|
|
* |
12
|
|
|
* Used for whitelisting entities while loading objects. |
13
|
|
|
* |
14
|
|
|
* @see IdentityMap |
15
|
|
|
* |
16
|
|
|
* @author Stratadox |
17
|
|
|
*/ |
18
|
|
|
final class Whitelist implements MapsObjectsByIdentity |
19
|
|
|
{ |
20
|
|
|
private $allow; |
21
|
|
|
private $map; |
22
|
|
|
|
23
|
|
|
private function __construct(array $allow, MapsObjectsByIdentity $map) |
24
|
|
|
{ |
25
|
|
|
$this->allow = $allow; |
26
|
|
|
$this->map = $map; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructs a whitelist for the identity map. |
31
|
|
|
* |
32
|
|
|
* @param MapsObjectsByIdentity $mapped The actual identity map. |
33
|
|
|
* @param string ...$allowedClasses The whitelisted classes. |
34
|
|
|
* @return MapsObjectsByIdentity The wrapped identity map. |
35
|
|
|
*/ |
36
|
|
|
public static function forThe( |
37
|
|
|
MapsObjectsByIdentity $mapped, |
38
|
|
|
string ...$allowedClasses |
39
|
|
|
): MapsObjectsByIdentity { |
40
|
|
|
$alreadyMapped = $mapped->classes(); |
41
|
|
|
foreach ($alreadyMapped as $class) { |
42
|
|
|
$mapped = Whitelist::purgeIfNotIn($allowedClasses, $mapped, $class); |
43
|
|
|
} |
44
|
|
|
return new Whitelist($allowedClasses, $mapped); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Whitelists the given classes in the identity map. |
49
|
|
|
* |
50
|
|
|
* @param string ...$classes The whitelisted classes. |
51
|
|
|
* @return MapsObjectsByIdentity The wrapped identity map. |
52
|
|
|
*/ |
53
|
|
|
public static function the(string ...$classes): MapsObjectsByIdentity |
54
|
|
|
{ |
55
|
|
|
return new Whitelist($classes, IdentityMap::startEmpty()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @inheritdoc */ |
59
|
|
|
public function has(string $class, string $id): bool |
60
|
|
|
{ |
61
|
|
|
return $this->map->has($class, $id); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @inheritdoc */ |
65
|
|
|
public function hasThe(object $object): bool |
66
|
|
|
{ |
67
|
|
|
return $this->map->hasThe($object); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
71
|
|
|
public function get(string $class, string $id): object |
72
|
|
|
{ |
73
|
|
|
return $this->map->get($class, $id); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @inheritdoc */ |
77
|
|
|
public function idOf(object $object): string |
78
|
|
|
{ |
79
|
|
|
return $this->map->idOf($object); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** @inheritdoc */ |
83
|
|
|
public function add(string $id, object $object): MapsObjectsByIdentity |
84
|
|
|
{ |
85
|
|
|
if (!in_array(get_class($object), $this->allow)) { |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
return $this->newMap($this->map->add($id, $object)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @inheritdoc */ |
92
|
|
|
public function remove(string $class, string $id): MapsObjectsByIdentity |
93
|
|
|
{ |
94
|
|
|
return $this->newMap($this->map->remove($class, $id)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** @inheritdoc */ |
98
|
|
|
public function removeThe(object $object): MapsObjectsByIdentity |
99
|
|
|
{ |
100
|
|
|
return $this->newMap($this->map->removeThe($object)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** @inheritdoc */ |
104
|
|
|
public function removeAllObjectsOfThe(string $class): MapsObjectsByIdentity |
105
|
|
|
{ |
106
|
|
|
if (!in_array($class, $this->allow)) { |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
return $this->newMap($this->map->removeAllObjectsOfThe($class)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @inheritdoc */ |
113
|
|
|
public function classes(): array |
114
|
|
|
{ |
115
|
|
|
return $this->map->classes(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private static function purgeIfNotIn( |
119
|
|
|
array $allowedClasses, |
120
|
|
|
MapsObjectsByIdentity $mapped, |
121
|
|
|
string $class |
122
|
|
|
): MapsObjectsByIdentity { |
123
|
|
|
if (in_array($class, $allowedClasses)) { |
124
|
|
|
return $mapped; |
125
|
|
|
} |
126
|
|
|
return $mapped->removeAllObjectsOfThe($class); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function newMap(MapsObjectsByIdentity $map): MapsObjectsByIdentity |
130
|
|
|
{ |
131
|
|
|
return new Whitelist($this->allow, $map); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|