1 | <?php |
||
14 | final class IdentityMap implements MapsObjectsByIdentity |
||
15 | { |
||
16 | private $map; |
||
17 | |||
18 | private function __construct(array $map) |
||
22 | |||
23 | /** |
||
24 | * Produces a new identity map that contains the objects. |
||
25 | * |
||
26 | * @param array $objects The objects to add, as [id => object] |
||
27 | * @return MapsObjectsByIdentity The map of objects. |
||
28 | */ |
||
29 | public static function with(array $objects): MapsObjectsByIdentity |
||
37 | |||
38 | /** |
||
39 | * Produces an empty identity map. |
||
40 | * |
||
41 | * @return MapsObjectsByIdentity The empty map of objects. |
||
42 | */ |
||
43 | public static function startEmpty(): MapsObjectsByIdentity |
||
47 | |||
48 | /** @inheritdoc */ |
||
49 | public function has(string $class, string $id): bool |
||
53 | |||
54 | /** @inheritdoc */ |
||
55 | public function get(string $class, string $id): object |
||
60 | |||
61 | /** @inheritdoc */ |
||
62 | public function add(string $id, object $object): MapsObjectsByIdentity |
||
70 | |||
71 | /** @inheritdoc */ |
||
72 | public function remove(string $class, string $id): MapsObjectsByIdentity |
||
79 | |||
80 | /** @inheritdoc */ |
||
81 | public function idOf(object $object): string |
||
91 | |||
92 | /** |
||
93 | * Asserts that the object of the class with this id is present in the map. |
||
94 | * |
||
95 | * @param string $class The class of the object to check for. |
||
96 | * @param string $id The identity of the object, unique per class. |
||
97 | * @throws NoSuchObject When there is no object with this id in the map. |
||
98 | */ |
||
99 | private function mustHave(string $class, string $id): void |
||
106 | |||
107 | /** |
||
108 | * Asserts that the object of the class with this id is not already there. |
||
109 | * |
||
110 | * @param string $class The class of the object to check for. |
||
111 | * @param string $id The identity of the object, unique per class. |
||
112 | * @throws AlreadyThere When there is already an object with this id. |
||
113 | */ |
||
114 | private function mayNotAlreadyHave(string $class, string $id): void |
||
120 | |||
121 | /** |
||
122 | * Asserts that the class of the object is known to the identity map. |
||
123 | * |
||
124 | * @param object $object The object whose class must be known. |
||
125 | * @throws NoSuchObject When there are no objects of this class in the map. |
||
126 | */ |
||
127 | private function mustKnowThisTypeOf(object $object): void |
||
133 | |||
134 | /** |
||
135 | * Adds the object to the map, returning the new map. |
||
136 | * |
||
137 | * @param array $map The original map. |
||
138 | * @param string $id The id of the object to add. |
||
139 | * @param object $object The object instance to add. |
||
140 | * @return array A new map that includes the new object. |
||
141 | */ |
||
142 | private static function addTo(array $map, string $id, object $object): array |
||
147 | } |
||
148 |