Passed
Push — master ( e0c380...5ead9c )
by Jan
06:48 queued 01:23
created

getGenericNodeTree()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace App\Repository;
24
25
use App\Entity\Base\AbstractStructuralDBElement;
26
use App\Helpers\Trees\StructuralDBElementIterator;
27
use App\Helpers\Trees\TreeViewNode;
28
use RecursiveIteratorIterator;
29
30
class StructuralDBElementRepository extends NamedDBElementRepository
31
{
32
    /**
33
     * Finds all nodes without a parent node. They are our root nodes.
34
     *
35
     * @return AbstractStructuralDBElement[]
36
     */
37
    public function findRootNodes(): array
38
    {
39
        return $this->findBy(['parent' => null], ['name' => 'ASC']);
40
    }
41
42
    /**
43
     * Gets a tree of TreeViewNode elements. The root elements has $parent as parent.
44
     * The treeview is generic, that means the href are null and ID values are set.
45
     *
46
     * @param AbstractStructuralDBElement|null $parent the parent the root elements should have
47
     *
48
     * @return TreeViewNode[]
49
     */
50
    public function getGenericNodeTree(?AbstractStructuralDBElement $parent = null): array
51
    {
52
        $result = [];
53
54
        $entities = $this->findBy(['parent' => $parent], ['name' => 'ASC']);
55
        foreach ($entities as $entity) {
56
            /** @var AbstractStructuralDBElement $entity */
57
            //Make a recursive call to find all children nodes
58
            $children = $this->getGenericNodeTree($entity);
59
            $node = new TreeViewNode($entity->getName(), null, $children);
60
            //Set the ID of this entity to later be able to reconstruct the URL
61
            $node->setId($entity->getID());
62
            $result[] = $node;
63
        }
64
65
        return $result;
66
    }
67
68
    /**
69
     * Gets a flattened hierarchical tree. Useful for generating option lists.
70
     *
71
     * @param AbstractStructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root
72
     *
73
     * @return AbstractStructuralDBElement[] a flattened list containing the tree elements
74
     */
75
    public function toNodesList(?AbstractStructuralDBElement $parent = null): array
76
    {
77
        $result = [];
78
79
        $entities = $this->findBy(['parent' => $parent], ['name' => 'ASC']);
80
81
        $elementIterator = new StructuralDBElementIterator($entities);
82
        $recursiveIterator = new RecursiveIteratorIterator($elementIterator, RecursiveIteratorIterator::SELF_FIRST);
83
        //$result = iterator_to_array($recursiveIterator);
84
85
        //We can not use iterator_to_array here or we get only the parent elements
86
        foreach ($recursiveIterator as $item) {
87
            $result[] = $item;
88
        }
89
90
        return $result;
91
    }
92
93
    /**
94
     * Creates a structure of AbsstractStructuralDBElements from a path separated by $separator, which splits the various levels.
95
     * This function will try to use existing elements, if they are already in the database. If not, they will be created.
96
     * An array of the created elements will be returned, with the last element being the deepest element.
97
     * @param  string  $path
98
     * @param  string  $separator
99
     * @return AbstractStructuralDBElement[]
100
     */
101
    public function getNewEntityFromPath(string $path, string $separator = '->'): array
102
    {
103
        $parent = null;
104
        $result = [];
105
        foreach (explode($separator, $path) as $name) {
106
            $name = trim($name);
107
            if ('' === $name) {
108
                continue;
109
            }
110
111
            //See if we already have an element with this name and parent
112
            $entity = $this->findOneBy(['name' => $name, 'parent' => $parent]);
113
            if (null === $entity) {
114
                $class = $this->getClassName();
115
                /** @var AbstractStructuralDBElement $entity */
116
                $entity = new $class;
117
                $entity->setName($name);
118
                $entity->setParent($parent);
119
            }
120
121
            $result[] = $entity;
122
            $parent = $entity;
123
        }
124
125
        return $result;
126
    }
127
}
128