1 | <?php |
||
2 | |||
3 | namespace Stratadox\TableLoader\Loader; |
||
4 | |||
5 | use ArrayAccess; |
||
6 | use IteratorAggregate; |
||
7 | use Stratadox\IdentityMap\AlreadyThere; |
||
8 | use Stratadox\IdentityMap\MapsObjectsByIdentity; |
||
9 | use Stratadox\IdentityMap\NoSuchObject; |
||
10 | use Stratadox\TableLoader\Loader\ContainsResultingObjects as Result; |
||
0 ignored issues
–
show
|
|||
11 | |||
12 | /** |
||
13 | * Contains the resulting objects and the updated identity map. |
||
14 | * |
||
15 | * @author Stratadox |
||
16 | */ |
||
17 | interface ContainsResultingObjects extends ArrayAccess, IteratorAggregate |
||
18 | { |
||
19 | /** |
||
20 | * Returns the resulting identity map. |
||
21 | * |
||
22 | * The identity map contains all the objects that have been loaded so far. |
||
23 | * Since the identity map itself is an immutable data structure, an updated |
||
24 | * copy is returned in the result. |
||
25 | * This updated copy is to be used in subsequent calls to load objects, so |
||
26 | * that the objects that have already been loaded are re-used instead of |
||
27 | * loaded twice. |
||
28 | * |
||
29 | * @return MapsObjectsByIdentity The updated identity map. |
||
30 | */ |
||
31 | public function identityMap(): MapsObjectsByIdentity; |
||
32 | |||
33 | /** |
||
34 | * Checks if the object is in the identity map. |
||
35 | * |
||
36 | * Note that this does not check whether the object was loaded by the |
||
37 | * operation that produced *this particular result*: instead it checks |
||
38 | * whether the object has been loaded by any of the operations so far. |
||
39 | * To check whether a result was first seen in this particular result, use |
||
40 | * `isset($result[$label][$id])` |
||
41 | * @see offsetExists |
||
42 | * |
||
43 | * @param string $class The class of the object to check for. |
||
44 | * @param string $id The identity of the object, unique per class. |
||
45 | * @return bool Whether the object is in the map. |
||
46 | */ |
||
47 | public function has(string $class, string $id): bool; |
||
48 | |||
49 | /** |
||
50 | * Gets the object from the identity map. |
||
51 | * |
||
52 | * @param string $class The class of the object to check for. |
||
53 | * @param string $id The identity of the object, unique per class. |
||
54 | * @return object The object that was stored in the map. |
||
55 | * @throws NoSuchObject |
||
56 | */ |
||
57 | public function get(string $class, string $id): object; |
||
58 | |||
59 | /** |
||
60 | * Checks whether this result retrieved anything with this label. |
||
61 | * |
||
62 | * Used to determine the result of `isset` calls, either as |
||
63 | * `isset($result[$label])` to determine whether a label got loaded, or as |
||
64 | * `isset($result[$label][$id])` to check whether the object got loaded. |
||
65 | * Note that the id used in the second example is the identifier used for |
||
66 | * loading the objects. |
||
67 | * @see IdentifiesEntities::andForLoading |
||
68 | * |
||
69 | * @param string $label |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function offsetExists($label): bool; |
||
73 | |||
74 | /** |
||
75 | * Retrieves the object(s) with the given label. |
||
76 | * |
||
77 | * Used to access the result through array access, for example as |
||
78 | * `$cars = $result['car'];` or |
||
79 | * `$car = $result['car']['3'];` |
||
80 | * |
||
81 | * @param mixed $label |
||
82 | * @return iterable |
||
83 | */ |
||
84 | public function offsetGet($label): iterable; |
||
85 | |||
86 | /** |
||
87 | * Adds the object to the result. |
||
88 | * |
||
89 | * @param string $label The label of the object to add. |
||
90 | * @param string $idForLoading The id to use in loading the object. |
||
91 | * @param string $idForMap The id to use in the identity map. |
||
92 | * @param object $object The object that was stored in the map. |
||
93 | * @return Result A copy of the result with the object added. |
||
94 | * @throws AlreadyThere When the object was already in the map. |
||
95 | */ |
||
96 | public function add( |
||
97 | string $label, |
||
98 | string $idForLoading, |
||
99 | string $idForMap, |
||
100 | object $object |
||
101 | ): Result; |
||
102 | |||
103 | /** |
||
104 | * Include the object in the results. |
||
105 | * |
||
106 | * @param string $label The label of the object to include in the results. |
||
107 | * @param string $id The id as used in loading the object. |
||
108 | * @param object $object The object to include in the results. |
||
109 | * @return Result The updated results. |
||
110 | */ |
||
111 | public function include( |
||
112 | string $label, |
||
113 | string $id, |
||
114 | object $object |
||
115 | ): Result; |
||
116 | |||
117 | /** |
||
118 | * Merge with other results. |
||
119 | * |
||
120 | * Note: The objects per label from both results are merged, but the identity map of the merged |
||
121 | * result is copied without alteration. |
||
122 | * |
||
123 | * @param Result $objects The other results to merge with |
||
124 | * @return Result The updated results. |
||
125 | */ |
||
126 | public function mergeWith(Result $objects): Result; |
||
127 | } |
||
128 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: