Skip to content

Commit 6b9ba18

Browse files
committed
Extract method Collection::first()
1 parent 002be38 commit 6b9ba18

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/Collection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Scriptotek\Marc;
44

55
use File_MARC_Record;
6+
use Scriptotek\Marc\Exceptions\RecordNotFound;
67
use Scriptotek\Marc\Exceptions\UnknownRecordType;
78
use Scriptotek\Marc\Importers\Importer;
89

@@ -99,6 +100,21 @@ public function toArray()
99100
return iterator_to_array($this);
100101
}
101102

103+
/**
104+
* Return the first record in the collection.
105+
*
106+
* @return BibliographicRecord|HoldingsRecord|AuthorityRecord
107+
* @throws RecordNotFound if the collection is empty
108+
*/
109+
public function first()
110+
{
111+
$this->rewind();
112+
if (is_null($this->current())) {
113+
throw new RecordNotFound();
114+
}
115+
return $this->current();
116+
}
117+
102118
/**
103119
* Creates a Record object from a File_MARC_Record object.
104120
*

src/Record.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,7 @@ public function getFields($spec = null, $pcre = null)
130130
*/
131131
public static function fromFile($filename)
132132
{
133-
$records = Collection::fromFile($filename)->toArray();
134-
135-
if (!count($records)) {
136-
throw new RecordNotFound();
137-
}
138-
139-
return $records[0];
133+
return Collection::fromFile($filename)->first();
140134
}
141135

142136
/**
@@ -151,13 +145,7 @@ public static function fromFile($filename)
151145
*/
152146
public static function fromString($data)
153147
{
154-
$records = Collection::fromString($data)->toArray();
155-
156-
if (!count($records)) {
157-
throw new RecordNotFound();
158-
}
159-
160-
return $records[0];
148+
return Collection::fromString($data)->first();
161149
}
162150

163151
/*************************************************************************

0 commit comments

Comments
 (0)