Bug 22454: Unit tests
[koha.git] / Koha / Biblio / Metadata.pm
blob9da2ec2b490b98a062dacf4535b960d323578dec
1 package Koha::Biblio::Metadata;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use Modern::Perl;
20 use MARC::Record;
21 use MARC::File::XML;
23 use Koha::Database;
24 use Koha::Exceptions::Metadata;
26 use base qw(Koha::Object);
28 =head1 NAME
30 Koha::Metadata - Koha Metadata Object class
32 =head1 API
34 =head2 Class methods
36 =cut
38 =head3 record
40 my $record = $metadata->record;
42 Returns an object representing the metadata record. The expected record type
43 corresponds to this table:
45 -------------------------------
46 | format | object type |
47 -------------------------------
48 | marcxml | MARC::Record |
49 -------------------------------
51 =head4 Error handling
53 =over
55 =item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
57 =item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
59 =back
61 =cut
63 sub record {
65 my ($self) = @_;
67 my $record;
69 if ( $self->format eq 'marcxml' ) {
70 $record = eval { MARC::Record::new_from_xml( $self->metadata, 'utf-8', $self->schema ); };
71 unless ($record) {
72 Koha::Exceptions::Metadata::Invalid->throw(
73 id => $self->id,
74 format => $self->format,
75 schema => $self->schema
79 else {
80 Koha::Exceptions::Metadata->throw(
81 'Koha::Biblio::Metadata->record called on unhandled format: ' . $self->format );
84 return $record;
87 =head2 Internal methods
89 =head3 _type
91 =cut
93 sub _type {
94 return 'BiblioMetadata';