Passed
Pull Request — master (#372)
by Wilmer
02:53
created

TraversableObject::rewind()   A

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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support;
6
7
/**
8
 * TraversableObject
9
 *
10
 * Object that implements `\Traversable` and `\Countable`, but counting throws an exception;
11
 * Used for testing support for traversable objects instead of arrays.
12
 */
13
class TraversableObject implements \Iterator, \Countable
14
{
15
    private int $position = 0;
16
17
    public function __construct(protected array $data)
18
    {
19
    }
20
21
    /**
22
     * @throws \Exception
23
     */
24
    public function count(): int
25
    {
26
        throw new \Exception('Count called on object that should only be traversed.');
27
    }
28
29
    public function current(): mixed
30
    {
31
        return $this->data[$this->position];
32
    }
33
34
    public function next(): void
35
    {
36
        $this->position++;
37
    }
38
39
    public function key(): mixed
40
    {
41
        return $this->position;
42
    }
43
44
    public function valid(): bool
45
    {
46
        return array_key_exists($this->position, $this->data);
47
    }
48
49
    public function rewind(): void
50
    {
51
        $this->position = 0;
52
    }
53
}
54