TraverseStepIterator::valid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Smoren\GraphTools\Structs;
4
5
use Smoren\GraphTools\Models\Interfaces\EdgeInterface;
6
use Smoren\GraphTools\Models\Interfaces\VertexInterface;
7
use Smoren\GraphTools\Structs\Interfaces\TraverseStepIteratorInterface;
8
9
/**
10
 * Class EdgeVertexIterator
11
 * @author Smoren <[email protected]>
12
 */
13
class TraverseStepIterator implements TraverseStepIteratorInterface
14
{
15
    /**
16
     * @var array<TraverseStepItem> source to iterate
17
     */
18
    protected array $source;
19
    /**
20
     * @var int iteration pointer
21
     */
22
    protected int $index = 0;
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public static function combine(TraverseStepIteratorInterface ...$iterators): TraverseStepIteratorInterface
28
    {
29
        $source = [];
30
        foreach($iterators as $iterator) {
31
            foreach($iterator as $edge => $vertex) {
32
                $source[] = new TraverseStepItem($edge, $vertex);
33
            }
34
        }
35
        return new TraverseStepIterator($source);
36
    }
37
38
    /**
39
     * EdgeVertexIterator constructor.
40
     * @param array<TraverseStepItem> $source
41
     */
42
    public function __construct(array $source)
43
    {
44
        $this->source = $source;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function current(): VertexInterface
51
    {
52
        return $this->source[$this->index]->getVertex();
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function next(): void
59
    {
60
        $this->index++;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function key(): ?EdgeInterface
67
    {
68
        return $this->source[$this->index]->getEdge();
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function valid(): bool
75
    {
76
        return $this->index < $this->count();
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function rewind(): void
83
    {
84
        $this->index = 0;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function count(): int
91
    {
92
        return count($this->source);
93
    }
94
}
95