Skip to content

Commit 049e730

Browse files
committed
Rename Response → SearchRetrieveResponse and extend basic Response class
1 parent 20d5d2f commit 049e730

File tree

8 files changed

+104
-73
lines changed

8 files changed

+104
-73
lines changed

src/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function getHttpOptions()
127127
* @param string $cql
128128
* @param int $start Start value in result set (optional)
129129
* @param int $count Number of records to request (optional)
130-
* @return Response
130+
* @return SearchRetrieveResponse
131131
*/
132132
public function search($cql, $start = 1, $count = 10) {
133133

@@ -137,7 +137,7 @@ public function search($cql, $start = 1, $count = 10) {
137137
$res = $this->httpClient->get($url, $options)->send();
138138
$body = $res->getBody(true);
139139

140-
return new Response($body, $this);
140+
return new SearchRetrieveResponse($body, $this);
141141
}
142142

143143
/**

src/ExplainResponse.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
44

55
/**
6-
* SRU explain response
6+
* Explain response
77
*/
8-
class ExplainResponse {
9-
10-
/** @var string Raw XML response */
11-
protected $rawResponse;
8+
class ExplainResponse extends Response implements ResponseInterface {
129

1310
/**
1411
* Create a new explain response
@@ -18,29 +15,10 @@ class ExplainResponse {
1815
*/
1916
public function __construct($text, &$client = null)
2017
{
21-
$this->rawResponse = $text;
22-
try {
23-
$doc = new QuiteSimpleXMLElement($text);
24-
} catch (\Exception $e) {
25-
throw new \Exception('Invalid XML received');
26-
}
27-
28-
$doc->registerXPathNamespaces(array(
29-
'srw' => 'http://www.loc.gov/zing/srw/',
30-
'd' => 'http://www.loc.gov/zing/srw/diagnostic/'
31-
));
18+
parent::__construct($text, $client);
3219

33-
$this->client = $client;
34-
}
20+
// TODO
3521

36-
/**
37-
* Get the raw xml response
38-
*
39-
* @return string
40-
*/
41-
public function asXml()
42-
{
43-
return $this->rawResponse;
4422
}
4523

4624
}

src/Records.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function fetchMore()
6868

6969
$res = $this->httpClient->get($url, $options)->send();
7070
$body = $res->getBody(true);
71-
$this->lastResponse = new Response($body);
71+
$this->lastResponse = new SearchRetrieveResponse($body);
7272
$this->data = $this->lastResponse->records;
7373

7474
if (count($this->data) != 0 && $this->data[0]->position != $this->position) {

src/Response.php

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,25 @@
33
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
44

55
/**
6-
* SRU response, containing a list of records or some error
6+
* Generic SRU response
77
*/
8-
class Response {
8+
class Response implements ResponseInterface {
99

1010
/** @var string Raw XML response */
1111
protected $rawResponse;
1212

13+
/** @var QuiteSimpleXMLElement XML response */
14+
protected $response;
15+
1316
/** @var Client Reference to SRU client object */
1417
protected $client;
1518

16-
/** @var Record[] Array of records */
17-
public $records;
18-
1919
/** @var string Error message */
2020
public $error;
2121

2222
/** @var string SRU protocol version */
2323
public $version;
2424

25-
/** @var int Total number of records in the result set */
26-
public $numberOfRecords;
27-
28-
/** @var int Position of next record in the result set, or null if no such record exist */
29-
public $nextRecordPosition;
30-
3125
/** @var string The CQL query used to generate the response */
3226
public $query;
3327

@@ -46,30 +40,25 @@ public function __construct($text, &$client = null)
4640
throw new \Exception('Invalid XML received');
4741
}
4842

43+
$this->client = $client;
44+
4945
$doc->registerXPathNamespaces(array(
5046
'srw' => 'http://www.loc.gov/zing/srw/',
5147
'd' => 'http://www.loc.gov/zing/srw/diagnostic/'
5248
));
5349

5450
$this->version = $doc->text('/srw:searchRetrieveResponse/srw:version');
55-
$this->numberOfRecords = (int) $doc->text('/srw:searchRetrieveResponse/srw:numberOfRecords');
56-
$this->nextRecordPosition = (int) $doc->text('/srw:searchRetrieveResponse/srw:nextRecordPosition') ?: null;
57-
58-
$this->records = array();
59-
foreach ($doc->xpath('/srw:searchRetrieveResponse/srw:records/srw:record') as $record) {
60-
$this->records[] = new Record($record);
61-
}
6251

6352
$e = $doc->first('/srw:searchRetrieveResponse/srw:diagnostics');
6453
if ($e) {
6554
$this->error = $e->text('d:diagnostic/d:message') . '. ' . $e->text('d:diagnostic/d:details');
6655
}
6756

68-
$this->client = $client;
69-
7057
// The server may echo the request back to the client along with the response
7158
$this->query = $doc->text('/srw:searchRetrieveResponse/srw:echoedSearchRetrieveRequest/srw:query') ?: null;
7259

60+
$this->response = $doc;
61+
7362
}
7463

7564
/**
@@ -82,24 +71,5 @@ public function asXml()
8271
return $this->rawResponse;
8372
}
8473

85-
/**
86-
* Request next batch of records in the result set, or return null if we're at the end of the set
87-
*
88-
* @return Response
89-
*/
90-
public function next()
91-
{
92-
if (is_null($this->client)) {
93-
throw new \Exception('No client reference passed to response');
94-
}
95-
if (is_null($this->query)) {
96-
throw new \Exception('No query available');
97-
}
98-
if (is_null($this->nextRecordPosition)) {
99-
return null;
100-
}
101-
return $this->client->search($this->query, $this->nextRecordPosition, count($this->records));
102-
}
103-
10474
}
10575

src/ResponseInterface.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace Scriptotek\Sru;
2+
3+
/**
4+
* Interface defining data objects that hold the information of an SRU response
5+
*/
6+
interface ResponseInterface {
7+
8+
/**
9+
* Create a new response
10+
*
11+
* @param string $text Raw XML response
12+
* @param Client $client SRU client reference (optional)
13+
*/
14+
public function __construct($text, &$client = null);
15+
16+
/**
17+
* Get the raw xml response
18+
*
19+
* @return string
20+
*/
21+
public function asXml();
22+
23+
}
24+

src/SearchRetrieveResponse.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php namespace Scriptotek\Sru;
2+
3+
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
4+
5+
/**
6+
* SearchRetrieve response, containing a list of records or some error
7+
*/
8+
class SearchRetrieveResponse extends Response implements ResponseInterface {
9+
10+
/** @var Record[] Array of records */
11+
public $records;
12+
13+
/** @var int Total number of records in the result set */
14+
public $numberOfRecords;
15+
16+
/** @var int Position of next record in the result set, or null if no such record exist */
17+
public $nextRecordPosition;
18+
19+
/**
20+
* Create a new searchRetrieve response
21+
*
22+
* @param string $text Raw XML response
23+
* @param Client $client SRU client reference (optional)
24+
*/
25+
public function __construct($text, &$client = null)
26+
{
27+
parent::__construct($text, $client);
28+
29+
$this->numberOfRecords = (int) $this->response->text('/srw:searchRetrieveResponse/srw:numberOfRecords');
30+
$this->nextRecordPosition = (int) $this->response->text('/srw:searchRetrieveResponse/srw:nextRecordPosition') ?: null;
31+
32+
$this->records = array();
33+
foreach ($this->response->xpath('/srw:searchRetrieveResponse/srw:records/srw:record') as $record) {
34+
$this->records[] = new Record($record);
35+
}
36+
}
37+
38+
/**
39+
* Request next batch of records in the result set, or return null if we're at the end of the set
40+
*
41+
* @return Response
42+
*/
43+
public function next()
44+
{
45+
if (is_null($this->client)) {
46+
throw new \Exception('No client reference passed to response');
47+
}
48+
if (is_null($this->query)) {
49+
throw new \Exception('No query available');
50+
}
51+
if (is_null($this->nextRecordPosition)) {
52+
return null;
53+
}
54+
return $this->client->search($this->query, $this->nextRecordPosition, count($this->records));
55+
}
56+
57+
}
58+

tests/ClientTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function testSearchWithAuth()
6060
);
6161
$sru = new Client($this->url, $options, $http);
6262

63-
$sru->search('test');
63+
$r = $sru->search('test');
64+
$this->assertInstanceOf('Scriptotek\Sru\SearchRetrieveResponse', $r);
6465
}
6566

6667
public function testNext()

tests/ResponseTest.php renamed to tests/SearchRetrieveResponseTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
use \Guzzle\Http\Message\Response as HttpResponse;
44
use \Mockery as m;
55

6-
class ResponseTest extends TestCase {
6+
class SearchRetrieveResponseTest extends TestCase {
77

88
public function testSingleRecordResult()
99
{
10-
$res = new Response('<?xml version="1.0" encoding="UTF-8" ?>
10+
$res = new SearchRetrieveResponse('<?xml version="1.0" encoding="UTF-8" ?>
1111
<srw:searchRetrieveResponse
1212
xmlns:srw="http://www.loc.gov/zing/srw/"
1313
xmlns:xcql="http://www.loc.gov/zing/cql/xcql/"
@@ -49,7 +49,7 @@ public function testSingleRecordResult()
4949

5050
public function testMultipleRecordsResult()
5151
{
52-
$res = new Response('<?xml version="1.0" encoding="UTF-8" ?>
52+
$res = new SearchRetrieveResponse('<?xml version="1.0" encoding="UTF-8" ?>
5353
<srw:searchRetrieveResponse
5454
xmlns:srw="http://www.loc.gov/zing/srw/"
5555
xmlns:xcql="http://www.loc.gov/zing/cql/xcql/"
@@ -98,7 +98,7 @@ public function testMultipleRecordsResult()
9898

9999
public function testError()
100100
{
101-
$res = new Response('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
101+
$res = new SearchRetrieveResponse('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
102102
<srw:version>1.1</srw:version>
103103
<srw:numberOfRecords>0</srw:numberOfRecords>
104104
<srw:diagnostics xmlns="http://www.loc.gov/zing/srw/diagnostic/">

0 commit comments

Comments
 (0)