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