1 | <?php |
||
16 | final class IdentityMap implements MapsObjectsByIdentity |
||
17 | { |
||
18 | private $map; |
||
19 | private $reverseMap; |
||
20 | |||
21 | private function __construct(array $map, array $reverseMap) |
||
26 | |||
27 | /** |
||
28 | * Produces a new identity map that contains the objects. |
||
29 | * |
||
30 | * @param object[] $objects The objects to add, as [id => object] |
||
31 | * @return MapsObjectsByIdentity The map of objects. |
||
32 | */ |
||
33 | public static function with(array $objects): 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 |
||
83 | |||
84 | /** @inheritdoc */ |
||
85 | public function remove(string $class, string $id): MapsObjectsByIdentity |
||
96 | |||
97 | /** @inheritdoc */ |
||
98 | public function removeThe(string $class): MapsObjectsByIdentity |
||
112 | |||
113 | /** @inheritdoc */ |
||
114 | public function idOf(object $object): string |
||
121 | |||
122 | /** |
||
123 | * Asserts that the object of the class with this id is present in the map. |
||
124 | * |
||
125 | * @param string $class The class of the object to check for. |
||
126 | * @param string $id The identity of the object, unique per class. |
||
127 | * @throws NoSuchObject When there is no object with this id in the map. |
||
128 | */ |
||
129 | private function mustHave(string $class, string $id): void |
||
136 | |||
137 | /** |
||
138 | * Asserts that the object of the class with this id is not already there. |
||
139 | * |
||
140 | * @param string $class The class of the object to check for. |
||
141 | * @param string $id The identity of the object, unique per class. |
||
142 | * @throws AlreadyThere When there is already an object with this id. |
||
143 | */ |
||
144 | private function mayNotAlreadyHave(string $class, string $id): void |
||
150 | |||
151 | /** |
||
152 | * Adds the object to the map, returning the new map. |
||
153 | * |
||
154 | * @param array $map The original map. |
||
155 | * @param string $id The id of the object to add. |
||
156 | * @param object $object The object instance to add. |
||
157 | * @return array A new map that includes the new object. |
||
158 | */ |
||
159 | private static function addTo(array $map, string $id, object $object): array |
||
164 | } |
||
165 |