-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathEndpoint.php
217 lines (194 loc) · 7.18 KB
/
Endpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* PHPOAIPMH Library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/phpoaipmh
* @version 3.0
* @package caseyamcl/phpoaipmh
* @author Casey McLaughlin <caseyamcl@gmail.com>
*
* For the full copyright and license information, -please view the LICENSE.md
* file that was distributed with this source code.
*
* ------------------------------------------------------------------
*/
namespace Phpoaipmh;
/**
* OAI-PMH Endpoint Class
*
* @since v1.0
* @author Casey McLaughlin <caseyamcl@gmail.com>
*/
class Endpoint implements EndpointInterface
{
const AUTO = null;
/**
* @var Client
*/
private $client;
/**
* @var string
*/
private $granularity;
/**
* Build endpoint using URL and default settings
*
* @param string $url
* @return Endpoint
* @throws \Exception
*/
public static function build($url)
{
return new Endpoint(new Client($url));
}
/**
* Constructor
*
* @param ClientInterface $client Optional; will attempt to auto-build dependency if not passed
* @param string $granularity Optional; the OAI date format for fetching records, use constants from
* Granularity class
* @throws \Exception
*/
public function __construct(ClientInterface $client = null, $granularity = self::AUTO)
{
$this->client = $client ?: new Client();
$this->granularity = $granularity;
}
/**
* Identify the OAI-PMH Endpoint
*
* @return \SimpleXMLElement A XML document with attributes describing the repository
*/
public function identify()
{
$resp = $this->client->request('Identify');
return $resp;
}
/**
* List Metadata Formats
*
* Return the list of supported metadata format for a particular record (if $identifier
* is provided), or the entire repository (if no arguments are provided)
*
* @param string $identifier If specified, will return only those metadata formats that a
* particular record supports
* @return RecordIteratorInterface
*/
public function listMetadataFormats($identifier = null)
{
$params = ($identifier) ? array('identifier' => $identifier) : array();
return new RecordIterator($this->client, 'ListMetadataFormats', $params);
}
/**
* List Record Sets
*
* @return RecordIteratorInterface
*/
public function listSets()
{
return new RecordIterator($this->client, 'ListSets');
}
/**
* Get a single record
*
* @param string $id Record Identifier
* @param string $metadataPrefix Required by OAI-PMH endpoint
* @return \SimpleXMLElement An XML document corresponding to the record
*/
public function getRecord($id, $metadataPrefix)
{
$params = array(
'identifier' => $id,
'metadataPrefix' => $metadataPrefix
);
return $this->client->request('GetRecord', $params);
}
/**
* List Record identifiers
*
* Corresponds to OAI Verb to list record identifiers
*
* @param string $metadataPrefix Required by OAI-PMH endpoint
* @param \DateTimeInterface $from An optional 'from' date for selective harvesting
* @param \DateTimeInterface $until An optional 'until' date for selective harvesting
* @param string $set An optional setSpec for selective harvesting
* @param string $resumptionToken An optional resumptionToken for selective harvesting
* @return RecordIteratorInterface
*/
public function listIdentifiers($metadataPrefix, $from = null, $until = null, $set = null, $resumptionToken = null)
{
return $this->createRecordIterator("ListIdentifiers", $metadataPrefix, $from, $until, $set, $resumptionToken);
}
/**
* List Records
*
* Corresponds to OAI Verb to list records
*
* @param string $metadataPrefix Required by OAI-PMH endpoint
* @param \DateTimeInterface $from An optional 'from' date for selective harvesting
* @param \DateTimeInterface $until An optional 'from' date for selective harvesting
* @param string $set An optional setSpec for selective harvesting
* @param string $resumptionToken An optional resumptionToken for selective harvesting
* @return RecordIteratorInterface
*/
public function listRecords($metadataPrefix, $from = null, $until = null, $set = null, $resumptionToken = null)
{
return $this->createRecordIterator("ListRecords", $metadataPrefix, $from, $until, $set, $resumptionToken);
}
/**
* Create a record iterator
*
* @param string $verb OAI Verb
* @param string $metadataPrefix Required by OAI-PMH endpoint
* @param \DateTimeInterface $from An optional 'from' date for selective harvesting
* @param \DateTimeInterface $until An optional 'from' date for selective harvesting
* @param string $set An optional setSpec for selective harvesting
* @param string $resumptionToken An optional resumptionToken for selective harvesting
*
* @return RecordIteratorInterface
*/
private function createRecordIterator($verb, $metadataPrefix, $from, $until, $set = null, $resumptionToken = null)
{
$params = array('metadataPrefix' => $metadataPrefix);
if ($from instanceof \DateTimeInterface) {
$params['from'] = Granularity::formatDate($from, $this->getGranularity());
} elseif (null !== $from) {
throw new \InvalidArgumentException(sprintf(
'%s::%s $from parameter must be an instance of \DateTimeInterface',
get_called_class(),
'createRecordIterator'
));
}
if ($until instanceof \DateTimeInterface) {
$params['until'] = Granularity::formatDate($until, $this->getGranularity());
} elseif (null !== $until) {
throw new \InvalidArgumentException(sprintf(
'%s::%s $until parameter must be an instance of \DateTimeInterface',
get_called_class(),
'createRecordIterator'
));
}
if ($set) {
$params['set'] = $set;
}
return new RecordIterator($this->client, $verb, $params, $resumptionToken);
}
/**
* Lazy load granularity from Identify, if not specified
*
* @return string
*/
private function getGranularity()
{
// If the granularity is not specified, attempt to retrieve it from the server
// Fall back on DATE granularity
if ($this->granularity === null) {
$response = $this->identify();
return (isset($response->Identify->granularity))
? (string) $response->Identify->granularity
: Granularity::DATE;
}
return $this->granularity;
}
}