Passed
Push — feature-family-log ( 655216 )
by Laurent
01:49
created

FamilyLog::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
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 $name;
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 $name, ?self $parent = null)
32
    {
33
        $this->uuid = $uuid->toString();
34
        $this->name = $name->getValue();
35
        $this->path = $name->slugify();
36
        $this->slug = $name->slugify();
37
        if (null !== $parent) {
38
            $this->parent = $parent;
39
            $this->parent->addChild($this);
40
            $this->path = $parent->slug() . ':' . $name->slugify();
41
            if (null !== $this->parent->parent) {
42
                $this->path = $this->parent->parent->slug() . ':' . $this->parent->slug() . ':' . $name->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 name(): string
58
    {
59
        return $this->name;
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->name => $arrayChildren];
87
        }
88
89
        foreach ($this->children as $child) {
90
            if (null !== $this->hasChildren($child)) {
91
                $arrayChildren[$child->name] = $this->hasChildren($child);
92
            } else {
93
                $arrayChildren[] = $child->name;
94
            }
95
        }
96
97
        return [$this->name => $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->name;
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