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