1
|
|
|
<?php namespace Scriptotek\Sru; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* When iterating, methods are called in the following order: |
5
|
|
|
* |
6
|
|
|
* rewind() |
7
|
|
|
* valid() |
8
|
|
|
* current() |
9
|
|
|
* |
10
|
|
|
* next() |
11
|
|
|
* valid() |
12
|
|
|
* current() |
13
|
|
|
* |
14
|
|
|
* ... |
15
|
|
|
* |
16
|
|
|
* next() |
17
|
|
|
* valid() |
18
|
|
|
*/ |
19
|
|
|
class Records implements \Iterator |
20
|
|
|
{ |
21
|
|
|
private $position; |
22
|
|
|
private $count; |
23
|
|
|
private $extraParams; |
24
|
|
|
private $cql; |
25
|
|
|
private $client; |
26
|
|
|
private $lastResponse; |
27
|
|
|
|
28
|
|
|
private $data = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new records iterator |
32
|
|
|
* |
33
|
|
|
* @param string $cql Query |
34
|
|
|
* @param Client $client SRU client reference (optional) |
35
|
|
|
* @param int $count Number of records to request per request |
36
|
|
|
* @param array $extraParams Extra GET parameters |
37
|
|
|
*/ |
38
|
|
|
public function __construct($cql, Client $client, $count = 10, $extraParams = array()) |
39
|
|
|
{ |
40
|
|
|
$this->position = 1; |
41
|
|
|
$this->count = $count; // number of records per request |
42
|
|
|
$this->extraParams = $extraParams; |
43
|
|
|
$this->cql = $cql; |
44
|
|
|
$this->client = $client; |
45
|
|
|
$this->fetchMore(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Return the number of records |
50
|
|
|
* |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
public function numberOfRecords() |
54
|
|
|
{ |
55
|
|
|
return $this->lastResponse->numberOfRecords; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Fetch more records from the service |
60
|
|
|
*/ |
61
|
|
|
private function fetchMore() |
62
|
|
|
{ |
63
|
|
|
$url = $this->client->urlTo($this->cql, $this->position, $this->count, $this->extraParams); |
64
|
|
|
$body = $this->client->request('GET', $url); |
65
|
|
|
$this->lastResponse = new SearchRetrieveResponse($body); |
66
|
|
|
$this->data = $this->lastResponse->records; |
67
|
|
|
|
68
|
|
|
if (count($this->data) != 0 && $this->data[0]->position != $this->position) { |
69
|
|
|
throw new Exceptions\InvalidResponseException('Wrong index of first record in result set. ' |
70
|
|
|
. 'Expected: ' .$this->position . ', got: ' . $this->data[0]->position |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return the current element |
77
|
|
|
* |
78
|
|
|
* @return Record |
79
|
|
|
*/ |
80
|
|
|
public function current() |
81
|
|
|
{ |
82
|
|
|
return $this->data[0]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the key of the current element |
87
|
|
|
* |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public function key() |
91
|
|
|
{ |
92
|
|
|
return $this->position; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Rewind the Iterator to the first element |
97
|
|
|
*/ |
98
|
|
|
public function rewind() |
99
|
|
|
{ |
100
|
|
|
if ($this->position != 1) { |
101
|
|
|
$this->position = 1; |
102
|
|
|
$this->data = array(); |
103
|
|
|
$this->fetchMore(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Move forward to next element |
109
|
|
|
*/ |
110
|
|
|
public function next() |
111
|
|
|
{ |
112
|
|
|
if (count($this->data) > 0) { |
113
|
|
|
array_shift($this->data); |
114
|
|
|
} |
115
|
|
|
++$this->position; |
116
|
|
|
|
117
|
|
|
if ($this->position > $this->numberOfRecords()) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (count($this->data) == 0) { |
122
|
|
|
$this->fetchMore(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (count($this->data) == 0) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if current position is valid |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function valid() |
136
|
|
|
{ |
137
|
|
|
return count($this->data) != 0; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|