Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
NodeMap
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 initialize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setNode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNode
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getParentId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getNodeMapArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\struct\treesearch;
10
11
12use pvc\interfaces\struct\treesearch\NodeMapInterface;
13use pvc\interfaces\struct\treesearch\NodeVisitableInterface;
14
15/**
16 * Class NodeMap
17 *
18 * This is generated as a by-product of a depth first search and as a result of iteration.  It is initialized in the
19 * rewind method of the search. The start node of the search is at depth = 0
20 *
21 * @phpstan-import-type NodeMapRow from NodeMapInterface
22 */
23class NodeMap implements NodeMapInterface
24{
25    /**
26     * @var array<non-negative-int, NodeMapRow>
27     */
28    protected array $nodes = [];
29
30    /**
31     * initialize by putting the start node of the search into the map
32     */
33    public function initialize(NodeVisitableInterface $node): void
34    {
35        $this->nodes = [];
36        $this->setNode($node, null);
37    }
38
39    /**
40     * setNode
41     * @param NodeVisitableInterface $node
42     * @param non-negative-int|null $parentId
43     */
44    public function setNode(NodeVisitableInterface $node, ?int $parentId): void
45    {
46        $this->nodes[$node->getNodeId()] = ['parentId' => $parentId, 'node' => $node];
47    }
48
49    /**
50     * getParent
51     * @param int $nodeId
52     * @return NodeVisitableInterface|null
53     */
54    public function getParent(int $nodeId): ?NodeVisitableInterface
55    {
56        return $this->getNode($this->getParentId($nodeId));
57    }
58
59    /**
60     * getNode
61     * @param ?int $nodeId
62     * @return NodeVisitableInterface|null
63     */
64    public function getNode(?int $nodeId): ?NodeVisitableInterface
65    {
66        $array = $this->nodes[$nodeId] ?? [];
67        return $array['node'] ?? null;
68    }
69
70    /**
71     * getParentId
72     * @param int $nodeId
73     * @return non-negative-int|null
74     */
75    public function getParentId(int $nodeId): ?int
76    {
77        $array = $this->nodes[$nodeId] ?? [];
78        return $array['parentId'] ?? null;
79    }
80
81    /**
82     * @return array<non-negative-int, NodeMapRow>
83     */
84    public function getNodeMapArray(): array
85    {
86        return $this->nodes;
87    }
88}