1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Wikibase\JsonDumpReader\Iterator; |
6
|
|
|
|
7
|
|
|
use Deserializers\Deserializer; |
8
|
|
|
use Deserializers\Exceptions\DeserializationException; |
9
|
|
|
use Iterator; |
10
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Package private |
14
|
|
|
* |
15
|
|
|
* @licence GNU GPL v2+ |
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
17
|
|
|
*/ |
18
|
|
|
class EntityDumpIterator implements Iterator { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Deserializer |
22
|
|
|
*/ |
23
|
|
|
private $deserializer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Iterator |
27
|
|
|
*/ |
28
|
|
|
private $dumpIterator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var callable|null |
32
|
|
|
*/ |
33
|
|
|
private $errorReporter = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EntityDocument|null |
37
|
|
|
*/ |
38
|
|
|
private $current = null; |
39
|
|
|
|
40
|
9 |
|
public function __construct( Iterator $objectIterator, Deserializer $entityDeserializer ) { |
41
|
9 |
|
$this->dumpIterator = $objectIterator; |
42
|
9 |
|
$this->deserializer = $entityDeserializer; |
43
|
9 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param callable|null $errorReporter |
47
|
|
|
*/ |
48
|
8 |
|
public function onError( callable $errorReporter = null ) { |
49
|
8 |
|
$this->errorReporter = $errorReporter; |
50
|
8 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return EntityDocument|null |
54
|
|
|
*/ |
55
|
5 |
|
public function current() { |
56
|
5 |
|
return $this->current; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return EntityDocument|null |
61
|
|
|
*/ |
62
|
6 |
|
public function next() { |
63
|
6 |
|
$this->dumpIterator->next(); |
64
|
|
|
|
65
|
6 |
|
$this->getCurrentFromObject(); |
66
|
|
|
|
67
|
6 |
|
return $this->current; |
68
|
|
|
} |
69
|
|
|
|
70
|
9 |
|
private function getCurrentFromObject() { |
71
|
9 |
|
while ( true ) { |
72
|
|
|
try { |
73
|
9 |
|
$jsonData = $this->dumpIterator->current(); |
74
|
|
|
|
75
|
9 |
|
if ( $jsonData === null ) { |
76
|
9 |
|
$this->current = null; |
77
|
9 |
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
7 |
|
$this->current = $this->deserializer->deserialize( $jsonData ); |
81
|
6 |
|
return; |
82
|
|
|
} |
83
|
3 |
|
catch ( DeserializationException $ex ) { |
84
|
3 |
|
$this->reportError( $ex->getMessage() ); |
85
|
3 |
|
$this->dumpIterator->next(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
private function reportError( $errorMessage ) { |
91
|
3 |
|
if ( $this->errorReporter !== null ) { |
92
|
1 |
|
call_user_func( $this->errorReporter, $errorMessage ); |
93
|
|
|
} |
94
|
3 |
|
} |
95
|
|
|
|
96
|
|
|
public function key() { |
97
|
|
|
return $this->current->getId()->getSerialization(); |
98
|
|
|
} |
99
|
|
|
|
100
|
9 |
|
public function valid() { |
101
|
9 |
|
return $this->current !== null; |
102
|
|
|
} |
103
|
|
|
|
104
|
9 |
|
public function rewind() { |
105
|
9 |
|
$this->current = null; |
106
|
9 |
|
$this->dumpIterator->rewind(); |
107
|
9 |
|
$this->getCurrentFromObject(); |
108
|
9 |
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|