Passed
Pull Request — develop (#130)
by Laurent
02:51 queued 01:23
created

FamilyLog   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A slug() 0 3 1
A create() 0 3 1
A parseTree() 0 16 4
A parent() 0 3 1
A hasChildren() 0 9 2
A __construct() 0 11 3
A path() 0 3 1
A addChild() 0 3 1
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 Core\Domain\Common\Model\Dependent;
15
16
use Core\Domain\Common\Model\VO\NameField;
17
18
final class FamilyLog
19
{
20
    private string $name;
21
    private ?FamilyLog $parent = null;
22
    /**
23
     * @var FamilyLog[]|null
24
     */
25
    private ?array $children = null;
26
    private string $slug;
27
    private string $path;
28
29
    public function __construct(NameField $name, ?self $parent = null)
30
    {
31
        $this->name = $name->getValue();
32
        $this->path = $name->slugify();
33
        $this->slug = $name->slugify();
34
        if (null !== $parent) {
35
            $this->parent = $parent;
36
            $this->parent->addChild($this);
37
            $this->path = $parent->slug() . ':' . $name->slugify();
38
            if (null !== $this->parent->parent) {
39
                $this->path = $this->parent->parent->slug() . ':' . $this->parent->slug() . ':' . $name->slugify();
40
            }
41
        }
42
    }
43
44
    public static function create(NameField $name, ?self $parent = null): self
45
    {
46
        return new self($name, $parent);
47
    }
48
49
    public function parent(): ?self
50
    {
51
        return $this->parent;
52
    }
53
54
    public function path(): string
55
    {
56
        return $this->path;
57
    }
58
59
    public function slug(): string
60
    {
61
        return $this->slug;
62
    }
63
64
    public function parseTree(): array
65
    {
66
        $arrayChildren = [];
67
        if (null === $this->children) {
68
            return [$this->name => $arrayChildren];
69
        }
70
71
        foreach ($this->children as $child) {
72
            if (null !== $this->hasChildren($child)) {
73
                $arrayChildren[$child->name] = $this->hasChildren($child);
74
            } else {
75
                $arrayChildren[] = $child->name;
76
            }
77
        }
78
79
        return [$this->name => $arrayChildren];
80
    }
81
82
    private function hasChildren(self $familyLog): ?array
83
    {
84
        if (null !== $familyLog->children) {
85
            return \array_map(static function (self $child) {
86
                return $child->name;
87
            }, $familyLog->children);
88
        }
89
90
        return null;
91
    }
92
93
    private function addChild(self $child): void
94
    {
95
        $this->children[] = $child;
96
    }
97
}
98