1 | <?php |
||
16 | final class IdentityMap implements MapsObjectsByIdentity |
||
17 | { |
||
18 | private $objectsBy; |
||
19 | private $entityIdFor; |
||
20 | |||
21 | private function __construct(array $objectsByClassAndId, array $idsByObject) |
||
26 | |||
27 | /** |
||
28 | * Produces a new identity map that contains the objects. |
||
29 | * |
||
30 | * @param object[] $theseObjects The objects to add, as [id => object] |
||
31 | * @return MapsObjectsByIdentity The map of objects. |
||
32 | */ |
||
33 | public static function with(array $theseObjects): MapsObjectsByIdentity |
||
43 | |||
44 | /** |
||
45 | * Produces an empty identity map. |
||
46 | * |
||
47 | * @return MapsObjectsByIdentity The empty map of objects. |
||
48 | */ |
||
49 | public static function startEmpty(): MapsObjectsByIdentity |
||
53 | |||
54 | /** @inheritdoc */ |
||
55 | public function has(string $class, string $id): bool |
||
59 | |||
60 | /** @inheritdoc */ |
||
61 | public function hasThe(object $object): bool |
||
65 | |||
66 | /** @inheritdoc */ |
||
67 | public function get(string $class, string $id): object |
||
72 | |||
73 | /** @inheritdoc */ |
||
74 | public function add(string $id, object $object): MapsObjectsByIdentity |
||
84 | |||
85 | /** @inheritdoc */ |
||
86 | public function remove(string $class, string $id): MapsObjectsByIdentity |
||
97 | |||
98 | /** @inheritdoc */ |
||
99 | public function removeThe(object $object): MapsObjectsByIdentity |
||
103 | |||
104 | /** @inheritdoc */ |
||
105 | public function removeAllObjectsOfThe(string $class): MapsObjectsByIdentity |
||
119 | |||
120 | /** @inheritdoc */ |
||
121 | public function idOf(object $object): string |
||
128 | |||
129 | /** |
||
130 | * Asserts that the object of the class with this id is present in the map. |
||
131 | * |
||
132 | * @param string $class The class of the object to check for. |
||
133 | * @param string $id The identity of the object, unique per class. |
||
134 | * @throws NoSuchObject When there is no object with this id in the map. |
||
135 | */ |
||
136 | private function mustHave(string $class, string $id): void |
||
143 | |||
144 | /** |
||
145 | * Asserts that the object of the class with this id is not already there. |
||
146 | * |
||
147 | * @param string $class The class of the object to check for. |
||
148 | * @param string $id The identity of the object, unique per class. |
||
149 | * @throws AlreadyThere When there is already an object with this id. |
||
150 | */ |
||
151 | private function mayNotAlreadyHave(string $class, string $id): void |
||
157 | |||
158 | /** |
||
159 | * Adds the object to the map, returning the new map. |
||
160 | * |
||
161 | * @param array $objectsBy The original map. |
||
162 | * @param string $withId The id of the object to add. |
||
163 | * @param object $object The object instance to add. |
||
164 | * @return array A new map that includes the new object. |
||
165 | */ |
||
166 | private static function addTo( |
||
174 | } |
||
175 |