InMemoryBatchingFetcher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace BatchingIterator\Fetchers;
6
7
use BatchingIterator\BatchingFetcher;
8
9
/**
10
 * Trivial implementation of BatchingFetcher that uses an in-memory array.
11
 * Values are returned in first in, first out fashion.
12
 * (In other words: an adapter from array to BatchingFetcher)
13
 *
14
 * @since 2.0
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class InMemoryBatchingFetcher implements BatchingFetcher {
20
21
	private $values;
22
23 11
	public function __construct( array $values ) {
24 11
		$this->values = $values;
25 11
	}
26
27
	/**
28
	 * @see BatchingFetcher::fetchNext
29
	 *
30
	 * @param int $maxFetchCount
31
	 *
32
	 * @return mixed[]
33
	 */
34 11
	public function fetchNext( int $maxFetchCount ): array {
35 11
		$values = [];
36
37 11
		while ( !is_null( key( $this->values ) ) && --$maxFetchCount >= 0 ) {
38 6
			$values[] = current( $this->values );
39 6
			next( $this->values );
40
		}
41
42 11
		return $values;
43
	}
44
45
	/**
46
	 * @see BatchingFetcher::rewind
47
	 */
48 3
	public function rewind() {
49 3
		reset( $this->values );
50 3
	}
51
52
}