1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the G.L.S.R. Apps package. |
7
|
|
|
* |
8
|
|
|
* (c) Dev-Int Création <[email protected]>. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Administration\Domain\FamilyLog\Model; |
15
|
|
|
|
16
|
|
|
use Administration\Domain\FamilyLog\Model\VO\FamilyLogUuid; |
17
|
|
|
use Core\Domain\Common\Model\VO\NameField; |
18
|
|
|
|
19
|
|
|
final class FamilyLog |
20
|
|
|
{ |
21
|
|
|
private string $uuid; |
22
|
|
|
private string $label; |
23
|
|
|
private int $level; |
24
|
|
|
private ?FamilyLog $parent = null; |
25
|
|
|
/** |
26
|
|
|
* @var FamilyLog[]|null |
27
|
|
|
*/ |
28
|
|
|
private ?array $children = null; |
29
|
|
|
private string $slug; |
30
|
|
|
private string $path; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
FamilyLogUuid $uuid, |
34
|
|
|
NameField $label, |
35
|
|
|
int $level, |
36
|
|
|
?self $parent = null, |
37
|
|
|
?string $path = null |
38
|
|
|
) { |
39
|
|
|
$this->uuid = $uuid->toString(); |
40
|
|
|
$this->label = $label->getValue(); |
41
|
|
|
$this->path = $label->slugify(); |
42
|
|
|
if (null !== $path) { |
43
|
|
|
$this->path = $path; |
44
|
|
|
} |
45
|
|
|
$this->slug = $label->slugify(); |
46
|
|
|
$this->level = $level; |
47
|
|
|
if (null !== $parent) { |
48
|
|
|
$this->attributeParent($parent); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function create( |
53
|
|
|
FamilyLogUuid $uuid, |
54
|
|
|
NameField $label, |
55
|
|
|
int $level, |
56
|
|
|
?self $parent = null, |
57
|
|
|
?string $path = null |
58
|
|
|
): self { |
59
|
|
|
return new self($uuid, $label, $level, $parent, $path); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function uuid(): string |
63
|
|
|
{ |
64
|
|
|
return $this->uuid; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function label(): string |
68
|
|
|
{ |
69
|
|
|
return $this->label; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function level(): int |
73
|
|
|
{ |
74
|
|
|
return $this->level; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function parent(): ?self |
78
|
|
|
{ |
79
|
|
|
return $this->parent; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function children(): ?array |
83
|
|
|
{ |
84
|
|
|
return $this->children; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function childrenArrayLabels(self $familyLog): ?array |
88
|
|
|
{ |
89
|
|
|
if (null !== $familyLog->children) { |
90
|
|
|
return \array_map(static function (self $child) { |
91
|
|
|
return $child->label; |
92
|
|
|
}, $familyLog->children); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function path(): string |
99
|
|
|
{ |
100
|
|
|
return $this->path; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function slug(): string |
104
|
|
|
{ |
105
|
|
|
return $this->slug; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function parseTree(): array |
109
|
|
|
{ |
110
|
|
|
$arrayChildren = []; |
111
|
|
|
if (null === $this->children) { |
112
|
|
|
return [$this->label => $arrayChildren]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($this->children as $child) { |
116
|
|
|
if (null !== $this->childrenArrayLabels($child)) { |
117
|
|
|
$arrayChildren[$child->label] = $this->childrenArrayLabels($child); |
118
|
|
|
} else { |
119
|
|
|
$arrayChildren[] = $child->label; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return [$this->label => $arrayChildren]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function rename(NameField $label): void |
127
|
|
|
{ |
128
|
|
|
$this->label = $label->getValue(); |
129
|
|
|
$this->slug = $label->slugify(); |
130
|
|
|
$this->path = $this->slug(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function attributeParent(self $parent): void |
134
|
|
|
{ |
135
|
|
|
$this->parent = $parent; |
136
|
|
|
$this->level = 1; |
137
|
|
|
if (null !== $parent) { |
138
|
|
|
$this->parent->addChild($this); |
139
|
|
|
$this->path = $parent->path() . '/' . $this->slug(); |
140
|
|
|
$this->level = $parent->level + 1; |
141
|
|
|
} |
142
|
|
|
if ($this->hasChildren()) { |
143
|
|
|
$this->changeLevel($this->children(), 1); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function hasChildren(): bool |
148
|
|
|
{ |
149
|
|
|
return null !== $this->children; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function addChild(self $child): void |
153
|
|
|
{ |
154
|
|
|
$this->children[] = $child; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param FamilyLog[] $children |
159
|
|
|
*/ |
160
|
|
|
private function changeLevel(array $children, int $scale): void |
161
|
|
|
{ |
162
|
|
|
foreach ($children as $child) { |
163
|
|
|
$child->level += $scale; |
164
|
|
|
if ($child->hasChildren()) { |
165
|
|
|
$this->changeLevel($child->children(), 1); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|