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