1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base daft objects. |
4
|
|
|
* |
5
|
|
|
* @author SignpostMarv |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
10
|
|
|
|
11
|
|
|
trait TraitRememberDaftObject |
12
|
|
|
{ |
13
|
114 |
|
public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void |
14
|
|
|
{ |
15
|
114 |
|
if ($object instanceof DaftNestedWriteableObject) { |
16
|
112 |
|
$this->RememberDaftObjectWriteableTyped($object); |
17
|
|
|
} else { |
18
|
2 |
|
static::ThrowIfNotType($object, DaftNestedWriteableObject::class, 1, __FUNCTION__); |
19
|
|
|
} |
20
|
112 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return array<int, DaftNestedObject> |
24
|
|
|
*/ |
25
|
|
|
abstract public function RecallDaftNestedObjectFullTree( |
26
|
|
|
int $relativeDepthLimit = null |
27
|
|
|
) : array; |
28
|
|
|
|
29
|
|
|
abstract public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int; |
30
|
|
|
|
31
|
112 |
|
protected function RememberDaftObjectWriteableTyped(DaftNestedWriteableObject $object) : void |
32
|
|
|
{ |
33
|
112 |
|
$left = $object->GetIntNestedLeft(); |
34
|
112 |
|
$right = $object->GetIntNestedRight(); |
35
|
112 |
|
$level = $object->GetIntNestedLevel(); |
36
|
|
|
|
37
|
112 |
|
if (0 === $left && 0 === $right && 0 === $level) { |
38
|
110 |
|
if ($this->CountDaftNestedObjectFullTree() > 0) { |
39
|
84 |
|
$tree = $this->RecallDaftNestedObjectFullTree(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var DaftNestedWriteableObject $end |
43
|
|
|
*/ |
44
|
84 |
|
$end = end($tree); |
45
|
|
|
|
46
|
84 |
|
$left = $end->GetIntNestedRight() + 1; |
47
|
|
|
} else { |
48
|
110 |
|
$left = $this->CountDaftNestedObjectFullTree() * 2; |
49
|
|
|
} |
50
|
|
|
|
51
|
110 |
|
$object->SetIntNestedLeft($left); |
52
|
110 |
|
$object->SetIntNestedRight($left + 1); |
53
|
|
|
} |
54
|
|
|
|
55
|
112 |
|
parent::RememberDaftObject($object); |
56
|
112 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param DaftObject|string $object |
60
|
|
|
*/ |
61
|
2 |
|
protected static function ThrowIfNotType( |
62
|
|
|
$object, |
63
|
|
|
string $type, |
64
|
|
|
int $argument, |
65
|
|
|
string $function |
66
|
|
|
) : void { |
67
|
2 |
|
if ( ! is_a($object, DaftNestedWriteableObject::class, is_string($object))) { |
68
|
2 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
69
|
2 |
|
$argument, |
70
|
2 |
|
static::class, |
71
|
2 |
|
$function, |
72
|
2 |
|
DaftNestedWriteableObject::class, |
73
|
2 |
|
is_string($object) ? $object : get_class($object) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
parent::ThrowIfNotType($object, $type, $argument, $function); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|