-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathArrayIterator.php
154 lines (143 loc) · 4.64 KB
/
ArrayIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?hh
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/class.arrayiterator.php )
*
* This iterator allows unsetting and modifying values and keys while
* iterating over Arrays and Objects.
*/
class ArrayIterator implements \HH\Iterator<mixed> {
// $vals will always be list-like. If the original array is list-like, then
// $keys will be null; otherwise, it will contain the original array's keys.
// (This optimization saves memory when iterating over varrays or vecs.)
private ?vec<arraykey> $keys = null;
private AnyArray $vals;
private int $index = 0;
private int $flags;
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/arrayiterator.construct.php )
*
* Constructs an ArrayIterator object. Warning: This function is currently
* not documented; only its argument list is available.
*
* @array mixed The array or object to be iterated on.
*
* @return mixed An ArrayIterator object.
*/
public function __construct(mixed $array, ?int $flags = null) {
if ($array is ArrayIterator) {
$this->keys = $array->keys;
$this->vals = $array->vals;
$this->flags = ($flags === null) ? $array->getFlags() : $flags;
return;
}
if (HH\is_any_array($array)) {
$this->vals = $array;
} else if (gettype($array) === 'object') {
$this->vals = get_object_vars($array);
} else {
throw new InvalidArgumentException(
"ArrayIterator takes an array or object input.",
);
}
// Maintain the invariants on $keys and $vals described above.
if (!HH\is_list_like($this->vals)) {
$this->keys = vec[];
foreach ($this->vals as $key => $_) {
$this->keys[] = $key;
}
$this->vals = vec($this->vals);
}
$this->flags = ($flags === null) ? 0 : $flags;
}
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/arrayiterator.current.php )
*
* Get the current array entry.
*
* @return mixed The current array entry.
*/
public function current(): mixed {
return $this->vals[$this->index] ?? null;
}
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/arrayiterator.getflags.php )
*
* Get the current flags. Warning: This function is currently not
* documented; only its argument list is available.
*
* @return mixed The current flags.
*/
public function getFlags(): int {
return $this->flags;
}
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/arrayiterator.key.php )
*
* This function returns the current array key
*
* @return mixed The current array key.
*/
public function key(): ?arraykey {
$index = $this->index;
if ($index < count($this->vals)) {
$keys = $this->keys;
return $keys is nonnull ? $keys[$index] : $index;
}
return null;
}
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/arrayiterator.next.php )
*
* The iterator to the next entry.
*
* @return mixed No value is returned.
*/
public function next(): void {
if ($this->index < count($this->vals)) $this->index++;
}
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/arrayiterator.rewind.php )
*
* This rewinds the iterator to the beginning.
*
* @return mixed No value is returned.
*/
public function rewind(): void {
$this->index = 0;
}
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/arrayiterator.setflags.php )
*
* Sets behaviour flags. Warning: This function is currently not
* documented; only its argument list is available.
*
* @flags mixed A bitmask as follows: 0 = Properties of the object
* have their normal functionality when accessed as
* list (var_dump, foreach, etc.). 1 = Array indices
* can be accessed as properties in read/write.
*
* @return mixed No value is returned.
*/
public function setFlags(int $flags): void {
$this->flags = $flags;
}
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/arrayiterator.valid.php )
*
* Checks if the array contains any more entries.
*
* @return mixed No value is returned.
*/
public function valid(): bool {
return $this->index < count($this->vals);
}
}