DataIterator::rewind()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 *
4
 * This file is part of the Aura project for PHP.
5
 *
6
 * @package Aura.Marshal
7
 *
8
 * @license https://opensource.org/licenses/mit-license.php MIT
9
 *
10
 */
11
namespace Aura\Marshal;
12
13
use ArrayAccess;
14
15
/**
16
 *
17
 * An object to allow iteration over the elements of a Data object.
18
 *
19
 * @package Aura.Marshal
20
 * 
21
 * @implements \Iterator<int|string, mixed>
22
 *
23
 */
24
class DataIterator implements \Iterator
25
{
26
    /**
27
     *
28
     * The data over which we are iterating.
29
     *
30
     * @var ArrayAccess<int|string, mixed>
31
     *
32
     */
33
    protected $data;
34
35
    /**
36
     *
37
     * The keys to iterate over in the Data object.
38
     *
39
     * @var array<int|string>
40
     *
41
     */
42
    protected $keys;
43
44
    /**
45
     *
46
     * Is the current iterator position valid?
47
     *
48
     * @var bool
49
     *
50
     */
51
    protected $valid;
52
53
    /**
54
     *
55
     * Constructor.
56
     *
57
     * @param ArrayAccess<int|string, mixed> $data The Data object over which to iterate.
58
     *
59
     * @param array<int|string> $keys The keys in the Data object.
60
     *
61
     */
62
    public function __construct(ArrayAccess $data, array $keys = [])
63
    {
64
        $this->data = $data;
65
        $this->keys = $keys;
66
    }
67
68
    /**
69
     *
70
     * Returns the value at the current iterator position.
71
     *
72
     * @return mixed
73
     *
74
     */
75
    #[\ReturnTypeWillChange]
76
    public function current()
77
    {
78
        return $this->data->offsetGet($this->key());
79
    }
80
81
    /**
82
     *
83
     * Returns the current iterator position.
84
     *
85
     * @return mixed
86
     *
87
     */
88
    #[\ReturnTypeWillChange]
89
    public function key()
90
    {
91
        return current($this->keys);
92
    }
93
94
    /**
95
     *
96
     * Moves the iterator to the next position.
97
     *
98
     * @return void
99
     *
100
     */
101
    #[\ReturnTypeWillChange]
102
    public function next()
103
    {
104
        $this->valid = (next($this->keys) !== false);
105
    }
106
107
    /**
108
     *
109
     * Moves the iterator to the first position.
110
     *
111
     * @return void
112
     *
113
     */
114
    #[\ReturnTypeWillChange]
115
    public function rewind()
116
    {
117
        $this->valid = (reset($this->keys) !== false);
118
    }
119
120
    /**
121
     *
122
     * Is the current iterator position valid?
123
     *
124
     * @return bool
125
     *
126
     */
127
    #[\ReturnTypeWillChange]
128
    public function valid()
129
    {
130
        return $this->valid;
131
    }
132
}
133