1
|
|
|
<?php namespace Scriptotek\Sru; |
2
|
|
|
|
3
|
|
|
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Single record from a SRU response |
7
|
|
|
*/ |
8
|
|
|
class Record |
9
|
|
|
{ |
10
|
|
|
/** @var int */ |
11
|
|
|
public $position; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
public $packing; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
public $schema; |
18
|
|
|
|
19
|
|
|
/** @var QuiteSimpleXMLElement */ |
20
|
|
|
public $data; |
21
|
|
|
|
22
|
|
|
static public $recordTpl = '<s:record xmlns:s="http://www.loc.gov/zing/srw/"> |
23
|
|
|
<s:recordSchema>{{recordSchema}}</s:recordSchema> |
24
|
|
|
<s:recordPacking>{{recordPacking}}</s:recordPacking> |
25
|
|
|
<s:recordPosition>{{position}}</s:recordPosition> |
26
|
|
|
<s:recordData>{{data}}</s:recordData> |
27
|
|
|
</s:record>'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create a new record |
31
|
|
|
* @param QuiteSimpleXMLElement $doc |
32
|
|
|
*/ |
33
|
|
|
public function __construct(QuiteSimpleXMLElement $doc) |
34
|
|
|
{ |
35
|
|
|
$this->position = intval($doc->text('./srw:recordPosition')); |
36
|
|
|
$this->packing = $doc->text('./srw:recordPacking'); |
37
|
|
|
$this->schema = $doc->text('./srw:recordSchema'); |
38
|
|
|
$this->data = $doc->first('./srw:recordData'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param int $position |
43
|
|
|
* @param string|QuiteSimpleXMLElement $data |
44
|
|
|
* @param string $recordSchema |
45
|
|
|
* @param string $recordPacking |
46
|
|
|
* @return Record |
47
|
|
|
*/ |
48
|
|
|
public static function make($position, $data, $recordSchema='marcxchange', $recordPacking='xml') |
49
|
|
|
{ |
50
|
|
|
$record = str_replace( |
51
|
|
|
array('{{position}}', '{{data}}', '{{recordSchema}}', '{{recordPacking}}'), |
52
|
|
|
array($position, $data, $recordSchema, $recordPacking), |
53
|
|
|
self::$recordTpl |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
return new Record(QuiteSimpleXMLElement::make($record, Response::$nsPrefixes)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the record data as a string. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function __toString() |
65
|
|
|
{ |
66
|
|
|
$nodes = $this->data->xpath('./child::*'); |
67
|
|
|
if (count($nodes) == 1) { |
68
|
|
|
return $nodes[0]->asXML(); |
69
|
|
|
} elseif (count($nodes) > 1) { |
70
|
|
|
throw new \RuntimeException('recordData contains more than one node!'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->data->text(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|