Skip to content

Commit 1fbbd4c

Browse files
committed
Add Record factory method
1 parent 7182c55 commit 1fbbd4c

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/Record.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,40 @@ class Record
1919
/** @var QuiteSimpleXMLElement */
2020
public $data;
2121

22+
static $recordTpl = '<srw:record xmlns:srw="http://www.loc.gov/zing/srw/">
23+
<srw:recordSchema>{{recordSchema}}</srw:recordSchema>
24+
<srw:recordPacking>{{recordPacking}}</srw:recordPacking>
25+
<srw:recordPosition>{{position}}</srw:recordPosition>
26+
<srw:recordData>{{data}}</srw:recordData>
27+
</srw:record>';
28+
2229
/**
2330
* Create a new record
24-
*
25-
* @param Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement $doc
31+
* @param QuiteSimpleXMLElement $doc
2632
*/
27-
public function __construct($doc)
33+
public function __construct(QuiteSimpleXMLElement $doc)
2834
{
2935
$this->position = intval($doc->text('./srw:recordPosition'));
3036
$this->packing = $doc->text('./srw:recordPacking');
3137
$this->schema = $doc->text('./srw:recordSchema');
3238
$this->data = $doc->first('./srw:recordData');
3339
}
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(new QuiteSimpleXMLElement($record));
57+
}
3458
}

tests/RecordTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php namespace Scriptotek\Sru;
2+
3+
class RecordTest extends TestCase
4+
{
5+
public function testMake() {
6+
$record = Record::make(29, 'Hello world');
7+
8+
$this->assertInstanceOf(Record::class, $record);
9+
$this->assertEquals(29, $record->position);
10+
}
11+
}

0 commit comments

Comments
 (0)