-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathIterableEntityIdPager.php
More file actions
52 lines (45 loc) · 1.05 KB
/
IterableEntityIdPager.php
File metadata and controls
52 lines (45 loc) · 1.05 KB
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
<?php
namespace Wikibase\DataModel\Services\EntityId;
use ArrayIterator;
use Iterator;
use IteratorIterator;
use Wikibase\DataModel\Entity\EntityId;
/**
* An entity ID pager that wraps an iterable and traverses it once.
* It is not seekable or rewindable.
*
* @since 5.3
* @license GPL-2.0-or-later
*/
class IterableEntityIdPager implements EntityIdPager {
/** @var Iterator */
private $iterator;
/**
* @param iterable<EntityId> $iterable
*/
public function __construct( iterable $iterable ) {
if ( $iterable instanceof Iterator ) {
$this->iterator = $iterable;
} elseif ( is_array( $iterable ) ) {
$this->iterator = new ArrayIterator( $iterable );
} else {
$this->iterator = new IteratorIterator( $iterable );
}
$this->iterator->rewind();
}
/**
* @see EntityIdPager::fetchIds
*
* @param int $limit
*
* @return EntityId[]
*/
public function fetchIds( $limit ) {
$ids = [];
while ( $limit-- > 0 && $this->iterator->valid() ) {
$ids[] = $this->iterator->current();
$this->iterator->next();
}
return $ids;
}
}