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 ?FamilyLog $parent = null; |
24
|
|
|
/** |
25
|
|
|
* @var FamilyLog[]|null |
26
|
|
|
*/ |
27
|
|
|
private ?array $children = null; |
28
|
|
|
private string $slug; |
29
|
|
|
private string $path; |
30
|
|
|
|
31
|
|
|
public function __construct(FamilyLogUuid $uuid, NameField $label, ?self $parent = null) |
32
|
|
|
{ |
33
|
|
|
$this->uuid = $uuid->toString(); |
34
|
|
|
$this->label = $label->getValue(); |
35
|
|
|
$this->path = $label->slugify(); |
36
|
|
|
$this->slug = $label->slugify(); |
37
|
|
|
if (null !== $parent) { |
38
|
|
|
$this->parent = $parent; |
39
|
|
|
$this->parent->addChild($this); |
40
|
|
|
$this->path = $parent->slug() . ':' . $label->slugify(); |
41
|
|
|
if (null !== $this->parent->parent) { |
42
|
|
|
$this->path = $this->parent->parent->slug() . ':' . $this->parent->slug() . ':' . $label->slugify(); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function create(FamilyLogUuid $uuid, NameField $name, ?self $parent = null): self |
48
|
|
|
{ |
49
|
|
|
return new self($uuid, $name, $parent); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function uuid(): string |
53
|
|
|
{ |
54
|
|
|
return $this->uuid; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function label(): string |
58
|
|
|
{ |
59
|
|
|
return $this->label; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function parent(): ?self |
63
|
|
|
{ |
64
|
|
|
return $this->parent; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function children(): ?array |
68
|
|
|
{ |
69
|
|
|
return $this->children; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function path(): string |
73
|
|
|
{ |
74
|
|
|
return $this->path; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function slug(): string |
78
|
|
|
{ |
79
|
|
|
return $this->slug; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function parseTree(): array |
83
|
|
|
{ |
84
|
|
|
$arrayChildren = []; |
85
|
|
|
if (null === $this->children) { |
86
|
|
|
return [$this->label => $arrayChildren]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
foreach ($this->children as $child) { |
90
|
|
|
if (null !== $this->hasChildren($child)) { |
91
|
|
|
$arrayChildren[$child->label] = $this->hasChildren($child); |
92
|
|
|
} else { |
93
|
|
|
$arrayChildren[] = $child->label; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return [$this->label => $arrayChildren]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function hasChildren(self $familyLog): ?array |
101
|
|
|
{ |
102
|
|
|
if (null !== $familyLog->children) { |
103
|
|
|
return \array_map(static function (self $child) { |
104
|
|
|
return $child->label; |
105
|
|
|
}, $familyLog->children); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function addChild(self $child): void |
112
|
|
|
{ |
113
|
|
|
$this->children[] = $child; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|