|
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
|
|
|
* @var array<string, array<string, mixed>> |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $data = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $type; |
|
27
|
|
|
|
|
28
|
|
|
protected function __construct(string $type) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->type = $type; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function ForgetDaftObject(DefinesOwnIdPropertiesInterface $object) : void |
|
34
|
|
|
{ |
|
35
|
|
|
static::ThrowIfNotType($object, $this->type, 1, __FUNCTION__); |
|
36
|
|
|
|
|
37
|
|
|
$id = []; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($object::DaftObjectIdProperties() as $prop) { |
|
40
|
|
|
$id[] = $object->$prop; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->ForgetDaftObjectById($id); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function RemoveDaftObject(DefinesOwnIdPropertiesInterface $object) : void |
|
47
|
|
|
{ |
|
48
|
|
|
static::ThrowIfNotType($object, $this->type, 1, __FUNCTION__); |
|
49
|
|
|
|
|
50
|
|
|
$id = []; |
|
51
|
|
|
|
|
52
|
|
|
foreach ($object::DaftObjectIdProperties() as $prop) { |
|
53
|
|
|
$id[] = $object->$prop; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->RemoveDaftObjectById($id); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function DaftObjectRepositoryByType(string $type) : DaftObjectRepository |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ( |
|
62
|
|
|
[ |
|
63
|
|
|
DaftObjectCreatedByArray::class, |
|
64
|
|
|
DefinesOwnIdPropertiesInterface::class, |
|
65
|
|
|
] as $checkFor |
|
66
|
|
|
) { |
|
67
|
|
|
static::ThrowIfNotType($type, $checkFor, 1, __FUNCTION__); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return new static($type); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function DaftObjectRepositoryByDaftObject( |
|
74
|
|
|
DefinesOwnIdPropertiesInterface $object |
|
75
|
|
|
) : DaftObjectRepository { |
|
76
|
|
|
return static::DaftObjectRepositoryByType(get_class($object)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param DaftObject|string $object |
|
81
|
|
|
*/ |
|
82
|
|
|
protected static function ThrowIfNotType( |
|
83
|
|
|
$object, |
|
84
|
|
|
string $type, |
|
85
|
|
|
int $argument, |
|
86
|
|
|
string $function |
|
87
|
|
|
) : void { |
|
88
|
|
|
if (false === is_a($object, $type, is_string($object))) { |
|
89
|
|
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
|
90
|
|
|
$argument, |
|
91
|
|
|
static::class, |
|
92
|
|
|
$function, |
|
93
|
|
|
$type, |
|
94
|
|
|
is_string($object) ? $object : get_class($object) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|