Skip to content

Commit 5179f5f

Browse files
committed
Add option to configure extra GET parameters
1 parent 7adfd4b commit 5179f5f

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

src/Client.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __construct($url, $options = null, $httpClient = null)
7777
* @param int $count Number of records to request (optional)
7878
* @return string
7979
*/
80-
public function urlTo($cql, $start = 1, $count = 10)
80+
public function urlTo($cql, $start = 1, $count = 10, $extraParams = array())
8181
{
8282
$qs = array(
8383
'operation' => 'searchRetrieve',
@@ -93,6 +93,10 @@ public function urlTo($cql, $start = 1, $count = 10)
9393
$qs['startRecord'] = $start;
9494
}
9595

96+
foreach ($extraParams as $key => $value) {
97+
$qs[$key] = $value;
98+
}
99+
96100
return $this->url . '?' . http_build_query($qs);
97101
}
98102

@@ -129,9 +133,9 @@ public function getHttpOptions()
129133
* @param int $count Number of records to request (optional)
130134
* @return SearchRetrieveResponse
131135
*/
132-
public function search($cql, $start = 1, $count = 10) {
136+
public function search($cql, $start = 1, $count = 10, $extraParams = array()) {
133137

134-
$url = $this->urlTo($cql, $start, $count);
138+
$url = $this->urlTo($cql, $start, $count, $extraParams);
135139
$options = $this->getHttpOptions();
136140

137141
$res = $this->httpClient->get($url, $options)->send();
@@ -148,9 +152,9 @@ public function search($cql, $start = 1, $count = 10) {
148152
* @param mixed $httpClient A http client
149153
* @return Records
150154
*/
151-
public function records($cql, $count = 10, $httpClient = null)
155+
public function records($cql, $count = 10, $extraParams = array(), $httpClient = null)
152156
{
153-
return new Records($cql, $this, $count, $httpClient);
157+
return new Records($cql, $this, $count, $extraParams, $httpClient);
154158
}
155159

156160
/**

src/Records.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Records implements \Iterator {
2525

2626
private $position;
2727
private $count;
28+
private $extraParams;
2829
private $cql;
2930
private $client;
3031
private $lastResponse;
@@ -39,9 +40,10 @@ class Records implements \Iterator {
3940
* @param int $count Number of records to request per request
4041
* @param mixed $httpClient A http client
4142
*/
42-
public function __construct($cql, Client $client, $count = 10, $httpClient = null) {
43+
public function __construct($cql, Client $client, $count = 10, $extraParams = array(), $httpClient = null) {
4344
$this->position = 1;
4445
$this->count = $count; // number of records per request
46+
$this->extraParams = $extraParams;
4547
$this->cql = $cql;
4648
$this->httpClient = $httpClient ?: new HttpClient;
4749
$this->client = $client;
@@ -74,7 +76,7 @@ public function numberOfRecords()
7476
*/
7577
private function fetchMore()
7678
{
77-
$url = $this->client->urlTo($this->cql, $this->position, $this->count);
79+
$url = $this->client->urlTo($this->cql, $this->position, $this->count, $this->extraParams);
7880
$options = $this->client->getHttpOptions();
7981

8082
$res = $this->httpClient->get($url, $options)->send();

tests/ClientTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function testUrlTo()
2020
$sru1 = new Client($this->url);
2121
$expectedUrl1 = $this->url . '?operation=searchRetrieve&version=1.1&recordSchema=marcxml&maximumRecords=10&query=isbn%3D123';
2222
$expectedUrl2 = $this->url . '?operation=searchRetrieve&version=1.1&recordSchema=marcxml&maximumRecords=50&query=isbn%3D123&startRecord=2';
23+
$expectedUrl5 = $this->url . '?operation=searchRetrieve&version=1.1&recordSchema=marcxml&maximumRecords=10&query=isbn%3D123&httpAccept=application%2Fxml';
2324

2425
$sru3 = new Client($this->url, array('schema' => 'CUSTOMSCHEMA'));
2526
$expectedUrl3 = $this->url . '?operation=searchRetrieve&version=1.1&recordSchema=CUSTOMSCHEMA&maximumRecords=10&query=isbn%3D123';
@@ -31,6 +32,7 @@ public function testUrlTo()
3132
$this->assertEquals($expectedUrl2, $sru1->urlTo('isbn=123', 2, 50));
3233
$this->assertEquals($expectedUrl3, $sru3->urlTo('isbn=123'));
3334
$this->assertEquals($expectedUrl4, $sru4->urlTo('isbn=123'));
35+
$this->assertEquals($expectedUrl5, $sru1->urlTo('isbn=123', 1, 10, array('httpAccept' => 'application/xml')));
3436
}
3537

3638
public function testSearch()
@@ -181,7 +183,7 @@ public function testRecords()
181183
$http = $this->httpMockSingleResponse($this->makeDummyResponse(1));
182184

183185
$sru1 = new Client($this->url);
184-
$r = $sru1->records('test', 1, $http);
186+
$r = $sru1->records('test', 1, array(), $http);
185187

186188
$this->assertInstanceOf('Scriptotek\Sru\Records', $r);
187189
}

tests/RecordsTest.php

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

1616
$client = new Client($uri);
17-
$records = new Records($cql, $client, 10, $http);
17+
$records = new Records($cql, $client, 10, array(), $http);
1818
$this->assertNull($records->error);
1919
$this->assertEquals(8, $records->numberOfRecords());
2020
$records->rewind();
@@ -51,7 +51,7 @@ public function testRepeatSameResponse()
5151

5252
// Request only one record in each request
5353
$client = new Client($uri);
54-
$rec = new Records($cql, $client, 1, $http);
54+
$rec = new Records($cql, $client, 1, array(), $http);
5555

5656
// Jumping to position 2 should call fetchMore() and throw
5757
// an InvalidResponseException on getting the same response
@@ -75,7 +75,7 @@ public function testMultipleRequests()
7575
$cql = 'dummy';
7676

7777
$client = new Client($uri);
78-
$records = new Records($cql, $client, 10, $http);
78+
$records = new Records($cql, $client, 10, array(), $http);
7979

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

0 commit comments

Comments
 (0)