1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base daft objects. |
4
|
|
|
* |
5
|
|
|
* @author SignpostMarv |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
10
|
|
|
|
11
|
|
|
abstract class AbstractDaftObjectRepository implements DaftObjectRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var DefinesOwnIdPropertiesInterface[] |
15
|
|
|
*/ |
16
|
|
|
protected $memory = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* mixed[][]. |
20
|
|
|
*/ |
21
|
|
|
protected $data = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $type; |
27
|
|
|
|
28
|
6 |
|
protected function __construct(string $type) |
29
|
|
|
{ |
30
|
6 |
|
$this->type = $type; |
31
|
6 |
|
} |
32
|
|
|
|
33
|
3 |
View Code Duplication |
public function ForgetDaftObject( |
34
|
|
|
DefinesOwnIdPropertiesInterface $object |
35
|
|
|
) : void { |
36
|
3 |
|
if (false === is_a($object, $this->type, true)) { |
37
|
1 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
38
|
1 |
|
1, |
39
|
1 |
|
static::class, |
40
|
1 |
|
__FUNCTION__, |
41
|
1 |
|
$this->type, |
42
|
1 |
|
get_class($object) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
$id = []; |
47
|
|
|
|
48
|
2 |
|
foreach ($object::DaftObjectIdProperties() as $prop) { |
49
|
2 |
|
$id[] = $object->$prop; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
$this->ForgetDaftObjectById($id); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
3 |
View Code Duplication |
public function RemoveDaftObject( |
56
|
|
|
DefinesOwnIdPropertiesInterface $object |
57
|
|
|
) : void { |
58
|
3 |
|
if (false === is_a($object, $this->type, true)) { |
59
|
1 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
60
|
1 |
|
1, |
61
|
1 |
|
static::class, |
62
|
1 |
|
__FUNCTION__, |
63
|
1 |
|
$this->type, |
64
|
1 |
|
get_class($object) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
$id = []; |
69
|
|
|
|
70
|
2 |
|
foreach ($object::DaftObjectIdProperties() as $prop) { |
71
|
2 |
|
$id[] = $object->$prop; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$this->RemoveDaftObjectById($id); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
6 |
|
public static function DaftObjectRepositoryByType( |
78
|
|
|
string $type |
79
|
|
|
) : DaftObjectRepository { |
80
|
|
|
if ( |
81
|
6 |
|
false === is_a( |
82
|
6 |
|
$type, |
83
|
6 |
|
DefinesOwnIdPropertiesInterface::class, |
84
|
6 |
|
true |
85
|
|
|
) |
86
|
|
|
) { |
87
|
1 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
88
|
1 |
|
1, |
89
|
1 |
|
static::class, |
90
|
1 |
|
__FUNCTION__, |
91
|
1 |
|
DefinesOwnIdPropertiesInterface::class, |
92
|
1 |
|
$type |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
6 |
|
return new static($type); |
97
|
|
|
} |
98
|
|
|
|
99
|
5 |
|
public static function DaftObjectRepositoryByDaftObject( |
100
|
|
|
DefinesOwnIdPropertiesInterface $object |
101
|
|
|
) : DaftObjectRepository { |
102
|
5 |
|
return static::DaftObjectRepositoryByType(get_class($object)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|