diff --git a/src/Record.php b/src/Record.php index 70d1907..f117e38 100644 --- a/src/Record.php +++ b/src/Record.php @@ -55,4 +55,21 @@ public static function make($position, $data, $recordSchema='marcxchange', $reco return new Record(QuiteSimpleXMLElement::make($record, Response::$nsPrefixes)); } + + /** + * Get the record data as a string. + * + * @return string + */ + public function __toString() + { + $nodes = $this->data->xpath('./child::*'); + if (count($nodes) == 1) { + return $nodes[0]->asXML(); + } elseif (count($nodes) > 1) { + throw new \RuntimeException('recordData contains more than one node!'); + } + + return $this->data->text(); + } } diff --git a/tests/RecordTest.php b/tests/RecordTest.php index e8006e7..ebdd99c 100644 --- a/tests/RecordTest.php +++ b/tests/RecordTest.php @@ -8,4 +8,22 @@ public function testMake() { $this->assertInstanceOf('Scriptotek\Sru\Record', $record); $this->assertEquals(29, $record->position); } -} \ No newline at end of file + + public function testToString() { + $record = Record::make(29, 'Hello world'); + + $this->assertEquals('Hello world', (string) $record); + } + + public function testXmlToString() { + $record = Record::make(29, 'world'); + + $this->assertEquals('world', (string) $record); + } + + public function testNamespacedXmlToString() { + $record = Record::make(29, 'Test'); + + $this->assertEquals('Test', (string) $record); + } +}