|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Domain\Model\Common\Entities; |
|
6
|
|
|
|
|
7
|
|
|
use Domain\Model\Common\VO\NameField; |
|
8
|
|
|
|
|
9
|
|
|
class FamilyLog |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $name; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string|null |
|
18
|
|
|
*/ |
|
19
|
|
|
private $parent = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var FamilyLog[]|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private $children; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $slug; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $path; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* FamilyLog constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param NameField $name |
|
39
|
|
|
* @param FamilyLog|null $parent |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(NameField $name, ?FamilyLog $parent = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->name = $name->getValue(); |
|
44
|
|
|
$this->path = $name->slugify(); |
|
45
|
|
|
$this->slug = $name->slugify(); |
|
46
|
|
|
if (null !== $parent) { |
|
47
|
|
|
$this->parent = $parent; |
|
|
|
|
|
|
48
|
|
|
$this->parent->addChild($this); |
|
49
|
|
|
$this->path = $parent->slug().':'.$name->slugify(); |
|
50
|
|
|
if (null !== $this->parent->parent) { |
|
51
|
|
|
$this->path = $this->parent->parent->slug().':'.$this->parent->slug().':'.$name->slugify(); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param NameField $name |
|
58
|
|
|
* @param FamilyLog|null $parent |
|
59
|
|
|
* |
|
60
|
|
|
* @return FamilyLog |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function create(NameField $name, ?FamilyLog $parent = null): self |
|
63
|
|
|
{ |
|
64
|
|
|
return new self($name, $parent); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
final public function parent(): FamilyLog |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->parent; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
final public function path(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->path; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
final public function slug(): string |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->slug; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
final public function parseTree(): array |
|
83
|
|
|
{ |
|
84
|
|
|
$arrayChildren = []; |
|
85
|
|
|
foreach ($this->children as $child) { |
|
|
|
|
|
|
86
|
|
|
if (null !== $this->hasChildren($child)) { |
|
87
|
|
|
$arrayChildren[$child->name] = $this->hasChildren($child); |
|
88
|
|
|
} else { |
|
89
|
|
|
$arrayChildren[] = $child->name; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return [$this->name => $arrayChildren]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param FamilyLog $familyLog |
|
98
|
|
|
* |
|
99
|
|
|
* @return array|null |
|
100
|
|
|
*/ |
|
101
|
|
|
private function hasChildren(FamilyLog $familyLog): ?array |
|
102
|
|
|
{ |
|
103
|
|
|
if (null !== $familyLog->children) { |
|
104
|
|
|
return array_map(static function ($child) { |
|
105
|
|
|
return $child->name; |
|
106
|
|
|
}, $familyLog->children); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function addChild(FamilyLog $child): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->children[] = $child; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..