Skip to content

Commit 2b9351c

Browse files
committed
Add numberOfRecords() to Records, and make sure it's available from start
1 parent b1d2373 commit 2b9351c

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ and run `composer install` to get the latest version of the package.
3030

3131
```php
3232
require_once('vendor/autoload.php');
33-
use Scriptotek\SruClient;
33+
use Scriptotek\Sru\Client as SruClient;
3434

3535
$url = 'http://sru.bibsys.no/search/biblioholdings';
3636

@@ -40,11 +40,10 @@ $client = new SruClient($url, array(
4040
'user-agent' => 'OpenKat/0.1'
4141
));
4242

43-
$records = array();
44-
$response = $client->search('dc.title="Hello world"');
45-
while ($response && count($response->records) != 0) {
46-
$records[] = array_merge($records, $response->records);
47-
$response = $response->next();
43+
$records = $client->records('dc.title="Hello world"');
44+
foreach ($records as $record) {
45+
echo "Got record " . $record->position . " of " . $records->numberOfRecords() . "\n";
46+
// processRecord($record->data);
4847
}
4948
```
5049

src/Records.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,36 @@ class Records implements \Iterator {
3636
*
3737
* @param string $cql Query
3838
* @param Client $client SRU client reference (optional)
39-
* @param mixed $httpClient A http client
4039
* @param int $count Number of records to request per request
40+
* @param mixed $httpClient A http client
4141
*/
42-
public function __construct($cql, Client $client, $httpClient = null, $count = 10) {
42+
public function __construct($cql, Client $client, $count = 10, $httpClient = null) {
4343
$this->position = 1;
4444
$this->count = $count; // number of records per request
4545
$this->cql = $cql;
4646
$this->httpClient = $httpClient ?: new HttpClient;
4747
$this->client = $client;
48+
$this->fetchMore();
4849
}
4950

5051
/**
5152
* Return error message from last reponse, if any
53+
*
54+
* @return string|null
5255
*/
5356
public function getError()
5457
{
55-
if (isset($this->lastResponse)) {
56-
return $this->lastResponse->error;
57-
}
58-
return null;
58+
return $this->lastResponse->error;
59+
}
60+
61+
/**
62+
* Return the number of records
63+
*
64+
* @return int|null
65+
*/
66+
public function numberOfRecords()
67+
{
68+
return $this->lastResponse->numberOfRecords;
5969
}
6070

6171
/**
@@ -78,14 +88,6 @@ private function fetchMore()
7888
}
7989
}
8090

81-
/**
82-
* Rewind the Iterator to the first element
83-
*/
84-
function rewind() {
85-
$this->position = 1;
86-
$this->fetchMore();
87-
}
88-
8991
/**
9092
* Return the current element
9193
*
@@ -104,6 +106,17 @@ function key() {
104106
return $this->position;
105107
}
106108

109+
/**
110+
* Rewind the Iterator to the first element
111+
*/
112+
function rewind() {
113+
if ($this->position != 1) {
114+
$this->position = 1;
115+
$this->data = array();
116+
$this->fetchMore();
117+
}
118+
}
119+
107120
/**
108121
* Move forward to next element
109122
*/
@@ -114,7 +127,7 @@ function next() {
114127
}
115128
++$this->position;
116129

117-
if (isset($this->lastResponse) && $this->position > $this->lastResponse->numberOfRecords) {
130+
if ($this->position > $this->numberOfRecords()) {
118131
return null;
119132
}
120133

tests/RecordsTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public function testIterating()
1414
$http = $this->httpMockSingleResponse($response);
1515

1616
$client = new Client($uri);
17-
$records = new Records($cql, $client, $http);
17+
$records = new Records($cql, $client, 10, $http);
1818
$this->assertNull($records->getError());
19+
$this->assertEquals(8, $records->numberOfRecords());
1920
$records->rewind();
2021
$this->assertNull($records->getError());
2122

@@ -41,16 +42,21 @@ public function testIterating()
4142
*/
4243
public function testRepeatSameResponse()
4344
{
44-
$response = $this->makeDummyResponse(1);
45+
// Result set contains two records
46+
$response = $this->makeDummyResponse(2, array('maxRecords' => 1));
4547

4648
$http = $this->httpMockSingleResponse($response);
4749
$uri = 'http://localhost';
4850
$cql = 'dummy';
4951

52+
// Request only one record in each request
5053
$client = new Client($uri);
51-
$rec = new Records($cql, $client, $http);
52-
$rec->next();
53-
$rec->next();
54+
$rec = new Records($cql, $client, 1, $http);
55+
56+
// Jumping to position 2 should call fetchMore() and throw
57+
// an InvalidResponseException on getting the same response
58+
// as we got for position 1
59+
$rec->next();
5460
}
5561

5662

@@ -69,7 +75,7 @@ public function testMultipleRequests()
6975
$cql = 'dummy';
7076

7177
$client = new Client($uri);
72-
$records = new Records($cql, $client, $http);
78+
$records = new Records($cql, $client, 10, $http);
7379

7480
$records->rewind();
7581
foreach (range(1, $nrecs) as $n) {

0 commit comments

Comments
 (0)