1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Stratadox\TableLoader\Loader; |
5
|
|
|
|
6
|
|
|
use ArrayIterator; |
7
|
|
|
use BadMethodCallException; |
8
|
|
|
use Exception; |
9
|
|
|
use Stratadox\IdentityMap\IdentityMap; |
10
|
|
|
use Stratadox\IdentityMap\MapsObjectsByIdentity; |
11
|
|
|
use Traversable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Result. |
15
|
|
|
* |
16
|
|
|
* @author Stratadox |
17
|
|
|
*/ |
18
|
|
|
final class Result implements ContainsResultingObjects |
19
|
|
|
{ |
20
|
|
|
private $objects; |
21
|
|
|
private $identityMap; |
22
|
|
|
|
23
|
|
|
private function __construct( |
24
|
|
|
array $objects, |
25
|
|
|
MapsObjectsByIdentity $identityMap |
26
|
|
|
) { |
27
|
|
|
$this->objects = $objects; |
28
|
|
|
$this->identityMap = $identityMap; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Makes a new result from an array of objects as produced by table loaders |
33
|
|
|
* and an identity map. |
34
|
|
|
* |
35
|
|
|
* @param array[] $objects Array of objects, as: |
36
|
|
|
* [label => [id => object]] |
37
|
|
|
* @param MapsObjectsByIdentity $identityMap The map of objects by class and |
38
|
|
|
* identifier. |
39
|
|
|
* @return ContainsResultingObjects |
40
|
|
|
*/ |
41
|
|
|
public static function fromArray( |
42
|
|
|
array $objects, |
43
|
|
|
MapsObjectsByIdentity $identityMap = null |
44
|
|
|
): ContainsResultingObjects { |
45
|
|
|
return new self($objects, $identityMap ?: IdentityMap::startEmpty()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @inheritdoc */ |
49
|
|
|
public function has(string $class, string $id): bool |
50
|
|
|
{ |
51
|
|
|
return $this->identityMap->has($class, $id); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @inheritdoc */ |
55
|
|
|
public function get(string $class, string $id): object |
56
|
|
|
{ |
57
|
|
|
return $this->identityMap->get($class, $id); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** @inheritdoc */ |
61
|
|
|
public function add( |
62
|
|
|
string $label, |
63
|
|
|
string $idForLoading, |
64
|
|
|
string $idForMap, |
65
|
|
|
object $object |
66
|
|
|
): ContainsResultingObjects { |
67
|
|
|
return new self($this->merge( |
68
|
|
|
$this->objects, |
69
|
|
|
[$label => [$idForLoading => $object]] |
70
|
|
|
), $this->identityMap->add($idForMap, $object)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @inheritdoc */ |
74
|
|
|
public function mergeWith( |
75
|
|
|
ContainsResultingObjects $otherObjects |
76
|
|
|
): ContainsResultingObjects { |
77
|
|
|
return new self( |
78
|
|
|
$this->merge($this->objects, $otherObjects), |
79
|
|
|
$otherObjects->identityMap() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @inheritdoc */ |
84
|
|
|
public function include( |
85
|
|
|
string $label, |
86
|
|
|
string $id, |
87
|
|
|
object $object |
88
|
|
|
): ContainsResultingObjects { |
89
|
|
|
return new self($this->merge( |
90
|
|
|
$this->objects, |
91
|
|
|
[$label => [$id => $object]] |
92
|
|
|
), $this->identityMap); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param object[][] $result |
97
|
|
|
* @param object[][] $with |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
private function merge(array $result, iterable $with): array |
101
|
|
|
{ |
102
|
|
|
foreach ($with as $label => $objects) { |
103
|
|
|
foreach ($objects as $id => $object) { |
104
|
|
|
$result[$label][$id] = $object; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** @inheritdoc */ |
111
|
|
|
public function identityMap(): MapsObjectsByIdentity |
112
|
|
|
{ |
113
|
|
|
return $this->identityMap; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** @inheritdoc */ |
117
|
|
|
public function getIterator(): Traversable |
118
|
|
|
{ |
119
|
|
|
return new ArrayIterator($this->objects); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** @inheritdoc */ |
123
|
|
|
public function offsetExists($offset): bool |
124
|
|
|
{ |
125
|
|
|
return isset($this->objects[$offset]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** @inheritdoc */ |
129
|
|
|
public function offsetGet($offset): array |
130
|
|
|
{ |
131
|
|
|
return $this->objects[$offset]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
* @throws Exception |
137
|
|
|
*/ |
138
|
|
|
public function offsetSet($offset, $value): void |
139
|
|
|
{ |
140
|
|
|
throw new BadMethodCallException('Altering the results is not allowed.'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritdoc |
145
|
|
|
* @throws Exception |
146
|
|
|
*/ |
147
|
|
|
public function offsetUnset($offset): void |
148
|
|
|
{ |
149
|
|
|
throw new BadMethodCallException('Altering the results is not allowed.'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|