ObjectDumpIterator::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
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 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Wikibase\JsonDumpReader\Iterator;
6
7
use Iterator;
8
9
/**
10
 * Package private
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class ObjectDumpIterator implements Iterator {
16
17
	/**
18
	 * @var callable|null
19
	 */
20
	private $errorReporter = null;
21
22
	/**
23
	 * @var string|null
24
	 */
25
	private $current = null;
26
27
	/**
28
	 * @var Iterator
29
	 */
30
	private $stringIterator;
31
32 4
	public function __construct( Iterator $stringIterator ) {
33 4
		$this->stringIterator = $stringIterator;
34 4
	}
35
36
	/**
37
	 * @param callable|null $errorReporter
38
	 */
39 4
	public function onError( callable $errorReporter = null ) {
40 4
		$this->errorReporter = $errorReporter;
41 4
	}
42
43
	/**
44
	 * @return string|null
45
	 */
46 3
	public function current() {
47 3
		return $this->current;
48
	}
49
50
	/**
51
	 * @return string|null
52
	 */
53 4
	public function next() {
54 4
		$this->stringIterator->next();
55 4
		$this->getCurrentFromString();
56
57 4
		return $this->current;
58
	}
59
60 4
	private function getCurrentFromString() {
61 4
		while ( true ) {
62 4
			$jsonString = $this->stringIterator->current();
63
64 4
			if ( $jsonString === null ) {
65 1
				$this->current = null;
66 1
				return;
67
			}
68
69 4
			$data = json_decode( $jsonString, true );
70 4
			if ( $data === null ) {
71 1
				$this->reportError( json_last_error_msg() );
72 1
				$this->stringIterator->next();
73
			}
74
			else {
75 4
				$this->current = $data;
76 4
				return;
77
			}
78
		}
79
	}
80
81 1
	private function reportError( $errorMessage ) {
82 1
		if ( $this->errorReporter !== null ) {
83 1
			call_user_func( $this->errorReporter, $errorMessage );
84
		}
85 1
	}
86
87 2
	public function key() {
88 2
		return $this->stringIterator->key();
89
	}
90
91 1
	public function valid() {
92 1
		return $this->current !== null;
93
	}
94
95 4
	public function rewind() {
96 4
		$this->current = null;
97 4
		$this->stringIterator->rewind();
98 4
		$this->getCurrentFromString();
99 4
	}
100
101
}
102