Skip to content

Commit 9ce2cbf

Browse files
committed
Add AsListIterator to remove gaps in arrays
1 parent 0c1b8a6 commit 9ce2cbf

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

psalm-baseline.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
<code><![CDATA[$context[$this->protocol]['options']]]></code>
6969
</MixedArrayAccess>
7070
</file>
71+
<file src="src/Model/AsListIterator.php">
72+
<InvalidTemplateParam occurrences="1">
73+
<code>AsListIterator</code>
74+
</InvalidTemplateParam>
75+
</file>
7176
<file src="src/Model/BSONArray.php">
7277
<MixedAssignment>
7378
<code>$this[$key]</code>

src/Model/AsListIterator.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/*
3+
* Copyright 2023-present MongoDB, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace MongoDB\Model;
19+
20+
use IteratorIterator;
21+
use Traversable;
22+
23+
/**
24+
* @internal
25+
*
26+
* @template TValue
27+
* @template-extends IteratorIterator<int, TValue, Traversable<mixed, TValue>>
28+
*/
29+
final class AsListIterator extends IteratorIterator
30+
{
31+
private int $index = 0;
32+
33+
public function key(): int
34+
{
35+
return $this->index;
36+
}
37+
38+
public function next(): void
39+
{
40+
$this->index++;
41+
42+
parent::next();
43+
}
44+
45+
public function rewind(): void
46+
{
47+
$this->index = 0;
48+
49+
parent::rewind();
50+
}
51+
}

0 commit comments

Comments
 (0)