| 1 | package MARC::Schema; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Cpanel::JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use File::Share ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use File::Slurper 'read_binary'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||
| 14 | 4 | ▶ | my ($class, $arg_ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | my $self = $arg_ref // {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg_ref // {}
MC/DC100%1/1
$arg_ref // {}
$arg_ref
Truth table100%2/2
A
A= $arg_ref
| |||||||||||||||||||||||||||||
| 16 | 4 | bless $self, $class; | |||||||||||||||||||||||||||
| 17 | 4 | $self->_initialize(); | |||||||||||||||||||||||||||
| 18 | 4 | return $self; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub _initialize { | ||||||||||||||||||||||||||||
| 22 | 4 | ▶ | my ($self) = shift; | ||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | if (!$self->{fields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 3 | $self->{fields} = $self->_load_schema(); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 4 | return; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub _load_schema { | ||||||||||||||||||||||||||||
| 30 | 3 | ▶ | my ($self) = shift; | ||||||||||||||||||||||||||
|
Subroutine
_load_schema: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 31 | 3 | my $json; | |||||||||||||||||||||||||||
| 32 | 3 | ▶ | if ($self->{file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | $json = read_binary($self->{file}); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||
| 36 | 3 | $self->{file} = dist_file('MARC-Schema', 'marc-schema.json'); | |||||||||||||||||||||||||||
| 37 | 3 | $json = read_binary($self->{file}); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 3 | my $schema = decode_json($json); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | return $schema->{fields}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub check { | ||||||||||||||||||||||||||||
| 45 | 11 | ▶ | my ($self, $record, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 11 | ▶ | $record = $record->{record} if reftype $record eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 11 | $options{counter} = {}; | |||||||||||||||||||||||||||
| 50 | 11 | return map {$self->check_field($_, %options)} @$record; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub _error { | ||||||||||||||||||||||||||||
| 54 | 3 | ▶ | my $field = shift; | ||||||||||||||||||||||||||
|
Subroutine
_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 3 | return {tag => $field->[0], @_}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub check_field { | ||||||||||||||||||||||||||||
| 59 | 190 | ▶ | my ($self, $field, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
check_field: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 190 | my $spec = $self->{fields}->{$field->[0]}; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 190 | ▶ | if (!$spec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | if (!$options{ignore_unknown_fields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | return _error($field, message => 'unknown field'); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||
| 68 | 0 | return (); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 189 | ▶ | if ($options{counter} && !$spec->{repeatable}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options{"counter"} and not $spec->{"repeatable"}
MC/DC50%1/2
$options{"counter"} and not $spec->{"repeatable"}
$options{"counter"}
not $spec->{"repeatable"}
Truth table66%2/3
A and not(B)
A= $options{"counter"}B= not $spec->{"repeatable"}
| |||||||||||||||||||||||||||||
| 73 | 110 | ▶ | if ($options{counter}{$field->[0]}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | return _error( | |||||||||||||||||||||||||||
| 75 | $field, | ||||||||||||||||||||||||||||
| 76 | repeatable => 'false', | ||||||||||||||||||||||||||||
| 77 | message => 'field is not repeatable' | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 188 | my %errors; | |||||||||||||||||||||||||||
| 83 | 188 | ▶ | if ($spec->{subfields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 136 | my %sfcounter; | |||||||||||||||||||||||||||
| 85 | 136 | my (undef, undef, undef, @subfields) = @$field; | |||||||||||||||||||||||||||
| 86 | 136 | ▶ | while (@subfields) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 221 | my ($code, undef) = splice @subfields, 0, 2; | |||||||||||||||||||||||||||
| 88 | 221 | my $sfspec = $spec->{subfields}->{$code}; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 221 | ▶ | if ($sfspec) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 91 | 220 | ▶ | if (!$sfspec->{repeatable} && $sfcounter{$code}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $sfspec->{"repeatable"} and $sfcounter{$code}++
MC/DC100%2/2
not $sfspec->{"repeatable"} and $sfcounter{$code}++
not $sfspec->{"repeatable"}
$sfcounter{$code}++
Truth table100%3/3
not(A) and B
A= not $sfspec->{"repeatable"}B= $sfcounter{$code}++
| |||||||||||||||||||||||||||||
| 92 | $errors{$code} = { | ||||||||||||||||||||||||||||
| 93 | message => 'subfield is not repeatable', | ||||||||||||||||||||||||||||
| 94 | label => $sfspec->{label}, | ||||||||||||||||||||||||||||
| 95 | 1 | repeatable => 'false' | |||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | elsif (!$options{ignore_unknown_subfields}) { | ||||||||||||||||||||||||||||
| 100 | 1 | $errors{$code} = {message => 'unknown subfield'}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 188 | ▶ | return %errors ? _error($field, subfields => \%errors) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 3 | 1; | |||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||