| 1 | package PICA::Schema; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use v5.14.1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | our $VERSION = '1.18'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | our @EXPORT_OK = qw(field_identifier check_value clean_pica); | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use PICA::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 13 | ▶ | my ($class, $schema) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 13 | bless $schema, $class; | |||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | sub check { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 23 | ▶ | my ($self, $record, %options) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 23 | my @errors; | |||||||||||||||||||||||||||||||||||||||||
| 22 | $record | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 23 | ▶ | = clean_pica($record, error => sub {push @errors, shift}, %options) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | or return @errors; | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 22 | $options{counter} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 22 | my %field_identifiers; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 22 | for my $field (@$record) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 3062 | my $id = $self->field_identifier($field); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 3062 | $field_identifiers{$id} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 3062 | my $error = $self->check_field($field, %options, _field_id => $id); | |||||||||||||||||||||||||||||||||||||||||
| 33 | 3062 | ▶ | if ($error and !grep {$_ eq $error} @errors) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$error and not grep({$_ eq $error;} @errors)
MC/DC0%0/2
$error and not grep({$_ eq $error;} @errors)
$error
not grep({$_ eq $error;} @errors)
Truth table33%1/3
A and not(B)
A= $errorB= not grep({$_ eq $error;} @errors)
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 88 | push @errors, $error; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | # check whether required fields exist | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 22 | for my $id (keys %{$self->{fields}}) { | |||||||||||||||||||||||||||||||||||||||||
| 40 | 23 | my $field = $self->{fields}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 23 | ▶ | if ($field->{required} && !$field_identifiers{$id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$field->{"required"} and not $field_identifiers{$id}
MC/DC100%2/2
$field->{"required"} and not $field_identifiers{$id}
$field->{"required"}
not $field_identifiers{$id}
Truth table100%3/3
A and not(B)
A= $field->{"required"}B= not $field_identifiers{$id}
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | push @errors, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | PICA::Error->new( | ||||||||||||||||||||||||||||||||||||||||||
| 44 | [substr($id, 0, 4), length $id > 4 ? substr($id, 5) : undef], | ||||||||||||||||||||||||||||||||||||||||||
| 45 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 22 | return @errors; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | sub check_field { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 3062 | ▶ | my ($self, $field, %opts) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_field: called CC=32 SCAR=36.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 3062 | ▶ | my $id = $opts{_field_id} || $self->field_identifier($field); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$opts{'_field_id'} || $self->field_identifier($field)
MC/DC0%0/2
$opts{'_field_id'} || $self->field_identifier($field)
$opts{'_field_id'}
$self->field_identifier($field)
Truth table33%1/3
A || B
A= $opts{'_field_id'}B= $self->field_identifier($field)
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 3062 | my $spec = $self->{fields}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 3062 | ▶ | if ($opts{allow_deprecated}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $opts{allow_deprecated_fields} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $opts{allow_deprecated_subfields} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 3062 | ▶ | if ($opts{ignore_unknown}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $opts{ignore_unknown_fields} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | $opts{ignore_unknown_subfields} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 3062 | ▶ | if (!$spec) { # field is not defined | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 3040 | $spec = $self->{'deprecated-fields'}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 3040 | my $simple = [split('/', $id)]; | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 3040 | ▶ | if ($spec) { # field is deprectaed | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | unless ($opts{allow_deprecated_fields}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return PICA::Error->new($simple, deprecated => 1); | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | elsif ($opts{ignore_unknown_fields}) { | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 3038 | return PICA::Error->new($simple); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 22 | ▶ | if ($opts{counter} && !$spec->{repeatable}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts{"counter"} and not $spec->{"repeatable"}
MC/DC50%1/2
$opts{"counter"} and not $spec->{"repeatable"}
$opts{"counter"}
not $spec->{"repeatable"}
Truth table66%2/3
A and not(B)
A= $opts{"counter"}B= not $spec->{"repeatable"}
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 20 | my $tag_occ = join '/', grep {defined} @$field[0, 1]; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 20 | ▶ | if ($opts{counter}{$tag_occ}++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | return PICA::Error->new($field, repeated => 1); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 20 | my $failed = check_annotation($field, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 20 | ▶ | return PICA::Error->new($failed) if $failed; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 20 | ▶ | if ($opts{ignore_subfields}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 20 | my %errors; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 20 | ▶ | if ($spec->{subfields}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 13 | my $order; | |||||||||||||||||||||||||||||||||||||||||
| 103 | my %sfcounter; | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 13 | my (undef, undef, @subfields) = @$field; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 13 | ▶ | pop @subfields if @subfields % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 13 | ▶ | while (@subfields) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 17 | my ($code, $value) = splice @subfields, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 17 | my $sfspec = $spec->{subfields}{$code}; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 17 | ▶ | if (!$sfspec) { # subfield is not defined | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | $sfspec = $spec->{'deprecated-subfields'}{$code}; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | if ($sfspec) { # subfield is deprecated | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | unless ($opts{allow_deprecated_subfields}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $errors{$code} = {deprecated => 1}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | elsif (!$opts{ignore_unknown_subfields}) { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | $errors{$code} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 17 | ▶ | if ($sfspec) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 15 | ▶ | if (!$sfspec->{repeatable} && $sfcounter{$code}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
not $sfspec->{"repeatable"} and $sfcounter{$code}
MC/DC50%1/2
not $sfspec->{"repeatable"} and $sfcounter{$code}
not $sfspec->{"repeatable"}
$sfcounter{$code}
Truth table66%2/3
not(A) and B
A= not $sfspec->{"repeatable"}B= $sfcounter{$code}
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | $errors{$code} = {repeated => 1}; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | ▶ | elsif (!$opts{ignore_subfield_order} | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
not $opts{"ignore_subfield_order"} and defined $sfspec->{"order"}
MC/DC100%2/2
not $opts{"ignore_subfield_order"} and defined $sfspec->{"order"}
not $opts{"ignore_subfield_order"}
defined $sfspec->{"order"}
Truth table100%3/3
not(A) and B
A= not $opts{"ignore_subfield_order"}B= defined $sfspec->{"order"}
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | && defined $sfspec->{order}) | ||||||||||||||||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 8 | ▶ | if (defined $order && $order > $sfspec->{order}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $order and $order > $sfspec->{"order"}
MC/DC100%2/2
defined $order and $order > $sfspec->{"order"}
defined $order
$order > $sfspec->{"order"}
Truth table100%3/3
A and B
A= defined $orderB= $order > $sfspec->{"order"}
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | $errors{$code} = {order => $sfspec->{order}}; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 7 | $order = 1 * $sfspec->{order}; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 15 | $sfcounter{$code}++; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 15 | $errors{$code} = $_ for check_value($value, $sfspec, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 13 | foreach my $code (keys %{$spec->{subfields}}) { | |||||||||||||||||||||||||||||||||||||||||
| 145 | 25 | ▶ | if (!$sfcounter{$code} && $spec->{subfields}{$code}{required}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $sfcounter{$code} and $spec->{"subfields"}{$code}{"required"}
MC/DC100%2/2
not $sfcounter{$code} and $spec->{"subfields"}{$code}{"required"}
not $sfcounter{$code}
$spec->{"subfields"}{$code}{"required"}
Truth table100%3/3
not(A) and B
A= not $sfcounter{$code}B= $spec->{"subfields"}{$code}{"required"}
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | $errors{$code} = {required => 1}; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 20 | ▶ | return %errors ? PICA::Error->new($field, subfields => \%errors) : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | sub check_annotation { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 3101 | ▶ | my ($field, %options) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_annotation: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 3101 | my (undef, undef, @subfields) = @$field; | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 3101 | ▶ | if (@subfields % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | return "Field annotation not allowed" | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | ▶ | if defined $options{annotated} && !$options{annotation}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $options{"annotated"} and not $options{"annotation"}
MC/DC50%1/2
defined $options{"annotated"} and not $options{"annotation"}
defined $options{"annotated"}
not $options{"annotation"}
Truth table66%2/3
A and not(B)
A= defined $options{"annotated"}B= not $options{"annotation"}
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 5 | ▶ | return "Annotation must not be non-alphanumeric character" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | if pop(@subfields) !~ /^[^A-Za-z0-9]\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | elsif ($options{annotated}) { | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | return "Missing field annotation"; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | sub check_value { | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 15 | ▶ | my ($value, $schedule, %opts) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_value: called CC=13 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | # TODO: check compatible with ECMA 262 (2015) regular expression grammar | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 15 | ▶ | if ($schedule->{pattern} and $value !~ /$schedule->{pattern}/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$schedule->{"pattern"} and not $value =~ /$schedule->{"pattern"}/u
MC/DC50%1/2
$schedule->{"pattern"} and not $value =~ /$schedule->{"pattern"}/u
$schedule->{"pattern"}
not $value =~ /$schedule->{"pattern"}/u
Truth table66%2/3
A and not(B)
A= $schedule->{"pattern"}B= not $value =~ /$schedule->{"pattern"}/u
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | return {value => $value, pattern => $schedule->{pattern},}; | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | # check positions and codes | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 14 | ▶ | my $positions = $schedule->{positions} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$schedule->{"positions"} // {}
MC/DC100%1/1
$schedule->{"positions"} // {}
$schedule->{"positions"}
Truth table100%2/2
A
A= $schedule->{"positions"}
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 14 | foreach my $pos (keys %$positions) { | |||||||||||||||||||||||||||||||||||||||||
| 182 | 5 | my @p = split '-', $pos; | |||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 5 | ▶ | if (length $value < int $p[-1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | return {value => $value, position => $pos,}; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 4 | my $def = $positions->{$pos}; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 4 | ▶ | if ($def->{codes}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 3 | my $codes = $def->{codes}; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 3 | ▶ | my $deprecated = $def->{'deprecated-codes'} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$def->{"deprecated-codes"} // {}
MC/DC0%0/1
$def->{"deprecated-codes"} // {}
$def->{"deprecated-codes"}
Truth table50%1/2
A
A= $def->{"deprecated-codes"}
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 3 | ▶ | my $c = substr $value, $p[0] - 1, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | (@p > 1 ? $p[1] - $p[0] : 0) + 1; | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 3 | ▶ | if (!defined $codes->{$c}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | ▶ | if (!$deprecated->{$c}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 1 | return {value => $value, position => $pos}; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | ▶ | elsif (!$opts{allow_deprecated} | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts{'allow_deprecated'} || $opts{'allow_deprecated_codes'}
MC/DC0%0/2
$opts{'allow_deprecated'} || $opts{'allow_deprecated_codes'}
$opts{'allow_deprecated'}
$opts{'allow_deprecated_codes'}
Truth table0%0/3
A || B
A= $opts{'allow_deprecated'}B= $opts{'allow_deprecated_codes'}
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | && !$opts{allow_deprecated_codes}) | ||||||||||||||||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # TODO: there is no way to see that an invalid value is deprecated | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | return {value => $value, position => $pos}; | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 12 | return; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | sub field_identifier { | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 3073 | ▶ | my $fields = ref $_[0] eq 'PICA::Schema' ? shift->{fields} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
field_identifier: called CC=13 SCAR=25.7
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 3073 | my ($tag, $occ) = @{$_[0]}; | |||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 3073 | ▶ | $occ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
substr($tag, 0, 1) ne 2 and defined $occ and $occ ne ""
substr($tag, 0, 1) ne 2 and defined $occ
MC/DC100%3/3
substr($tag, 0, 1) ne 2 and defined $occ and $occ ne ""
substr($tag, 0, 1) ne 2
defined $occ
$occ ne ""
Truth table100%4/4
A and B and C
A= substr($tag, 0, 1) ne 2B= defined $occC= $occ ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | = (substr($tag, 0, 1) ne '2' && defined $occ && $occ ne '') | ||||||||||||||||||||||||||||||||||||||||||
| 217 | ? sprintf("%02d", $occ) | ||||||||||||||||||||||||||||||||||||||||||
| 218 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 3073 | ▶ | if ($fields && $occ ne '' && !exists $fields->{"$tag/$occ"}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$fields and $occ ne "" and not exists $$fields{"$tag/$occ"}
$fields and $occ ne ""
MC/DC100%3/3
$fields and $occ ne "" and not exists $$fields{"$tag/$occ"}
$fields
$occ ne ""
not exists $$fields{"$tag/$occ"}
Truth table100%4/4
A and B and not(C)
A= $fieldsB= $occ ne ""C= not exists $$fields{"$tag/$occ"}
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 20 | for my $id (keys %$fields) { | |||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | ▶ | return $id | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$id =~ m[^$tag/(..)-(..)$]u and $occ >= $1 and $occ <= $2
$id =~ m[^$tag/(..)-(..)$]u and $occ >= $1
MC/DC33%1/3
$id =~ m[^$tag/(..)-(..)$]u and $occ >= $1 and $occ <= $2
$id =~ m[^$tag/(..)-(..)$]u
$occ >= $1
$occ <= $2
Truth table50%2/4
A and B and C
A= $id =~ m[^$tag/(..)-(..)$]uB= $occ >= $1C= $occ <= $2
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | if $id =~ /^$tag\/(..)-(..)$/ && $occ >= $1 && $occ <= $2; | ||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 3072 | ▶ | return $occ ne '' ? "$tag/$occ" : $tag; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | sub TO_JSON { | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | return {map {$_ => $self->{$_}} keys %$self}; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | sub abbreviated { | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abbreviated: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | my $abbr = dclone($self->TO_JSON); | |||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | for (values %{$abbr->{fields} // {}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$abbr->{"fields"} // {}
MC/DC0%0/1
$abbr->{"fields"} // {}
$abbr->{"fields"}
Truth table0%0/2
A
A= $abbr->{"fields"}
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | delete $_->{tag}; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | delete $_->{occurrence}; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | for (values %{$_->{subfields} // {}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$_->{"subfields"} // {}
MC/DC0%0/1
$_->{"subfields"} // {}
$_->{"subfields"}
Truth table0%0/2
A
A= $_->{"subfields"}
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | delete $_->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | return $abbr; | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | sub clean_pica { | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 50 | ▶ | my ($record, %options) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clean_pica: called CC=22 SCAR=31.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 50 | my $ok = 1; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 50 | my $handler = $options{error}; | |||||||||||||||||||||||||||||||||||||||||
| 253 | my $error = exists $options{error} | ||||||||||||||||||||||||||||||||||||||||||
| 254 | ? sub { | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 17 | ▶ | if ($handler) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 17 | ▶ | $handler->(PICA::Error->new($_[1] || [], message => $_[0])); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[1] || []
MC/DC100%1/1
$_[1] || []
$_[1]
Truth table100%2/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 17 | $ok = 0; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 50 | ▶ | : sub {say STDERR shift; $ok = 0}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 50 | ▶ | $record = $record->{record} if reftype $record eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 50 | ▶ | if (reftype $record ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 5 | $error->('PICA record must be array reference'); | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | ▶ | elsif (!@$record && !$options{ignore_empty_records}) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
@$record || $options{'ignore_empty_records'}
MC/DC100%2/2
@$record || $options{'ignore_empty_records'}
@$record
$options{'ignore_empty_records'}
Truth table100%3/3
A || B
A= @$recordB= $options{'ignore_empty_records'}
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 1 | $error->('PICA record should not be empty'); | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 50 | ▶ | return unless $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 44 | my @filtered; | |||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 44 | for my $field (@$record) { | |||||||||||||||||||||||||||||||||||||||||
| 276 | 3083 | ▶ | if (reftype $field ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 2 | $error->('PICA field must be array reference'); | |||||||||||||||||||||||||||||||||||||||||
| 278 | return | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 3081 | my ($tag, $occ, @sf) = @$field; | |||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 3081 | ▶ | if ($tag !~ /^[012.][0-9.][0-9.][A-Z@.]$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 2 | $error->("Malformed PICA tag: $tag", $field); | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 3081 | ▶ | if ($occ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 2874 | ▶ | if ($occ !~ /^[0-9]{1,3}$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | $error->("Malformed occurrence: $occ", $field); | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | ▶ | elsif (substr($tag, 0, 1) ne '2' && length $occ eq 3) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
substr($tag, 0, 1) ne 2 and length $occ eq 3
MC/DC100%2/2
substr($tag, 0, 1) ne 2 and length $occ eq 3
substr($tag, 0, 1) ne 2
length $occ eq 3
Truth table100%3/3
A and B
A= substr($tag, 0, 1) ne 2B= length $occ eq 3
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 1 | $error->( | |||||||||||||||||||||||||||||||||||||||||
| 293 | "Three digit occurrences only allowed on PICA level 2", | ||||||||||||||||||||||||||||||||||||||||||
| 294 | $field | ||||||||||||||||||||||||||||||||||||||||||
| 295 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 3081 | my $msg = check_annotation($field, %options); | |||||||||||||||||||||||||||||||||||||||||
| 300 | 3081 | ▶ | $error->($msg, $field) if $msg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 3081 | ▶ | next if $options{ignore_subfields}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 3081 | ▶ | pop @sf if @sf % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 3081 | ▶ | while (@sf) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 6764 | my ($code, $value) = splice @sf, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 6764 | ▶ | $error->("Malformed PICA subfield: $code", $field) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | if $code !~ /^[_A-Za-z0-9]$/; | ||||||||||||||||||||||||||||||||||||||||||
| 310 | $error->("PICA subfield \$$code must be non-empty string", $field) | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 6764 | ▶ | if $value !~ /^./ && !$options{allow_empty_subfields}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$value =~ /^./u or $options{"allow_empty_subfields"}
MC/DC50%1/2
$value =~ /^./u or $options{"allow_empty_subfields"}
$value =~ /^./u
$options{"allow_empty_subfields"}
Truth table66%2/3
A or B
A= $value =~ /^./uB= $options{"allow_empty_subfields"}
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | 42 | ▶ | return $record if $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 319 | __END__ | ||||||||||||||||||||||||||||||||||||||||||