|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base daft objects. |
|
4
|
|
|
* |
|
5
|
|
|
* @author SignpostMarv |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
|
10
|
|
|
|
|
11
|
|
|
class DaftObjectMemoryRepository extends AbstractDaftObjectRepository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var DefinesOwnIdPropertiesInterface[] |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $memory = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* mixed[][]. |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $data = []; |
|
22
|
|
|
|
|
23
|
|
|
public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void |
|
24
|
|
|
{ |
|
25
|
|
|
static::ThrowIfNotType($object, $this->type, 1, __FUNCTION__); |
|
26
|
|
|
|
|
27
|
|
|
$hashId = $object::DaftObjectIdHash($object); |
|
28
|
|
|
|
|
29
|
|
|
$this->memory[$hashId] = $object; |
|
30
|
|
|
|
|
31
|
|
|
$this->RememberDaftObjectData($object); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param mixed $id |
|
36
|
|
|
*/ |
|
37
|
|
|
public function ForgetDaftObjectById($id) : void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->ForgetDaftObjectByHashId($this->ObjectHashId($id)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param mixed $id |
|
44
|
|
|
*/ |
|
45
|
|
|
public function RemoveDaftObjectById($id) : void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->RemoveDaftObjectByHashId($this->ObjectHashId($id)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param mixed $id |
|
52
|
|
|
*/ |
|
53
|
|
|
public function RecallDaftObject($id) : ? DaftObject |
|
54
|
|
|
{ |
|
55
|
|
|
$hashId = $this->ObjectHashId($id); |
|
56
|
|
|
|
|
57
|
|
|
if (false === isset($this->memory[$hashId])) { |
|
58
|
|
|
return $this->RecallDaftObjectFromData($id); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->memory[$hashId]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void |
|
65
|
|
|
{ |
|
66
|
|
|
$hashId = $object::DaftObjectIdHash($object); |
|
67
|
|
|
|
|
68
|
|
|
$this->data[$hashId] = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($object::DaftObjectPublicGetters() as $property) { |
|
71
|
|
|
$getter = 'Get' . ucfirst($property); |
|
72
|
|
|
$this->data[$hashId][$property] = $object->$getter(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Recalls the corresponding daft object instance from cached data. |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $id |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function RecallDaftObjectFromData($id) : ? DaftObject |
|
82
|
|
|
{ |
|
83
|
|
|
$hashId = $this->ObjectHashId($id); |
|
84
|
|
|
if (true === isset($this->data[$hashId])) { |
|
85
|
|
|
$type = $this->type; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var DaftObject $out |
|
89
|
|
|
*/ |
|
90
|
|
|
$out = new $type($this->data[$hashId]); |
|
91
|
|
|
|
|
92
|
|
|
return $out; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Converts an id to a unique-enough-for-now string. |
|
100
|
|
|
* |
|
101
|
|
|
* @param mixed $id |
|
102
|
|
|
*/ |
|
103
|
|
|
private function ObjectHashId($id) : string |
|
104
|
|
|
{ |
|
105
|
|
|
$id = is_array($id) ? $id : [$id]; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var DefinesOwnIdPropertiesInterface $type |
|
109
|
|
|
*/ |
|
110
|
|
|
$type = $this->type; |
|
111
|
|
|
|
|
112
|
|
|
return $type::DaftObjectIdValuesHash($id); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function ForgetDaftObjectByHashId(string $hashId) : void |
|
116
|
|
|
{ |
|
117
|
|
|
if (true === isset($this->memory[$hashId])) { |
|
118
|
|
|
unset($this->memory[$hashId]); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
private function RemoveDaftObjectByHashId(string $hashId) : void |
|
123
|
|
|
{ |
|
124
|
|
|
$this->ForgetDaftObjectByHashId($hashId); |
|
125
|
|
|
|
|
126
|
|
|
if (true === isset($this->data[$hashId])) { |
|
127
|
|
|
unset($this->data[$hashId]); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|