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

IterableEntityIdPager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A fetchIds() 0 8 3
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
 */
16
class IterableEntityIdPager implements EntityIdPager {
17
18
	/** @var Iterator */
19
	private $iterator;
20
21
	/**
22
	 * @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...
23
	 */
24
	public function __construct( iterable $iterable ) {
25
		if ( $iterable instanceof Iterator ) {
26
			$this->iterator = $iterable;
27
		} elseif ( is_array( $iterable ) ) {
28
			$this->iterator = new ArrayIterator( $iterable );
29
		} else {
30
			$this->iterator = new IteratorIterator( $iterable );
31
		}
32
		$this->iterator->rewind();
33
	}
34
35
	/**
36
	 * @see EntityIdPager::fetchIds
37
	 *
38
	 * @param int $limit
39
	 *
40
	 * @return EntityId[]
41
	 */
42
	public function fetchIds( $limit ) {
43
		$ids = [];
44
		while ( $limit-- > 0 && $this->iterator->valid() ) {
45
			$ids[] = $this->iterator->current();
46
			$this->iterator->next();
47
		}
48
		return $ids;
49
	}
50
51
}
52