Passed
Push — feature-refactor-orm-less ( d53a1e...2cb1be )
by Laurent
05:34
created

FamilyLog   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 35
c 1
b 1
f 0
dl 0
loc 83
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __construct() 0 11 3
A slug() 0 3 1
A parseTree() 0 16 4
A parent() 0 3 1
A hasChildren() 0 9 2
A path() 0 3 1
A name() 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 name(): string
50
    {
51
        return $this->name;
52
    }
53
54
    public function parent(): ?self
55
    {
56
        return $this->parent;
57
    }
58
59
    public function path(): string
60
    {
61
        return $this->path;
62
    }
63
64
    public function slug(): string
65
    {
66
        return $this->slug;
67
    }
68
69
    public function parseTree(): array
70
    {
71
        $arrayChildren = [];
72
        if (null === $this->children) {
73
            return [$this->name => $arrayChildren];
74
        }
75
76
        foreach ($this->children as $child) {
77
            if (null !== $this->hasChildren($child)) {
78
                $arrayChildren[$child->name] = $this->hasChildren($child);
79
            } else {
80
                $arrayChildren[] = $child->name;
81
            }
82
        }
83
84
        return [$this->name => $arrayChildren];
85
    }
86
87
    private function hasChildren(self $familyLog): ?array
88
    {
89
        if (null !== $familyLog->children) {
90
            return \array_map(static function (self $child) {
91
                return $child->name;
92
            }, $familyLog->children);
93
        }
94
95
        return null;
96
    }
97
98
    private function addChild(self $child): void
99
    {
100
        $this->children[] = $child;
101
    }
102
}
103