Skip to content

Commit 6480735

Browse files
committed
Add explain method to the client
1 parent 7ecb9f4 commit 6480735

File tree

3 files changed

+98
-20
lines changed

3 files changed

+98
-20
lines changed

src/Client.php

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ class Client {
3535
*/
3636
protected $credentials;
3737

38-
/**
39-
* Create a new client
40-
*
41-
* @param string $url Base URL to the SRU service
42-
* @param array $options Associative array of options
43-
* @param HttpClient $httpClient
44-
*/
38+
/**
39+
* Create a new client
40+
*
41+
* @param string $url Base URL to the SRU service
42+
* @param array $options Associative array of options
43+
* @param HttpClient $httpClient
44+
*/
4545
public function __construct($url, $options = null, $httpClient = null)
4646
{
4747
$this->url = $url;
@@ -80,8 +80,8 @@ public function __construct($url, $options = null, $httpClient = null)
8080
public function urlTo($cql, $start = 1, $count = 10)
8181
{
8282
$qs = array(
83-
'version' => $this->version,
8483
'operation' => 'searchRetrieve',
84+
'version' => $this->version,
8585
'recordSchema' => $this->schema,
8686
'maximumRecords' => $count,
8787
'query' => $cql
@@ -97,16 +97,12 @@ public function urlTo($cql, $start = 1, $count = 10)
9797
}
9898

9999
/**
100-
* Perform a searchRetrieveResponse request
100+
* Get HTTP client configuration options (authentication, proxy, headers)
101101
*
102-
* @param string $cql
103-
* @param int $start Start value in result set (optional)
104-
* @param int $count Number of records to request (optional)
105-
* @return Response
102+
* @return array
106103
*/
107-
public function search($cql, $start = 1, $count = 10) {
108-
109-
$url = $this->urlTo($cql, $start, $count);
104+
protected function getHttpOptions()
105+
{
110106
$headers = array(
111107
'Accept' => 'application/xml'
112108
);
@@ -122,11 +118,46 @@ public function search($cql, $start = 1, $count = 10) {
122118
if ($this->proxy) {
123119
$options['proxy'] = $this->proxy;
124120
}
121+
return $options;
122+
}
123+
124+
/**
125+
* Perform a searchRetrieve request
126+
*
127+
* @param string $cql
128+
* @param int $start Start value in result set (optional)
129+
* @param int $count Number of records to request (optional)
130+
* @return Response
131+
*/
132+
public function search($cql, $start = 1, $count = 10) {
133+
134+
$url = $this->urlTo($cql, $start, $count);
135+
$options = $this->getHttpOptions();
125136

126137
$res = $this->httpClient->get($url, $options)->send();
127138
$body = $res->getBody(true);
128139

129140
return new Response($body, $this);
141+
}
142+
143+
/**
144+
* Perform an explain request
145+
*
146+
* @return ExplainResponse
147+
*/
148+
public function explain() {
149+
150+
$url = $this->url . '?' . http_build_query(array(
151+
'operation' => 'explain',
152+
'version' => $this->version,
153+
'recordSchema' => $this->schema
154+
));
155+
$options = $this->getHttpOptions();
156+
157+
$res = $this->httpClient->get($url, $options)->send();
158+
$body = $res->getBody(true);
159+
160+
return new ExplainResponse($body, $this);
130161

131162
}
132163

src/ExplainResponse.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php namespace Scriptotek\Sru;
2+
3+
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
4+
5+
/**
6+
* SRU explain response
7+
*/
8+
class ExplainResponse {
9+
10+
/** @var string Raw XML response */
11+
protected $rawResponse;
12+
13+
/**
14+
* Create a new explain response
15+
*
16+
* @param string $text Raw XML response
17+
* @param Client $client SRU client reference (optional)
18+
*/
19+
public function __construct($text, &$client = null)
20+
{
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+
));
32+
33+
$this->client = $client;
34+
}
35+
36+
/**
37+
* Get the raw xml response
38+
*
39+
* @return string
40+
*/
41+
public function asXml()
42+
{
43+
return $this->rawResponse;
44+
}
45+
46+
}
47+

tests/ClientTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ class ClientTest extends TestCase {
1414
public function testUrlTo()
1515
{
1616
$sru1 = new Client($this->url);
17-
$expectedUrl1 = $this->url . '?version=1.1&operation=searchRetrieve&recordSchema=marcxml&maximumRecords=10&query=isbn%3D123';
18-
$expectedUrl2 = $this->url . '?version=1.1&operation=searchRetrieve&recordSchema=marcxml&maximumRecords=50&query=isbn%3D123&startRecord=2';
17+
$expectedUrl1 = $this->url . '?operation=searchRetrieve&version=1.1&recordSchema=marcxml&maximumRecords=10&query=isbn%3D123';
18+
$expectedUrl2 = $this->url . '?operation=searchRetrieve&version=1.1&recordSchema=marcxml&maximumRecords=50&query=isbn%3D123&startRecord=2';
1919

2020
$sru3 = new Client($this->url, array('schema' => 'CUSTOMSCHEMA'));
21-
$expectedUrl3 = $this->url . '?version=1.1&operation=searchRetrieve&recordSchema=CUSTOMSCHEMA&maximumRecords=10&query=isbn%3D123';
21+
$expectedUrl3 = $this->url . '?operation=searchRetrieve&version=1.1&recordSchema=CUSTOMSCHEMA&maximumRecords=10&query=isbn%3D123';
2222

2323
$sru4 = new Client($this->url, array('version' => '0.9'));
24-
$expectedUrl4 = $this->url . '?version=0.9&operation=searchRetrieve&recordSchema=marcxml&maximumRecords=10&query=isbn%3D123';
24+
$expectedUrl4 = $this->url . '?operation=searchRetrieve&version=0.9&recordSchema=marcxml&maximumRecords=10&query=isbn%3D123';
2525

2626
$this->assertEquals($expectedUrl1, $sru1->urlTo('isbn=123'));
2727
$this->assertEquals($expectedUrl2, $sru1->urlTo('isbn=123', 2, 50));

0 commit comments

Comments
 (0)