Skip to content

Commit 1f1ce3d

Browse files
committed
refactor: Return Field objects
Return `Field` wrapper objects rather than raw File_MARC objects so we get access to the helper methods.
1 parent 2f15927 commit 1f1ce3d

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed

src/Fields/Field.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Scriptotek\Marc\Record;
66

7-
abstract class Field implements \JsonSerializable
7+
class Field implements \JsonSerializable
88
{
99
/**
1010
* @var array List of properties to be included when serializing the record using the `toArray()` method.
@@ -107,7 +107,7 @@ public static function makeFieldObject(Record $record, $tag, $pcre=false)
107107

108108
// Note: `new static()` is a way of creating a new instance of the
109109
// called class using late static binding.
110-
return $field ? new static($field) : null;
110+
return $field ? new static($field->getField()) : null;
111111
}
112112

113113
public static function makeFieldObjects(Record $record, $tag, $pcre=false)
@@ -116,7 +116,7 @@ public static function makeFieldObjects(Record $record, $tag, $pcre=false)
116116

117117
// Note: `new static()` is a way of creating a new instance of the
118118
// called class using late static binding.
119-
return new static($field);
119+
return new static($field->getField());
120120
}, $record->getFields($tag, $pcre));
121121
}
122122
}

src/QueryResult.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,34 @@
55
use ArrayAccess;
66
use ArrayIterator;
77
use Countable;
8+
use File_MARC_Field;
89
use File_MARC_Reference;
910
use IteratorAggregate;
1011
use Traversable;
12+
use Scriptotek\Marc\Fields\Field;
1113

1214
class QueryResult implements IteratorAggregate, ArrayAccess, Countable
1315
{
1416
protected $ref;
17+
protected $data;
18+
protected $content;
1519

1620
/**
1721
* QueryResult constructor.
18-
* @param File_MARC_Reference $spec
22+
*
23+
* @param File_MARC_Reference $ref
1924
*/
2025
public function __construct(File_MARC_Reference $ref)
2126
{
22-
$this->ref = $ref;
27+
$this->ref = $ref->ref;
28+
$this->data = $ref->data;
29+
$this->content = $ref->content;
30+
31+
for ($i=0; $i < count($this->data); $i++) {
32+
if (is_a($this->data[$i], File_MARC_Field::class)) {
33+
$this->data[$i] = new Field($this->data[$i]);
34+
}
35+
}
2336
}
2437

2538
public function getReference()
@@ -34,7 +47,7 @@ public function getReference()
3447
*/
3548
public function first()
3649
{
37-
return isset($this->ref->data[0]) ? $this->ref->data[0] : null;
50+
return isset($this->data[0]) ? $this->data[0] : null;
3851
}
3952

4053
/**
@@ -44,7 +57,7 @@ public function first()
4457
*/
4558
public function text()
4659
{
47-
return isset($this->ref->content[0]) ? $this->ref->content[0] : null;
60+
return isset($this->content[0]) ? $this->content[0] : null;
4861
}
4962

5063
/**
@@ -54,7 +67,7 @@ public function text()
5467
*/
5568
public function getIterator()
5669
{
57-
return new ArrayIterator($this->ref->data);
70+
return new ArrayIterator($this->data);
5871
}
5972

6073
/**
@@ -65,7 +78,7 @@ public function getIterator()
6578
*/
6679
public function offsetExists($offset)
6780
{
68-
return isset($this->ref->data[$offset]);
81+
return isset($this->data[$offset]);
6982
}
7083

7184
/**
@@ -76,7 +89,7 @@ public function offsetExists($offset)
7689
*/
7790
public function offsetGet($offset)
7891
{
79-
return $this->ref->data[$offset];
92+
return $this->data[$offset];
8093
}
8194

8295
/**
@@ -87,7 +100,7 @@ public function offsetGet($offset)
87100
*/
88101
public function offsetSet($offset, $value)
89102
{
90-
$this->ref->data[$offset] = $value;
103+
$this->data[$offset] = $value;
91104
}
92105

93106
/**
@@ -97,7 +110,7 @@ public function offsetSet($offset, $value)
97110
*/
98111
public function offsetUnset($offset)
99112
{
100-
unset($this->ref->data[$offset]);
113+
unset($this->data[$offset]);
101114
}
102115

103116
/**
@@ -107,6 +120,6 @@ public function offsetUnset($offset)
107120
*/
108121
public function count()
109122
{
110-
return count($this->ref->data);
123+
return count($this->data);
111124
}
112125
}

src/Record.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Scriptotek\Marc;
44

5+
use File_MARC_Field;
56
use File_MARC_Record;
67
use File_MARC_Reference;
78
use Scriptotek\Marc\Exceptions\RecordNotFound;
89
use Scriptotek\Marc\Exceptions\UnknownRecordType;
910
use Scriptotek\Marc\Fields\ControlField;
11+
use Scriptotek\Marc\Fields\Field;
1012

1113
/**
1214
* The MARC record wrapper.
@@ -38,6 +40,21 @@ public function __construct(File_MARC_Record $record)
3840
$this->record = $record;
3941
}
4042

43+
public function getField($spec = null, $pcre = null)
44+
{
45+
$q = $this->record->getField($spec, $pcre);
46+
if ($q) {
47+
return new Field($q);
48+
}
49+
}
50+
51+
public function getFields($spec = null, $pcre = null)
52+
{
53+
return array_values(array_map(function(File_MARC_Field $field) {
54+
return new Field($field);
55+
}, $this->record->getFields($spec, $pcre)));
56+
}
57+
4158
/*************************************************************************
4259
* Data loading
4360
*************************************************************************/

tests/QueryResultTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use PHPUnit\Framework\TestCase;
4+
use Scriptotek\Marc\Fields\Field;
45
use Scriptotek\Marc\Record;
56
use Scriptotek\Marc\QueryResult;
67

@@ -35,7 +36,13 @@ public function testInitialization()
3536
public function testFirstField()
3637
{
3738
$result = $this->record->query('020{$a}')->first();
38-
$this->assertInstanceOf('File_MARC_Field', $result);
39+
$this->assertInstanceOf(Field::class, $result);
40+
}
41+
42+
public function testControlField()
43+
{
44+
$result = $this->record->query('001')->first();
45+
$this->assertInstanceOf(Field::class, $result);
3946
}
4047

4148
public function testFirstSubfield()

tests/RecordTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use PHPUnit\Framework\TestCase;
44
use Scriptotek\Marc\AuthorityRecord;
55
use Scriptotek\Marc\BibliographicRecord;
6+
use Scriptotek\Marc\Fields\Field;
67
use Scriptotek\Marc\Fields\Subject;
78
use Scriptotek\Marc\HoldingsRecord;
89
use Scriptotek\Marc\Marc21;
@@ -70,6 +71,13 @@ public function testBinaryMarc()
7071
$this->assertInstanceOf(Record::class, $record);
7172
}
7273

74+
public function testThatFieldObjectsAreReturned()
75+
{
76+
$record = Record::fromFile(__DIR__ . '/data/binary-marc.mrc');
77+
$this->assertInstanceOf(Field::class, $record->getField('020'));
78+
$this->assertInstanceOf(Field::class, $record->getFields('020')[0]);
79+
}
80+
7381
public function testRecordTypeBiblio()
7482
{
7583
$source = '<?xml version="1.0" encoding="UTF-8" ?>

0 commit comments

Comments
 (0)