File tree Expand file tree Collapse file tree 2 files changed +18
-14
lines changed
Expand file tree Collapse file tree 2 files changed +18
-14
lines changed Original file line number Diff line number Diff line change 33namespace Scriptotek \Marc ;
44
55use File_MARC_Record ;
6+ use Scriptotek \Marc \Exceptions \RecordNotFound ;
67use Scriptotek \Marc \Exceptions \UnknownRecordType ;
78use 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 *
Original file line number Diff line number Diff 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 /*************************************************************************
You can’t perform that action at this time.
0 commit comments