| 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.06'; | |||||||||||||||||||||||||||
| 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 | sub new { | ||||||||||||||||||||||||||||
| 14 | 6 | ▶ | my ($class, $arg_ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub _initialize { | ||||||||||||||||||||||||||||
| 22 | 6 | ▶ | my ($self) = shift; | ||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | if (!$self->{fields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 5 | $self->{fields} = $self->_load_schema(); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 6 | return; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub _load_schema { | ||||||||||||||||||||||||||||
| 30 | 5 | ▶ | my ($self) = shift; | ||||||||||||||||||||||||||
|
Subroutine
_load_schema: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 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 | sub check { | ||||||||||||||||||||||||||||
| 45 | 31 | ▶ | my ($self, $record, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub _error { | ||||||||||||||||||||||||||||
| 54 | 8 | ▶ | my $field = shift; | ||||||||||||||||||||||||||
|
Subroutine
_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 8 | return {tag => $field->[0], @_}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub check_field { | ||||||||||||||||||||||||||||
| 59 | 558 | ▶ | my ($self, $field, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
check_field: called CC=19 SCAR=29.5
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 558 | my $spec = $self->{fields}->{$field->[0]}; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 558 | ▶ | 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 | 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"}
| |||||||||||||||||||||||||||||
| 73 | 324 | ▶ | 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 | 556 | my %errors; | |||||||||||||||||||||||||||
| 83 | 556 | ▶ | if ($spec->{subfields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 404 | my %sfcounter; | |||||||||||||||||||||||||||
| 85 | 404 | my (undef, undef, undef, @subfields) = @$field; | |||||||||||||||||||||||||||
| 86 | 404 | ▶ | while (@subfields) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 649 | my ($code, undef) = splice @subfields, 0, 2; | |||||||||||||||||||||||||||
| 88 | 649 | my $sfspec = $spec->{subfields}->{$code}; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 649 | ▶ | if ($sfspec) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 91 | 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}++
| |||||||||||||||||||||||||||||
| 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 | 556 | ▶ | if ($spec->{indicator1}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 221 | my (undef, $code, @other) = @$field; | |||||||||||||||||||||||||||
| 107 | 221 | ▶ | $code //= ' '; | ||||||||||||||||||||||||||
|
Condition100%2/2
$code //= " "
MC/DC100%1/1
$code //= " "
$code
Truth table100%2/2
A
A= $code
| |||||||||||||||||||||||||||||
| 108 | my (@matches) | ||||||||||||||||||||||||||||
| 109 | 221 | = grep {$code =~ /^[$_]/} keys %{$spec->{indicator1}->{codes}}; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 221 | ▶ | if (@matches > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # everything is ok | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 4 | $errors{ind1} = {message => "unknown indicator1 value `$code'"}; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 556 | ▶ | if ($spec->{indicator2}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 158 | my (undef, undef, $code, @other) = @$field; | |||||||||||||||||||||||||||
| 122 | 158 | ▶ | $code //= ' '; | ||||||||||||||||||||||||||
|
Condition50%1/2
$code //= " "
MC/DC0%0/1
$code //= " "
$code
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||||||||||
| 123 | my (@matches) | ||||||||||||||||||||||||||||
| 124 | 158 | = grep {$code =~ /^[$_]/} keys %{$spec->{indicator2}->{codes}}; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 158 | ▶ | if (@matches > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # everything is ok | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||
| 131 | 1 | $errors{ind2} = {message => "unknown indicator2 value `$code'"}; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 556 | ▶ | return %errors ? _error($field, subfields => \%errors) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | 1; | |||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||