forked from Respect/Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractMapper.php
More file actions
240 lines (184 loc) · 6.36 KB
/
AbstractMapper.php
File metadata and controls
240 lines (184 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
declare(strict_types=1);
namespace Respect\Data;
use Respect\Data\Collections\Collection;
use Respect\Data\Collections\Filtered;
use SplObjectStorage;
use function array_flip;
use function array_intersect_key;
use function count;
use function is_int;
use function is_scalar;
use function is_string;
abstract class AbstractMapper
{
/** @var SplObjectStorage<object, Collection> Maps entity → source Collection */
protected SplObjectStorage $tracked;
/** @var SplObjectStorage<object, string> Maps entity → 'insert'|'update'|'delete' */
protected SplObjectStorage $pending;
/** @var array<string, array<int|string, object>> PK-indexed identity map: [collectionName][pkValue] → entity */
protected array $identityMap = [];
/** @var array<string, Collection> */
private array $collections = [];
public Styles\Stylable $style { get => $this->entityFactory->style; }
public function __construct(
public readonly EntityFactory $entityFactory = new EntityFactory(),
) {
$this->tracked = new SplObjectStorage();
$this->pending = new SplObjectStorage();
}
abstract public function flush(): void;
abstract public function fetch(Collection $collection, mixed $extra = null): mixed;
/** @return array<int, mixed> */
abstract public function fetchAll(Collection $collection, mixed $extra = null): array;
public function reset(): void
{
$this->pending = new SplObjectStorage();
}
public function clearIdentityMap(): void
{
$this->identityMap = [];
}
public function trackedCount(): int
{
return count($this->tracked);
}
public function identityMapCount(): int
{
$total = 0;
foreach ($this->identityMap as $entries) {
$total += count($entries);
}
return $total;
}
public function markTracked(object $entity, Collection $collection): bool
{
$this->tracked[$entity] = $collection;
return true;
}
public function persist(object $object, Collection $onCollection): bool
{
$next = $onCollection->next;
if ($onCollection instanceof Filtered && $next !== null) {
$this->persist($object, $next);
return true;
}
if ($this->isTracked($object)) {
$currentOp = $this->pending[$object] ?? null;
if ($currentOp !== 'insert') {
$this->pending[$object] = 'update';
}
return true;
}
$this->pending[$object] = 'insert';
$this->markTracked($object, $onCollection);
return true;
}
public function remove(object $object, Collection $fromCollection): bool
{
$this->pending[$object] = 'delete';
if (!$this->isTracked($object)) {
$this->markTracked($object, $fromCollection);
}
return true;
}
public function isTracked(object $entity): bool
{
return $this->tracked->offsetExists($entity);
}
public function registerCollection(string $alias, Collection $collection): void
{
$collection->mapper = $this;
$this->collections[$alias] = $collection;
}
abstract protected function defaultHydrator(Collection $collection): Hydrator;
/**
* @param array<string, mixed> $columns
*
* @return array<string, mixed>
*/
protected function filterColumns(array $columns, Collection $collection): array
{
if (
!$collection instanceof Filtered
|| !$collection->filters
|| $collection->identifierOnly
|| $collection->name === null
) {
return $columns;
}
$pk = $this->style->identifier($collection->name);
return array_intersect_key($columns, array_flip([...$collection->filters, $pk]));
}
protected function resolveHydrator(Collection $collection): Hydrator
{
return $collection->hydrator ?? $this->defaultHydrator($collection);
}
protected function registerInIdentityMap(object $entity, Collection $coll): void
{
if ($coll->name === null) {
return;
}
$pkValue = $this->entityPkValue($entity, $coll->name);
if ($pkValue === null) {
return;
}
$this->identityMap[$coll->name][$pkValue] = $entity;
}
protected function evictFromIdentityMap(object $entity, Collection $coll): void
{
if ($coll->name === null) {
return;
}
$pkValue = $this->entityPkValue($entity, $coll->name);
if ($pkValue === null) {
return;
}
unset($this->identityMap[$coll->name][$pkValue]);
}
protected function findInIdentityMap(Collection $collection): object|null
{
if ($collection->name === null || !is_scalar($collection->condition) || $collection->more) {
return null;
}
$condition = $collection->condition;
if (!is_int($condition) && !is_string($condition)) {
return null;
}
return $this->identityMap[$collection->name][$condition] ?? null;
}
private function entityPkValue(object $entity, string $collName): int|string|null
{
$pkValue = $this->entityFactory->get($entity, $this->style->identifier($collName));
return is_int($pkValue) || is_string($pkValue) ? $pkValue : null;
}
public function __get(string $name): Collection
{
if (isset($this->collections[$name])) {
return $this->collections[$name];
}
$coll = new Collection($name);
$coll->mapper = $this;
return $coll;
}
public function __isset(string $alias): bool
{
return isset($this->collections[$alias]);
}
public function __set(string $alias, Collection $collection): void
{
$this->registerCollection($alias, $collection);
}
/** @param list<Collection|array<scalar, mixed>|scalar|null> $arguments */
public function __call(string $name, array $arguments): Collection
{
if (isset($this->collections[$name])) {
$collection = clone $this->collections[$name];
$collection->mapper = $this;
return $collection->with(...$arguments);
}
$collection = Collection::__callstatic($name, $arguments);
$collection->mapper = $this;
return $collection;
}
}