Skip to content

Commit 518cf2f

Browse files
committed
Update for PHP 8.1
1 parent 6affe2c commit 518cf2f

File tree

7 files changed

+94
-111
lines changed

7 files changed

+94
-111
lines changed

src/Client.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,28 @@ class Client
5353
/**
5454
* Create a new client
5555
*
56-
* @param string $url Base URL to the SRU service
57-
* @param array $options Associative array of options
58-
* @param ClientInterface $httpClient
59-
* @param RequestFactoryInterface $requestFactory
56+
* @param string $url Base URL to the SRU service
57+
* @param ?array $options Associative array of options
58+
* @param ?ClientInterface $httpClient
59+
* @param ?RequestFactoryInterface $requestFactory
6060
* @throws \ErrorException
6161
*/
6262
public function __construct(
63-
$url,
64-
$options = null,
63+
string $url,
64+
?array $options = null,
6565
ClientInterface $httpClient = null,
6666
RequestFactoryInterface $requestFactory = null
6767
) {
6868
$this->url = $url;
69-
$options = $options ?: array();
69+
$options = $options ?: [];
7070

7171
$plugins = [];
7272

73-
$this->schema = isset($options['schema'])
74-
? $options['schema']
75-
: 'marcxml';
73+
$this->schema = $options['schema'] ?? 'marcxml';
7674

77-
$this->version = isset($options['version'])
78-
? $options['version']
79-
: '1.1';
75+
$this->version = $options['version'] ?? '1.1';
8076

81-
$this->headers = isset($options['headers'])
82-
? $options['headers']
83-
: ['Accept' => 'application/xml'];
77+
$this->headers = $options['headers'] ?? ['Accept' => 'application/xml'];
8478

8579
if (isset($options['user-agent'])) {
8680
// legacy option
@@ -93,7 +87,7 @@ public function __construct(
9387
}
9488

9589
if (isset($options['proxy'])) {
96-
throw new\ErrorException('Not supported');
90+
throw new \ErrorException('Not supported');
9791
}
9892

9993
$this->httpClient = new PluginClient($httpClient ?: HttpClient::client(), $plugins);
@@ -109,7 +103,7 @@ public function __construct(
109103
* @param array $extraParams Extra GET parameters
110104
* @return string
111105
*/
112-
public function urlTo($cql, $start = 1, $count = 10, $extraParams = array())
106+
public function urlTo(string $cql, int $start = 1, int $count = 10, array $extraParams = []): string
113107
{
114108
$qs = array(
115109
'operation' => 'searchRetrieve',
@@ -135,14 +129,14 @@ public function urlTo($cql, $start = 1, $count = 10, $extraParams = array())
135129
/**
136130
* Perform a searchRetrieve request
137131
*
138-
* @deprecated
139132
* @param string $cql
140133
* @param int $start Start value in result set (optional)
141134
* @param int $count Number of records to request (optional)
142135
* @param array $extraParams Extra GET parameters
143136
* @return SearchRetrieveResponse
137+
*@deprecated
144138
*/
145-
public function search($cql, $start = 1, $count = 10, $extraParams = array())
139+
public function search(string $cql, int $start = 1, int $count = 10, array $extraParams = []): SearchRetrieveResponse
146140
{
147141
$url = $this->urlTo($cql, $start, $count, $extraParams);
148142
$body = $this->request('GET', $url);
@@ -158,20 +152,20 @@ public function search($cql, $start = 1, $count = 10, $extraParams = array())
158152
* @param array $extraParams Extra GET parameters
159153
* @return Records
160154
*/
161-
public function all($cql, $batchSize = 10, $extraParams = array())
155+
public function all(string $cql, int $batchSize = 10, array $extraParams = []): Records
162156
{
163157
return new Records($cql, $this, $batchSize, $extraParams);
164158
}
165159

166160
/**
167161
* Alias for `all()`
168-
* @deprecated
169-
* @param $cql
162+
* @param string $cql
170163
* @param int $batchSize
171164
* @param array $extraParams
172165
* @return Records
166+
*@deprecated
173167
*/
174-
public function records($cql, $batchSize = 10, $extraParams = array())
168+
public function records(string $cql, int $batchSize = 10, array $extraParams = []): Records
175169
{
176170
return $this->all($cql, $batchSize, $extraParams);
177171
}
@@ -181,9 +175,9 @@ public function records($cql, $batchSize = 10, $extraParams = array())
181175
*
182176
* @param string $cql
183177
* @param array $extraParams Extra GET parameters
184-
* @return Record
178+
* @return ?Record
185179
*/
186-
public function first($cql, $extraParams = array())
180+
public function first(string $cql, array $extraParams = []): ?Record
187181
{
188182
$recs = new Records($cql, $this, 1, $extraParams);
189183
return $recs->numberOfRecords() ? $recs->current() : null;
@@ -194,7 +188,7 @@ public function first($cql, $extraParams = array())
194188
*
195189
* @return ExplainResponse
196190
*/
197-
public function explain()
191+
public function explain(): ExplainResponse
198192
{
199193
$url = $this->url . '?' . http_build_query(array(
200194
'operation' => 'explain',
@@ -211,7 +205,7 @@ public function explain()
211205
* @param string $url
212206
* @return string
213207
*/
214-
public function request($method, $url)
208+
public function request(string $method, string $url): string
215209
{
216210
$request = $this->requestFactory->createRequest($method, $url, $this->headers);
217211
$response = $this->httpClient->sendRequest($request);

src/ExplainResponse.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@
77
*/
88
class ExplainResponse extends Response implements ResponseInterface
99
{
10-
/** @var string Server hostname */
11-
public $host;
10+
/** @var string|null Server hostname */
11+
public ?string $host = null;
1212

1313
/** @var int Server port */
14-
public $port;
14+
public int $port;
1515

1616
/** @var object Server database */
17-
public $database;
17+
public object $database;
1818

1919
/** @var array Server indexes */
20-
public $indexes;
20+
public array $indexes;
2121

2222
/**
2323
* Create a new explain response
2424
*
25-
* @param string $text Raw XML response
26-
* @param Client $client SRU client reference (optional)
27-
* @param string $url Request URL
25+
* @param string|null $text Raw XML response
26+
* @param Client|null $client SRU client reference (optional)
27+
* @param string|null $url
2828
*/
29-
public function __construct($text = null, &$client = null, $url = null)
29+
public function __construct(string $text = null, Client &$client = null, string $url = null)
3030
{
3131
parent::__construct($text, $client, $url);
3232

33-
$this->indexes = array();
33+
$this->indexes = [];
3434

3535
if (is_null($this->response)) {
3636
return;
@@ -62,7 +62,7 @@ protected function parseExplainResponse(QuiteSimpleXMLElement $node)
6262
$ind->search = ($index->attr('search') == 'true');
6363
$ind->sort = ($index->attr('sort') == 'true');
6464
$ind->title = $index->text('exp:title');
65-
$ind->maps = array();
65+
$ind->maps = [];
6666
foreach ($index->xpath('exp:map') as $map) {
6767
$set = $map->first('exp:name')->attr('set');
6868
$name = $map->text('exp:name');

src/Record.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
class Record
99
{
1010
/** @var int */
11-
public $position;
11+
public int $position;
1212

1313
/** @var string */
14-
public $packing;
14+
public string $packing;
1515

1616
/** @var string */
17-
public $schema;
17+
public string $schema;
1818

19-
/** @var QuiteSimpleXMLElement */
20-
public $data;
19+
/** @var ?QuiteSimpleXMLElement */
20+
public ?QuiteSimpleXMLElement $data;
2121

22-
static public $recordTpl = '<s:record xmlns:s="http://www.loc.gov/zing/srw/">
22+
static public string $recordTpl = '<s:record xmlns:s="http://www.loc.gov/zing/srw/">
2323
<s:recordSchema>{{recordSchema}}</s:recordSchema>
2424
<s:recordPacking>{{recordPacking}}</s:recordPacking>
2525
<s:recordPosition>{{position}}</s:recordPosition>
@@ -45,7 +45,12 @@ public function __construct(QuiteSimpleXMLElement $doc)
4545
* @param string $recordPacking
4646
* @return Record
4747
*/
48-
public static function make($position, $data, $recordSchema='marcxchange', $recordPacking='xml')
48+
public static function make(
49+
int $position,
50+
string|QuiteSimpleXMLElement $data,
51+
string $recordSchema='marcxchange',
52+
string $recordPacking='xml'
53+
): Record
4954
{
5055
$record = str_replace(
5156
array('{{position}}', '{{data}}', '{{recordSchema}}', '{{recordPacking}}'),

src/Records.php

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
*/
1919
class Records implements \Iterator
2020
{
21-
private $position;
22-
private $count;
23-
private $extraParams;
24-
private $cql;
25-
private $client;
26-
private $lastResponse;
27-
28-
private $data = array();
21+
private int $position;
22+
private int $count;
23+
private array $extraParams;
24+
private string $cql;
25+
private Client $client;
26+
private SearchRetrieveResponse $lastResponse;
27+
private array $data;
2928

3029
/**
3130
* Create a new records iterator
@@ -35,9 +34,10 @@ class Records implements \Iterator
3534
* @param int $count Number of records to request per request
3635
* @param array $extraParams Extra GET parameters
3736
*/
38-
public function __construct($cql, Client $client, $count = 10, $extraParams = array())
37+
public function __construct(string $cql, Client $client, int $count = 10, array $extraParams = [])
3938
{
4039
$this->position = 1;
40+
$this->data = [];
4141
$this->count = $count; // number of records per request
4242
$this->extraParams = $extraParams;
4343
$this->cql = $cql;
@@ -47,18 +47,16 @@ public function __construct($cql, Client $client, $count = 10, $extraParams = ar
4747

4848
/**
4949
* Return the number of records
50-
*
51-
* @return int
5250
*/
53-
public function numberOfRecords()
51+
public function numberOfRecords(): int
5452
{
5553
return $this->lastResponse->numberOfRecords;
5654
}
5755

5856
/**
5957
* Fetch more records from the service
6058
*/
61-
private function fetchMore()
59+
private function fetchMore(): void
6260
{
6361
$url = $this->client->urlTo($this->cql, $this->position, $this->count, $this->extraParams);
6462
$body = $this->client->request('GET', $url);
@@ -74,40 +72,36 @@ private function fetchMore()
7472

7573
/**
7674
* Return the current element
77-
*
78-
* @return Record
7975
*/
80-
public function current()
76+
public function current(): Record
8177
{
8278
return $this->data[0];
8379
}
8480

8581
/**
8682
* Return the key of the current element
87-
*
88-
* @return int
8983
*/
90-
public function key()
84+
public function key(): int
9185
{
9286
return $this->position;
9387
}
9488

9589
/**
9690
* Rewind the Iterator to the first element
9791
*/
98-
public function rewind()
92+
public function rewind(): void
9993
{
10094
if ($this->position != 1) {
10195
$this->position = 1;
102-
$this->data = array();
96+
$this->data = [];
10397
$this->fetchMore();
10498
}
10599
}
106100

107101
/**
108102
* Move forward to next element
109103
*/
110-
public function next()
104+
public function next(): void
111105
{
112106
if (count($this->data) > 0) {
113107
array_shift($this->data);
@@ -121,18 +115,12 @@ public function next()
121115
if (count($this->data) == 0) {
122116
$this->fetchMore();
123117
}
124-
125-
if (count($this->data) == 0) {
126-
return;
127-
}
128118
}
129119

130120
/**
131121
* Check if current position is valid
132-
*
133-
* @return bool
134122
*/
135-
public function valid()
123+
public function valid(): bool
136124
{
137125
return count($this->data) != 0;
138126
}

0 commit comments

Comments
 (0)