1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base daft objects. |
4
|
|
|
* |
5
|
|
|
* @author SignpostMarv |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @template T as SuitableForRepositoryType |
13
|
|
|
* |
14
|
|
|
* @template-extends AbstractDaftObjectRepository<T> |
15
|
|
|
*/ |
16
|
|
|
class DaftObjectMemoryRepository extends AbstractDaftObjectRepository |
17
|
|
|
{ |
18
|
|
|
const BOOL_DEFAULT_ASSUMEDOESNOTEXIST = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @psalm-param T $object |
24
|
|
|
*/ |
25
|
4 |
|
public function RememberDaftObject(SuitableForRepositoryType $object) : void |
26
|
|
|
{ |
27
|
4 |
|
$hashId = $object::DaftObjectIdHash($object); |
28
|
|
|
|
29
|
4 |
|
$this->memory[$hashId] = $object; |
30
|
|
|
|
31
|
4 |
|
$this->RememberDaftObjectData($object); |
32
|
4 |
|
} |
33
|
|
|
|
34
|
4 |
|
public function ForgetDaftObjectById($id) : void |
35
|
|
|
{ |
36
|
4 |
|
$this->ForgetDaftObjectByHashId($this->type::DaftObjectIdValuesHash( |
37
|
4 |
|
is_array($id) ? $id : [$id] |
38
|
|
|
)); |
39
|
4 |
|
} |
40
|
|
|
|
41
|
4 |
|
public function RemoveDaftObjectById($id) : void |
42
|
|
|
{ |
43
|
4 |
|
$this->RemoveDaftObjectByHashId($this->type::DaftObjectIdValuesHash( |
44
|
4 |
|
is_array($id) ? $id : [$id] |
45
|
|
|
)); |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
* |
51
|
|
|
* @psalm-return T|null |
52
|
|
|
*/ |
53
|
4 |
|
public function RecallDaftObject($id) : ? SuitableForRepositoryType |
54
|
|
|
{ |
55
|
4 |
|
$hashId = $this->type::DaftObjectIdValuesHash( |
56
|
4 |
|
is_array($id) ? $id : [$id] |
57
|
|
|
); |
58
|
|
|
|
59
|
4 |
|
return $this->memory[$hashId] ?? $this->RecallDaftObjectFromData($id); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param scalar|(scalar|array|object|null)[] $id |
64
|
|
|
* |
65
|
|
|
* @psalm-param class-string<T> $type |
66
|
|
|
* |
67
|
|
|
* @psalm-return T |
68
|
|
|
*/ |
69
|
4 |
|
final public function RecallDaftObjectOrThrow( |
70
|
|
|
$id, |
71
|
|
|
string $type = SuitableForRepositoryType::class |
72
|
|
|
) : SuitableForRepositoryType { |
73
|
4 |
|
$out = $this->RecallDaftObject($id); |
74
|
|
|
|
75
|
4 |
|
if (is_null($out) || ! is_a($out, $type, true)) { |
76
|
4 |
|
throw new DaftObjectNotRecalledException( |
77
|
|
|
'Argument 1 passed to ' . |
78
|
|
|
DaftObjectRepository::class . |
79
|
|
|
'::RecallDaftObjectOrThrow() did not resolve to an instance of ' . |
80
|
4 |
|
$type . |
81
|
4 |
|
' from ' . |
82
|
4 |
|
static::class . |
83
|
4 |
|
'::RecallDaftObject()' |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @psalm-var T |
89
|
|
|
*/ |
90
|
4 |
|
$out = $out; |
91
|
|
|
|
92
|
4 |
|
return $out; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
* |
98
|
|
|
* @psalm-param T $object |
99
|
|
|
*/ |
100
|
4 |
|
public function RememberDaftObjectData( |
101
|
|
|
SuitableForRepositoryType $object, |
102
|
|
|
bool $assumeDoesNotExist = self::BOOL_DEFAULT_ASSUMEDOESNOTEXIST |
103
|
|
|
) : void { |
104
|
4 |
|
$hashId = $object::DaftObjectIdHash($object); |
105
|
|
|
|
106
|
4 |
|
$this->data[$hashId] = []; |
107
|
|
|
|
108
|
4 |
|
foreach ($object::DaftObjectPublicGetters() as $property) { |
109
|
4 |
|
$this->data[$hashId][$property] = $object->__get($property); |
110
|
|
|
} |
111
|
4 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Recalls the corresponding daft object instance from cached data. |
115
|
|
|
* |
116
|
|
|
* @param scalar|(scalar|array|object|null)[] $id |
117
|
|
|
* |
118
|
|
|
* @psalm-return T|null |
119
|
|
|
*/ |
120
|
8 |
|
protected function RecallDaftObjectFromData($id) : ? SuitableForRepositoryType |
121
|
|
|
{ |
122
|
8 |
|
$hashId = $this->type::DaftObjectIdValuesHash( |
123
|
8 |
|
is_array($id) ? $id : [$id] |
124
|
|
|
); |
125
|
|
|
|
126
|
8 |
|
if (isset($this->data[$hashId])) { |
127
|
4 |
|
$type = $this->type; |
128
|
|
|
|
129
|
4 |
|
$out = new $type($this->data[$hashId]); |
130
|
|
|
|
131
|
4 |
|
return $out; |
132
|
|
|
} |
133
|
|
|
|
134
|
8 |
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
4 |
|
private function ForgetDaftObjectByHashId(string $hashId) : void |
138
|
|
|
{ |
139
|
4 |
|
if (true === isset($this->memory[$hashId])) { |
140
|
4 |
|
unset($this->memory[$hashId]); |
141
|
|
|
} |
142
|
4 |
|
} |
143
|
|
|
|
144
|
4 |
|
private function RemoveDaftObjectByHashId(string $hashId) : void |
145
|
|
|
{ |
146
|
4 |
|
$this->ForgetDaftObjectByHashId($hashId); |
147
|
|
|
|
148
|
4 |
|
if (true === isset($this->data[$hashId])) { |
149
|
4 |
|
unset($this->data[$hashId]); |
150
|
|
|
} |
151
|
4 |
|
} |
152
|
|
|
} |
153
|
|
|
|