Completed
Pull Request — master (#239)
by
unknown
02:00
created

IterableEntityIdPager::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\EntityId;
4
5
use ArrayIterator;
6
use Iterator;
7
use IteratorIterator;
8
use Wikibase\DataModel\Entity\EntityId;
9
10
/**
11
 * An entity ID pager that wraps an iterable and traverses it once.
12
 * It is not seekable or rewindable.
13
 *
14
 * @since 5.3
15
 * @license GPL-2.0-or-later
16
 */
17
class IterableEntityIdPager implements EntityIdPager {
18
19
	/** @var Iterator */
20
	private $iterator;
21
22
	/**
23
	 * @param iterable<EntityId> $iterable
0 ignored issues
show
Documentation introduced by
The doc-type iterable<EntityId> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
24
	 */
25
	public function __construct( iterable $iterable ) {
26
		if ( $iterable instanceof Iterator ) {
27
			$this->iterator = $iterable;
28
		} elseif ( is_array( $iterable ) ) {
29
			$this->iterator = new ArrayIterator( $iterable );
30
		} else {
31
			$this->iterator = new IteratorIterator( $iterable );
32
		}
33
		$this->iterator->rewind();
34
	}
35
36
	/**
37
	 * @see EntityIdPager::fetchIds
38
	 *
39
	 * @param int $limit
40
	 *
41
	 * @return EntityId[]
42
	 */
43
	public function fetchIds( $limit ) {
44
		$ids = [];
45
		while ( $limit-- > 0 && $this->iterator->valid() ) {
46
			$ids[] = $this->iterator->current();
47
			$this->iterator->next();
48
		}
49
		return $ids;
50
	}
51
52
}
53