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 3
3
namespace Scriptotek \Marc ;
4
4
5
5
use File_MARC_Record ;
6
+ use Scriptotek \Marc \Exceptions \RecordNotFound ;
6
7
use Scriptotek \Marc \Exceptions \UnknownRecordType ;
7
8
use Scriptotek \Marc \Importers \Importer ;
8
9
@@ -99,6 +100,21 @@ public function toArray()
99
100
return iterator_to_array ($ this );
100
101
}
101
102
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
+
102
118
/**
103
119
* Creates a Record object from a File_MARC_Record object.
104
120
*
Original file line number Diff line number Diff line change @@ -130,13 +130,7 @@ public function getFields($spec = null, $pcre = null)
130
130
*/
131
131
public static function fromFile ($ filename )
132
132
{
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 ();
140
134
}
141
135
142
136
/**
@@ -151,13 +145,7 @@ public static function fromFile($filename)
151
145
*/
152
146
public static function fromString ($ data )
153
147
{
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 ();
161
149
}
162
150
163
151
/*************************************************************************
You can’t perform that action at this time.
0 commit comments