Skip to content

Commit 3550b3f

Browse files
committed
Add __toString method
1 parent ce4e122 commit 3550b3f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Record.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,21 @@ public static function make($position, $data, $recordSchema='marcxchange', $reco
5555

5656
return new Record(QuiteSimpleXMLElement::make($record, Response::$nsPrefixes));
5757
}
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+
}
5875
}

tests/RecordTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,22 @@ public function testMake() {
88
$this->assertInstanceOf('Scriptotek\Sru\Record', $record);
99
$this->assertEquals(29, $record->position);
1010
}
11-
}
11+
12+
public function testToString() {
13+
$record = Record::make(29, 'Hello world');
14+
15+
$this->assertEquals('Hello world', (string) $record);
16+
}
17+
18+
public function testXmlToString() {
19+
$record = Record::make(29, '<hello>world</hello>');
20+
21+
$this->assertEquals('<hello>world</hello>', (string) $record);
22+
}
23+
24+
public function testNamespacedXmlToString() {
25+
$record = Record::make(29, '<c:test xmlns:c="http://www.loc.gov/zing/cql/xcql/">Test</c:test>');
26+
27+
$this->assertEquals('<c:test xmlns:c="http://www.loc.gov/zing/cql/xcql/">Test</c:test>', (string) $record);
28+
}
29+
}

0 commit comments

Comments
 (0)