Skip to content

Commit 94a376d

Browse files
committed
refactor: Client::records -> Client::all
1 parent 9d03e24 commit 94a376d

File tree

2 files changed

+70
-59
lines changed

2 files changed

+70
-59
lines changed

README.md

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,67 @@
55
[![Latest Stable Version](http://img.shields.io/packagist/v/scriptotek/sru-client.svg?style=flat-square)](https://packagist.org/packages/scriptotek/sru-client)
66
[![Total Downloads](http://img.shields.io/packagist/dt/scriptotek/sru-client.svg?style=flat-square)](https://packagist.org/packages/scriptotek/sru-client)
77

8-
## php-sru-client
8+
# php-sru-client
99

10-
Simple PHP package for making [Search/Retrieve via URL](http://www.loc.gov/standards/sru/) (SRU) requests, using the
11-
[Guzzle HTTP client](http://guzzlephp.org/)
12-
and returning
10+
Simple PHP package for making [Search/Retrieve via URL](http://www.loc.gov/standards/sru/) (SRU) requests, using the [Guzzle HTTP client](http://guzzlephp.org/) and returning
1311
[QuiteSimpleXMLElement](//github.com/danmichaelo/quitesimplexmlelement) instances. Includes an iterator to easily iterate over search results, abstracting away the process of making multiple requests.
1412

1513
If you prefer a simple text response, you might have a look at
1614
the [php-sru-search](https://github.com/Zeitschriftendatenbank/php-sru-search) package.
1715

18-
### Install using Composer
16+
## Install using Composer
1917

20-
Add the package to the `require` list of your `composer.json` file.
18+
Make sure you have [Composer](https://getcomposer.org) installed, then run
2119

22-
```json
23-
{
24-
"require": {
25-
"scriptotek/sru-client": "dev-master"
26-
},
27-
}
20+
```bash
21+
composer require scriptotek/alma-client
2822
```
2923

30-
and run `composer install` to get the latest version of the package.
31-
32-
### Laravel 5 integration
33-
34-
In the $providers array add the service providers for this package:
24+
in your project directory to get the latest stable version of the package.
3525

36-
Scriptotek\Sru\Providers\SruServiceProvider::class,
37-
38-
Add the facade of this package to the `$aliases` array:
39-
40-
'SruClient' => Scriptotek\Sru\Facades\SruClient::class,
41-
42-
Publish configuration in Laravel 5:
43-
44-
$ php artisan vendor:publish --provider="Scriptotek\Sru\Providers\SruServiceProvider"
45-
46-
The configuration file is copied to `config/sru.php`.
47-
48-
### Example
26+
## Configuring the client
4927

5028
```php
5129
require_once('vendor/autoload.php');
5230
use Scriptotek\Sru\Client as SruClient;
5331

54-
$url = 'http://sru.bibsys.no/search/biblioholdings';
55-
56-
$client = new SruClient($url, array(
32+
$sru = new SruClient('http://bibsys-network.alma.exlibrisgroup.com/view/sru/47BIBSYS_NETWORK', [
5733
'schema' => 'marcxml',
58-
'version' => '1.1',
59-
'user-agent' => 'MyTool/0.1'
60-
));
34+
'version' => '1.2',
35+
'user-agent' => 'MyTool/0.1',
36+
]);
6137
```
6238

63-
To get the first record matching a query:
64-
```php
65-
$client->first('bs.isbn="0415919118"');
66-
```
67-
The result is a [Record](//scriptotek.github.io/php-sru-client/api_docs/Scriptotek/Sru/Record.html)
68-
object, or `null` if not found.
39+
## Search and retrieve
6940

70-
To iterate over all the results from a `searchRetrieve` query, use the [Records](//scriptotek.github.io/php-sru-client/api_docs/Scriptotek/Sru/Records.html) object returned from `Client::records()`. The first argument is
71-
the CQL query, and the second optional argument is the number of records to fetch for each request (defaults to 10).
41+
To get all the records matching a given CQL query:
7242

7343
```php
74-
$records = $client->records('dc.title="Hello world"');
75-
if ($records->error) {
76-
print 'ERROR: ' . $records->error . "\n";
77-
}
44+
$records = $sru->all('alma.title="Hello world"');
7845
foreach ($records as $record) {
7946
echo "Got record " . $record->position . " of " . $records->numberOfRecords() . "\n";
8047
// processRecord($record->data);
8148
}
8249
```
8350

84-
#### Use explain to get information about servers:
51+
where `$record` is an instance of [Record](//scriptotek.github.io/php-sru-client/api_docs/Scriptotek/Sru/Record.html) and `$record->data` is an instance of [QuiteSimpleXMLElement](https://github.com/danmichaelo/quitesimplexmlelement).
52+
53+
The `all()` method takes care of continuation for you under the hood for you;
54+
the [Records](//scriptotek.github.io/php-sru-client/api_docs/Scriptotek/Sru/Records.html) generator
55+
continues to fetch records until the result set is depleted. A default batch size of 10 is used,
56+
but you can give any number supported by the server as a second argument to the `all()` method.
57+
58+
If you query for some identifier, you can use the convenience method `first()`:
59+
60+
```php
61+
$record = $sru->first('alma.isbn="0415919118"');
62+
```
63+
64+
The result is a [Record](//scriptotek.github.io/php-sru-client/api_docs/Scriptotek/Sru/Record.html)
65+
object, or `null` if not found.
66+
67+
68+
## Use explain to get information about servers
8569

8670
```php
8771
$urls = array(
@@ -93,15 +77,15 @@ $urls = array(
9377

9478
foreach ($urls as $url) {
9579

96-
$client = new SruClient($url, array(
80+
$sru = new SruClient($url, [
9781
'version' => '1.1',
9882
'user-agent' => 'MyTool/0.1'
99-
));
100-
101-
$response = $client->explain();
83+
]);
10284

103-
if ($response->error) {
104-
print 'ERROR: ' . $response->error . "\n";
85+
try {
86+
$response = $sru->explain();
87+
} catch (\Scriptotek\Sru\Exceptions\SruErrorException $e)
88+
print 'ERROR: ' . $e->getMessage() . "\n";
10589
continue;
10690
}
10791

@@ -117,11 +101,28 @@ foreach ($urls as $url) {
117101
}
118102
```
119103

120-
### API documentation
104+
## API documentation
121105

122106
API documentation can be generated using e.g. [Sami](https://github.com/fabpot/sami),
123107
which is included in the dev requirements of `composer.json`.
124108

125109
php vendor/bin/sami.php update sami.config.php -v
126110

127111
You can view it at [scriptotek.github.io/php-sru-client](//scriptotek.github.io/php-sru-client/)
112+
113+
## Laravel 5 integration
114+
115+
In the $providers array add the service providers for this package:
116+
117+
Scriptotek\Sru\Providers\SruServiceProvider::class,
118+
119+
Add the facade of this package to the `$aliases` array:
120+
121+
'SruClient' => Scriptotek\Sru\Facades\SruClient::class,
122+
123+
Publish configuration in Laravel 5:
124+
125+
$ php artisan vendor:publish --provider="Scriptotek\Sru\Providers\SruServiceProvider"
126+
127+
The configuration file is copied to `config/sru.php`.
128+

src/Client.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public function getHttpOptions()
129129
/**
130130
* Perform a searchRetrieve request
131131
*
132+
* @deprecated
132133
* @param string $cql
133134
* @param int $start Start value in result set (optional)
134135
* @param int $count Number of records to request (optional)
@@ -150,14 +151,23 @@ public function search($cql, $start = 1, $count = 10, $extraParams = array())
150151
* Perform a searchRetrieve request and return an iterator over the records
151152
*
152153
* @param string $cql
153-
* @param int $count Number of records to request per request
154+
* @param int $batchSize Number of records to request per request
154155
* @param array $extraParams Extra GET parameters
155156
* @param mixed $httpClient A http client
156157
* @return Records
157158
*/
158-
public function records($cql, $count = 10, $extraParams = array(), $httpClient = null)
159+
public function all($cql, $batchSize = 10, $extraParams = array(), $httpClient = null)
159160
{
160-
return new Records($cql, $this, $count, $extraParams, $httpClient);
161+
return new Records($cql, $this, $batchSize, $extraParams, $httpClient);
162+
}
163+
164+
/**
165+
* Alias for `all()`
166+
* @deprecated
167+
*/
168+
public function records($cql, $batchSize = 10, $extraParams = array(), $httpClient = null)
169+
{
170+
return $this->all($cql, $batchSize = 10, $extraParams = array(), $httpClient);
161171
}
162172

163173
/**

0 commit comments

Comments
 (0)