| 1 | package MARC::Schema; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Cpanel::JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use File::Share ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use File::Slurper 'read_binary'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 6 | my ($class, $arg_ref) = @_; | |||||||||||||||||||||||||||
| 15 | 6 | ▶ | 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 | 6 | bless $self, $class; | |||||||||||||||||||||||||||
| 17 | 6 | $self->_initialize(); | |||||||||||||||||||||||||||
| 18 | 6 | return $self; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 6 | ▶ | sub _initialize { | ||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | 6 | my ($self) = shift; | |||||||||||||||||||||||||||
| 23 | 6 | ▶ | if (!$self->{fields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 5 | $self->{fields} = $self->_load_schema(); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 6 | return; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | sub _load_schema { | ||||||||||||||||||||||||||
|
Subroutine
_load_schema: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 5 | my ($self) = shift; | |||||||||||||||||||||||||||
| 31 | 5 | my $json; | |||||||||||||||||||||||||||
| 32 | 5 | ▶ | if ($self->{file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | $json = read_binary($self->{file}); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||
| 36 | 4 | $self->{file} = dist_file('MARC-Schema', 'marc-schema.json'); | |||||||||||||||||||||||||||
| 37 | 4 | $json = read_binary($self->{file}); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 5 | my $schema = decode_json($json); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 5 | return $schema->{fields}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 31 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 31 | my ($self, $record, %options) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 31 | ▶ | $record = $record->{record} if reftype $record eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 31 | $options{counter} = {}; | |||||||||||||||||||||||||||
| 50 | 31 | return map {$self->check_field($_, %options)} @$record; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 558 | ▶ | sub check_field { | ||||||||||||||||||||||||||
|
Subroutine
check_field: called CC=18 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 558 | my ($self, $field, %options) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 558 | my $tag = $field->[0]; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 558 | my $spec = $self->{fields}->{$tag}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 558 | ▶ | if (!$spec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | if (!$options{ignore_unknown_fields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | return ({tag => $tag, error => 'unknown field', type => 'field'}); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||
| 65 | 0 | return (); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 557 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 70 | 324 | ▶ | if ($options{counter}{$field->[0]}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | return ( | ||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 1 | tag => $tag, | |||||||||||||||||||||||||||
| 74 | error => 'field is not repeatable', | ||||||||||||||||||||||||||||
| 75 | type => 'field', | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 556 | my @errors; | |||||||||||||||||||||||||||
| 82 | 556 | ▶ | if ($spec->{subfields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 404 | my %sfcounter; | |||||||||||||||||||||||||||
| 84 | 404 | my (undef, undef, undef, @subfields) = @$field; | |||||||||||||||||||||||||||
| 85 | 404 | ▶ | while (@subfields) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 649 | my ($code, undef) = splice @subfields, 0, 2; | |||||||||||||||||||||||||||
| 87 | 649 | my $sfspec = $spec->{subfields}->{$code}; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 649 | ▶ | if ($sfspec) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 90 | 648 | ▶ | 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}++
| |||||||||||||||||||||||||||||
| 91 | 1 | push @errors, | |||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | tag => $tag, | ||||||||||||||||||||||||||||
| 94 | error => 'subfield is not repeatable', | ||||||||||||||||||||||||||||
| 95 | type => 'subfield', | ||||||||||||||||||||||||||||
| 96 | value => $code, | ||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | elsif (!$options{ignore_unknown_subfields}) { | ||||||||||||||||||||||||||||
| 101 | 1 | push @errors, | |||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | tag => $tag, | ||||||||||||||||||||||||||||
| 104 | error => "unknown subfield", | ||||||||||||||||||||||||||||
| 105 | type => 'subfield', | ||||||||||||||||||||||||||||
| 106 | value => $code, | ||||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 556 | ▶ | if ($spec->{indicator1}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 221 | my (undef, $code, @other) = @$field; | |||||||||||||||||||||||||||
| 114 | 221 | ▶ | $code //= ' '; | ||||||||||||||||||||||||||
|
Condition100%2/2
$code //= " "
MC/DC100%1/1
$code //= " "
$code
Truth table100%2/2
A
A= $code
| |||||||||||||||||||||||||||||
| 115 | my (@matches) | ||||||||||||||||||||||||||||
| 116 | 221 | = grep {$code =~ /^[$_]/} keys %{$spec->{indicator1}->{codes}}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 221 | ▶ | if (@matches > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # everything is ok | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 4 | push @errors, | |||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | tag => $tag, | ||||||||||||||||||||||||||||
| 126 | error => 'unknown first indicator', | ||||||||||||||||||||||||||||
| 127 | type => 'indicator', | ||||||||||||||||||||||||||||
| 128 | value => $code, | ||||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 556 | ▶ | if ($spec->{indicator2}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 158 | my (undef, undef, $code, @other) = @$field; | |||||||||||||||||||||||||||
| 135 | 158 | ▶ | $code //= ' '; | ||||||||||||||||||||||||||
|
Condition50%1/2
$code //= " "
MC/DC0%0/1
$code //= " "
$code
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||||||||||
| 136 | my (@matches) | ||||||||||||||||||||||||||||
| 137 | 158 | = grep {$code =~ /^[$_]/} keys %{$spec->{indicator2}->{codes}}; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 158 | ▶ | if (@matches > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # everything is ok | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||
| 144 | 1 | push @errors, | |||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||
| 146 | tag => $tag, | ||||||||||||||||||||||||||||
| 147 | error => 'unknown second indicator', | ||||||||||||||||||||||||||||
| 148 | type => 'indicator', | ||||||||||||||||||||||||||||
| 149 | value => $code, | ||||||||||||||||||||||||||||
| 150 | }; | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 556 | return @errors; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 5 | 1; | |||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||