Completed
Push — master ( 356d31...2bbcd7 )
by mw
02:43
created

MappingIterator::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
crap 3
1
<?php
2
3
namespace SMW\Notifications\Iterator;
4
5
use IteratorIterator;
6
use Iterator;
7
use ArrayIterator;
8
use RuntimeException;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class MappingIterator extends IteratorIterator {
17
18
	/**
19
	 * @var callable
20
	 */
21
	private $callback;
22
23
	/**
24
	 * @since 1.0
25
	 *
26
	 * @param Iterator|array $iterator
27
	 * @param callable  $callback
28
	 */
29 4
	public function __construct( $iterator, callable $callback ) {
30
31 4
		if ( is_array( $iterator ) ) {
32 2
			$iterator = new ArrayIterator( $iterator );
33 2
		}
34
35 4
		if ( !$iterator instanceof Iterator ) {
36 1
			throw new RuntimeException( "CallbackIterator expected an Iterator" );
37
		}
38
39 3
		parent::__construct( $iterator );
40 3
		$this->callback = $callback;
41 3
	}
42
43
	/**
44
	 * @since 1.0
45
	 *
46
	 * {@inheritDoc}
47
	 */
48 2
	public function current() {
49 2
		return call_user_func( $this->callback, parent::current() );
50
	}
51
52
}
53