Skip to content

Commit

Permalink
Add __toString method
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Jul 2, 2017
1 parent ce4e122 commit 3550b3f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
20 changes: 19 additions & 1 deletion tests/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,22 @@ public function testMake() {
$this->assertInstanceOf('Scriptotek\Sru\Record', $record);
$this->assertEquals(29, $record->position);
}
}

public function testToString() {
$record = Record::make(29, 'Hello world');

$this->assertEquals('Hello world', (string) $record);
}

public function testXmlToString() {
$record = Record::make(29, '<hello>world</hello>');

$this->assertEquals('<hello>world</hello>', (string) $record);
}

public function testNamespacedXmlToString() {
$record = Record::make(29, '<c:test xmlns:c="http://www.loc.gov/zing/cql/xcql/">Test</c:test>');

$this->assertEquals('<c:test xmlns:c="http://www.loc.gov/zing/cql/xcql/">Test</c:test>', (string) $record);
}
}

0 comments on commit 3550b3f

Please sign in to comment.