Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchBreadthFirst
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 rewind
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 next
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\struct\treesearch;
10
11use pvc\interfaces\struct\treesearch\NodeSearchableCollectionFactoryInterface;
12use pvc\interfaces\struct\treesearch\NodeSearchableCollectionInterface;
13use pvc\interfaces\struct\treesearch\NodeSearchableInterface;
14use pvc\struct\treesearch\err\StartNodeUnsetException;
15
16/**
17 * Class SearchBreadthFirst
18 *
19 * @template NodeIdType of array-key
20 * @template NodeType of NodeSearchableInterface
21 * @extends SearchAbstract<NodeIdType, NodeType, NodeSearchableCollectionInterface>
22 */
23class SearchBreadthFirst extends SearchAbstract
24{
25    /**
26     * nodes in the "current level" of the tree.
27     *
28     * @var NodeSearchableCollectionInterface<NodeIdType, NodeType>
29     */
30    private NodeSearchableCollectionInterface $currentLevelNodes;
31
32    public function __construct(NodeSearchableCollectionFactoryInterface $collectionFactory)
33    {
34        parent::__construct($collectionFactory);
35        $this->currentLevelNodes = $collectionFactory->makeCollection();
36    }
37
38    /**
39     * rewind
40     *
41     * @throws StartNodeUnsetException
42     */
43    public function rewind(): void
44    {
45        parent::rewind();
46        $this->currentLevelNodes->initialize();
47        $this->currentLevelNodes->add($this->getStartNode());
48        $this->next();
49    }
50
51    /**
52     * next
53     */
54    public function next(): void
55    {
56        /**
57         * If there are no nodes at the current level, or we are at the maximum level
58         * permitted in the search and there are no more nodes at this level to
59         * process, set valid to false and return
60         */
61        if (empty($this->currentLevelNodes) || $this->atMaxLevels()) {
62            $this->invalidate();
63            return;
64        }
65
66        /**
67         * Move to the next node in the current level. Check valid and if not valid,
68         * replenish currentLevelNodes with the next level of nodes, change the
69         * current level and go next again. Otherwise, set the current node.
70         * et
71         */
72        $this->currentLevelNodes->next();
73
74        if (!$this->currentLevelNodes->valid()) {
75            $this->currentLevelNodes = $this->currentLevelNodes->getChildren();
76            $this->setCurrentLevel(-Direction::MOVE_DOWN->value);
77            $this->next();
78        } else {
79            $this->setCurrent($this->currentLevelNodes->current());
80        }
81    }
82}