Passed
Pull Request — master (#163)
by Wilmer
17:48 queued 02:54
created

TraversableObject::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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\TestUtility;
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
    protected array $data = [];
16
17
    private int $position = 0;
18
19
    public function __construct(array $array)
20
    {
21
        $this->data = $array;
22
    }
23
24
    /**
25
     * @throws \Exception
26
     */
27
    public function count()
28
    {
29
        throw new \Exception('Count called on object that should only be traversed.');
30
    }
31
32
    public function current()
33
    {
34
        return $this->data[$this->position];
35
    }
36
37
    public function next()
38
    {
39
        $this->position++;
40
    }
41
42
    public function key()
43
    {
44
        return $this->position;
45
    }
46
47
    public function valid()
48
    {
49
        return array_key_exists($this->position, $this->data);
50
    }
51
52
    public function rewind()
53
    {
54
        $this->position = 0;
55
    }
56
}
57