blib/lib/PICA/Schema.pm

filter: Statementstmt 86.3% 203 / 235 Branchbran 86.5% 142 / 164 Conditioncond 73.9% 51 / 69 MC/DCmcdc 63.6% 28 / 44 Subroutinesub 92.3% 24 / 26 Podpod 56.2% 9 / 16 totaltotal 82.4% 457 / 554 CC 120 SCAR 26.8
CC
120
Cov
85%
abbreviated
30.0
clean_pica
22.1
field_identifier
21.1
CRAP 14.6
« blib/lib/PICA/Writer/XML.pm ↑ Summary blib/lib/PICA/Path.pm »
1 package PICA::Schema;
2 16 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 16 our $VERSION = '2.09';
5  
6 16 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use List::Util qw(pairs);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use PICA::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use PICA::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 16 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 our @EXPORT_OK
14 16 = qw(field_identifier check_value clean_pica parse_subfield_schedule);
15  
16 sub new {
17 16 ▶ my ($class, $schema) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 16 cleanup_field_schedule($schema->{fields});
19 16 bless $schema, $class;
20 }
21  
22 sub check {
23 25 ▶ my ($self, $record, %options) = @_;
Subroutine check: called CC=2 SCAR=6.9
Pod documented
24  
25 25 my @errors;
26 $record
27 25 ▶ = clean_pica($record, error => sub {push @errors, shift}, %options)
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$record = clean_pica($record, "error", sub { push @errors, shift(); } , %options) or return @errors 1 24 25
28 or return @errors;
29  
30 24 my @errors = map {$self->check_record($_->{record}, %options)}
31 PICA::Data::pica_split($record);
32  
33 24 my %seen;
34 24 return grep {not $seen{"$_"}++} @errors;
35 }
36  
37 sub check_record {
38 436 ▶ my ($self, $record, %options) = @_;
Subroutine check_record: called CC=12 SCAR=25.9
Pod undocumented
39  
40 436 my @errors;
41 436 $options{counter} = {};
42  
43 436 my %field_identifiers;
44 436 for my $field (@$record) {
45 3065 my $id = $self->field_identifier($field);
46 3065 $field_identifiers{$id} = 1;
47 3065 my $error = $self->check_field($field, %options, _field_id => $id);
48 3065 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 3047 18 3065
49 3047 ▶ push @errors, $error unless grep {$_ eq $error} @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $error;} @errors 2980 67 3047
50 }
51 3065 ▶ if ($options{annotate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"annotate"}) 0 3065 3065
52 0 my $annotation = ' ';
53 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
54 0 ▶ $annotation = $error->{message} =~ /^unknown/ ? '?' : '!';
Branch: 1 branch
BranchTrueFalseTotal
$error->{'message'} =~ /^unknown/u ? : 0 0 0
55 }
56 0 PICA::Data::pica_annotation($field, $annotation);
57 }
58 }
59  
60 # check whether required fields exist for this record level
61 436 my $level = substr $record->[0][0], 0, 1;
62 436 for my $id (keys %{$self->{fields}}) {
63 442 ▶ next if $level ne substr $id, 0, 1;
Branch: 1 branch
BranchTrueFalseTotal
if $level ne substr($id, 0, 1) 63 379 442
64 379 my $field = $self->{fields}{$id};
65 379 ▶ if ($field->{required} && !$field_identifiers{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"required"} and not $field_identifiers{$id}) 2 377 379
Condition100%3/3
$field->{"required"} and not $field_identifiers{$id}
!ll&&!rl&&r
367102
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}
ABresult
0X0
100
111
66 2 ▶ push @errors,
Branch: 1 branch
BranchTrueFalseTotal
length $id > 4 ? : 1 1 2
67 PICA::Error->new(
68 [substr($id, 0, 4), length $id > 4 ? substr($id, 5) : undef],
69 required => 1
70 );
71 }
72 }
73  
74 436 return @errors;
75 }
76  
77 sub check_field {
78 3065 ▶ my ($self, $field, %opts) = @_;
Subroutine check_field: called CC=13 SCAR=28.0
Pod documented
79  
80 3065 ▶ my $id = $opts{_field_id} || $self->field_identifier($field);
Condition33%1/3
$opts{'_field_id'} || $self->field_identifier($field)
l!l&&r!l&&!r
306500
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)
ABresult
1X1
011
000
81 3065 my $spec = $self->{fields}{$id};
82  
83 3065 ▶ if ($opts{allow_deprecated}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"allow_deprecated"}) 0 3065 3065
84 0 $opts{allow_deprecated_fields} = 1;
85 0 $opts{allow_deprecated_subfields} = 1;
86 }
87  
88 3065 ▶ if ($opts{ignore_unknown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"ignore_unknown"}) 1 3064 3065
89 1 $opts{ignore_unknown_fields} = 1;
90 1 $opts{ignore_unknown_subfields} = 1;
91 }
92  
93 3065 ▶ if (!$spec) { # field is not defined
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec) 2687 378 3065
94 2687 $spec = $self->{'deprecated-fields'}{$id};
95 2687 my $simple = [split('/', $id)];
96  
97 2687 ▶ if ($spec) { # field is deprectaed
Branch: 2 branches
BranchTrueFalseTotal
if ($spec) { } 0 2687 2687
elsif ($opts{'ignore_unknown_fields'}) { } 2 2685 2687
98 0 ▶ unless ($opts{allow_deprecated_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"allow_deprecated_fields"}) 0 0 0
99 0 return PICA::Error->new($simple, deprecated => 1);
100 }
101 }
102 elsif ($opts{ignore_unknown_fields}) {
103 2 return ();
104 }
105 else {
106 2685 return PICA::Error->new($simple);
107 }
108 }
109  
110 378 ▶ if ($opts{counter} && !$spec->{repeatable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"counter"} and not $spec->{"repeatable"}) 376 2 378
Condition66%2/3
$opts{"counter"} and not $spec->{"repeatable"}
!ll&&!rl&&r
02376
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"}
ABresult
0X0
100
111
111 376 ▶ if ($opts{counter}{$id}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"counter"}{$id}++) 2 374 376
112 2 return PICA::Error->new($field, repeated => 1);
113 }
114 }
115  
116 376 my $failed = check_annotation($field, %opts);
117 376 ▶ return PICA::Error->new($failed) if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 0 376 376
118  
119 376 ▶ if ($opts{ignore_subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"ignore_subfields"}) 0 376 376
120 0 return ();
121 }
122  
123 376 return check_subfields($field, $spec, %opts);
124 }
125  
126 sub check_subfields {
127 376 ▶ my ($field, $spec, %opts) = @_;
Subroutine check_subfields: called CC=20 SCAR=30.2
Pod undocumented
128  
129 376 my %errors;
130 376 ▶ if ($spec->{subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"subfields"}) 366 10 376
131 366 my $order;
132 my %sfcounter;
133 366 my (undef, undef, @subfields) = @$field;
134 366 ▶ pop @subfields if @subfields % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @subfields % 2 0 366 366
135  
136 366 ▶ while (@subfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subfields) 723 366 1089
137 723 my ($code, $value) = splice @subfields, 0, 2;
138 723 my $sfspec = $spec->{subfields}{$code};
139  
140 723 ▶ if (!$sfspec) { # subfield is not defined
Branch: 1 branch
BranchTrueFalseTotal
unless ($sfspec) 708 15 723
141 708 $sfspec = $spec->{'deprecated-subfields'}{$code};
142 708 ▶ if ($sfspec) { # subfield is deprecated
Branch: 2 branches
BranchTrueFalseTotal
if ($sfspec) { } 0 708 708
elsif (not $opts{'ignore_unknown_subfields'}) { } 707 1 708
143 0 ▶ unless ($opts{allow_deprecated_subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"allow_deprecated_subfields"}) 0 0 0
144 0 $errors{$code} = {deprecated => 1};
145 }
146 }
147 elsif (!$opts{ignore_unknown_subfields}) {
148 707 $errors{$code} = {};
149 }
150 }
151  
152 723 ▶ if ($sfspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sfspec) 15 708 723
153 15 ▶ if (!$sfspec->{repeatable} && $sfcounter{$code}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $sfspec->{'repeatable'} and $sfcounter{$code}) { } 1 14 15
elsif (not $opts{'ignore_subfield_order'} and defined $sfspec->{'order'}) { } 8 8 16
Condition66%2/3
not $sfspec->{"repeatable"} and $sfcounter{$code}
!ll&&!rl&&r
0141
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}
ABresult
0X0
100
111
154 1 $errors{$code} = {repeated => 1};
155 }
156 ▶ elsif (!$opts{ignore_subfield_order}
Condition100%3/3
not $opts{"ignore_subfield_order"} and defined $sfspec->{"order"}
!ll&&!rl&&r
248
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"}
ABresult
0X0
100
111
157 && defined $sfspec->{order})
158 {
159 8 ▶ if (defined $order && $order > $sfspec->{order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $order and $order > $sfspec->{'order'}) { } 1 7 8
Condition100%3/3
defined $order and $order > $sfspec->{"order"}
!ll&&!rl&&r
611
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"}
ABresult
0X0
100
111
160 1 $errors{$code} = {order => $sfspec->{order}};
161 }
162 else {
163 7 $order = 1 * $sfspec->{order};
164 }
165 }
166 15 $sfcounter{$code}++;
167  
168 15 $errors{$code} = $_ for check_value($value, $sfspec, %opts);
169 }
170  
171 }
172  
173 366 foreach my $code (keys %{$spec->{subfields}}) {
174 378 ▶ if (!$sfcounter{$code} && $spec->{subfields}{$code}{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sfcounter{$code} and $spec->{"subfields"}{$code}{"required"}) 354 24 378
Condition100%3/3
not $sfcounter{$code} and $spec->{"subfields"}{$code}{"required"}
!ll&&!rl&&r
1410354
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"}
ABresult
0X0
100
111
175 354 $errors{$code} = {required => 1};
176 }
177 }
178 }
179 376 ▶ return %errors ? PICA::Error->new($field, subfields => \%errors) : ();
Branch: 1 branch
BranchTrueFalseTotal
%errors ? : 360 16 376
180 }
181  
182 sub check_annotation {
183 3508 ▶ my ($field, %options) = @_;
Subroutine check_annotation: called CC=6 SCAR=17.9
Pod documented
184  
185 3508 my (undef, undef, @subfields) = @$field;
186  
187 3508 ▶ if (@subfields % 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@subfields % 2) { } 13 3495 3508
elsif ($options{'check_annotation'}) { } 1 3494 3495
188 return "Field annotation not allowed"
189 if defined $options{check_annotation}
190 13 ▶ && !$options{check_annotation};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"check_annotation"} and not $options{"check_annotation"} 1 12 13
Condition66%2/3
defined $options{"check_annotation"} and not $options{"check_annotation"}
!ll&&!rl&&r
1201
MC/DC50%1/2
defined $options{"check_annotation"} and not $options{"check_annotation"}
defined $options{"check_annotation"} not $options{"check_annotation"}
Truth table66%2/3
A and not(B)
A= defined $options{"check_annotation"}B= not $options{"check_annotation"}
ABresult
0X0
100
111
191  
192 12 ▶ return "Annotation must not be non-alphanumeric character"
Branch: 1 branch
BranchTrueFalseTotal
if not pop(@subfields) =~ /^[^A-Za-z0-9]\z/u 1 11 12
193 if pop(@subfields) !~ /^[^A-Za-z0-9]\z/;
194 }
195 elsif ($options{check_annotation}) {
196 1 return "Missing field annotation";
197 }
198 }
199  
200 sub check_pattern {
201 23 ▶ my ($value, $pattern) = @_;
Subroutine check_pattern: called CC=3 SCAR=11.0
Pod documented
202  
203 # TODO: check compatible with ECMA 262 (2015) regular expression grammar
204 23 ▶ if ($pattern and $value !~ /$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern and not $value =~ /$pattern/u) 1 22 23
Condition66%2/3
$pattern and not $value =~ /$pattern/u
!ll&&!rl&&r
2201
MC/DC50%1/2
$pattern and not $value =~ /$pattern/u
$pattern not $value =~ /$pattern/u
Truth table66%2/3
A and not(B)
A= $patternB= not $value =~ /$pattern/u
ABresult
0X0
100
111
205 1 return {value => $value, pattern => $pattern};
206 }
207 }
208  
209 sub check_positions {
210 14 ▶ my ($value, $positions, %opts) = @_;
Subroutine check_positions: called CC=7 SCAR=19.5
Pod undocumented
211  
212 14 ▶ return unless $positions;
Branch: 1 branch
BranchTrueFalseTotal
unless $positions 11 3 14
213  
214 3 for my $pos (keys %$positions) {
215 9 my $def = $positions->{$pos};
216  
217 9 my @p = split '-', $pos;
218  
219 9 ▶ if (length $value < int $p[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value < int $p[-1]) 1 8 9
220 1 return {value => $value, position => $pos,};
221 }
222  
223 8 ▶ my $val = substr $value, $p[0] - 1, (@p > 1 ? $p[1] - $p[0] : 0) + 1;
Branch: 1 branch
BranchTrueFalseTotal
@p > 1 ? : 5 3 8
224  
225 8 ▶ if (my $error = check_pattern($val, $def->{pattern})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = check_pattern($val, $def->{"pattern"})) 0 8 8
226 0 return {%$error, position => $pos};
227 }
228  
229 8 ▶ if (check_code($val, $def, %opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_code($val, $def, %opts)) 1 7 8
230 1 return {value => $val, position => $pos};
231 }
232 }
233 }
234  
235 sub check_code {
236 8 ▶ my ($code, $def, %opts) = @_;
Subroutine check_code: called CC=6 SCAR=21.6
Pod undocumented
237  
238 # code list only given as URI
239 8 ▶ return unless ref $def->{codes};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $def->{"codes"} 3 5 8
240  
241 # code is defined
242 5 ▶ return if exists $def->{codes}{$code};
Branch: 1 branch
BranchTrueFalseTotal
if exists $def->{"codes"}{$code} 4 1 5
243  
244 # code is deprecated and we allow deprecated codes
245 1 ▶ if ($opts{allow_deprecated} || $opts{allow_deprecated_codes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"allow_deprecated"} or $opts{"allow_deprecated_codes"}) 0 1 1
Condition33%1/3
$opts{"allow_deprecated"} or $opts{"allow_deprecated_codes"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$opts{"allow_deprecated"} or $opts{"allow_deprecated_codes"}
$opts{"allow_deprecated"} $opts{"allow_deprecated_codes"}
Truth table33%1/3
A or B
A= $opts{"allow_deprecated"}B= $opts{"allow_deprecated_codes"}
ABresult
1X1
011
000
246 0 my $deprecated = $def->{'deprecated-codes'};
247 0 ▶ return ref $deprecated && !(exists $deprecated->{$code});
Condition0%0/3
ref $deprecated && !exists $deprecated->{$code}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $deprecated && !exists $deprecated->{$code}
ref $deprecated !exists $deprecated->{$code}
Truth table0%0/3
A && not(B)
A= ref $deprecatedB= !exists $deprecated->{$code}
ABresult
0X0
100
111
248 }
249  
250 1 return 1;
251 }
252  
253 sub check_value {
254 15 ▶ my ($value, $schedule, %opts) = @_;
Subroutine check_value: called CC=3 SCAR=11.0
Pod documented
255  
256 15 ▶ if (my $error = check_pattern($value, $schedule->{pattern})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = check_pattern($value, $schedule->{"pattern"})) 1 14 15
257 1 return $error;
258 }
259  
260 14 ▶ if (my $error = check_positions($value, $schedule->{positions}, %opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = check_positions($value, $schedule->{"positions"}, %opts)) 2 12 14
261 2 return $error;
262 }
263  
264 12 return;
265 }
266  
267 sub cleanup_field_schedule {
268 16 ▶ my ($fields) = @_;
Subroutine cleanup_field_schedule: called CC=3 SCAR=11.8
Pod undocumented
269  
270 # allow ..../00 as field identifier
271 16 for (grep {$_ =~ qr{/00$}} keys %$fields) {
272 1 my $tag = substr $_, 0, 4;
273 1 ▶ if ($fields->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$tag}) { } 0 1 1
274 0 warn "duplicated field $_ is removed from schedule\n";
275 0 delete $fields->{$_};
276 }
277 else {
278 1 $fields->{$tag} = delete $fields->{$_};
279 }
280 }
281  
282 # TODO: detect overlap of ranges (not required by Avram specification)
283 }
284  
285 sub field_identifier {
286  
287 3107 ▶ $_[0]->{_ranges} = 1 if reftype $_[0] eq 'HASH';
Subroutine field_identifier: called CC=21 SCAR=30.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($_[0]) eq "HASH" 3079 28 3107
288  
289 3107 ▶ my $fields = reftype $_[0] eq 'HASH' ? shift->{fields} : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 3079 28 3107
290  
291 3107 my ($tag, $occ, @sf) = @{$_[0]};
292 3107 my $level = substr $tag, 0, 1;
293  
294 3107 ▶ $occ = ($level ne '2' && $occ > 0) ? sprintf("%02d", $occ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$level ne '2' && $occ > 0 ? : 25 3082 3107
Condition100%3/3
$level ne 2 and $occ > 0
!ll&&!rl&&r
285622625
MC/DC100%2/2
$level ne 2 and $occ > 0
$level ne 2 $occ > 0
Truth table100%3/3
A and B
A= $level ne 2B= $occ > 0
ABresult
0X0
100
111
295  
296 3107 ▶ if ($fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields) 3079 28 3107
297 3079 ▶ return "$tag/$occ" if exists $fields->{"$tag/$occ"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$fields{"$tag/$occ"} 1 3078 3079
298  
299 # Find matching occurrence range
300 # (TODO: create an index to speed up this slow lookup?)
301  
302 return $_
303 3078 ▶ for grep {$_ =~ /^$tag\/(..)-(..)$/ && $occ >= $1 && $occ <= $2}
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ m[^$tag/(..)-(..)$]u and $occ >= $1 and $occ <= $2 5 3091 3096
Condition66%2/3
$_ =~ m[^$tag/(..)-(..)$]u and $occ >= $1
!ll&&!rl&&r
309105
MC/DC50%1/2
$_ =~ m[^$tag/(..)-(..)$]u and $occ >= $1
$_ =~ m[^$tag/(..)-(..)$]u $occ >= $1
Truth table66%2/3
A and B
A= $_ =~ m[^$tag/(..)-(..)$]uB= $occ >= $1
ABresult
0X0
100
111
304 sort keys %$fields;
305  
306 3074 ▶ if ($level eq '2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($level eq 2) 2855 219 3074
307  
308 # get value of first subfield $x
309 2855 my $x;
310 2855 for my $i (0 .. $#sf) {
311 12664 ▶ if ($i % 2 && $sf[$i - 1] eq 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2 and $sf[$i - 1] eq "x") 790 11874 12664
Condition100%3/3
$i % 2 and $sf[$i - 1] eq "x"
!ll&&!rl&&r
63325542790
MC/DC100%2/2
$i % 2 and $sf[$i - 1] eq "x"
$i % 2 $sf[$i - 1] eq "x"
Truth table100%3/3
A and B
A= $i % 2B= $sf[$i - 1] eq "x"
ABresult
0X0
100
111
312 790 $x = $sf[$i];
313 790 last;
314 }
315 }
316  
317 # find field definition with matching field counter
318 2855 ▶ if ($x =~ /^[0-9][0-9]?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ /^[0-9][0-9]?$/u) 790 2065 2855
319 790 ▶ return "${tag}x$x" if exists $fields->{"${tag}x$x"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$fields{"${tag}x$x"} 1 789 790
320  
321 789 return $_ for grep {
322 795 ▶ $_ =~ /^${tag}x(..?)-(..?)$/
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^${tag}x(..?)-(..?)$/u and $x >= $1 and $x <= $2 and length $1 == length $x 4 791 795
Condition66%4/6
$_ =~ /^${tag}x(..?)-(..?)$/u and $x >= $1 and $x <= $2
!ll&&!rl&&r
79104
$_ =~ /^${tag}x(..?)-(..?)$/u and $x >= $1
!ll&&!rl&&r
79104
MC/DC0%0/3
$_ =~ /^${tag}x(..?)-(..?)$/u and $x >= $1 and $x <= $2
$_ =~ /^${tag}x(..?)-(..?)$/u $x >= $1 $x <= $2
Truth table0%0/4
A and B and C
A= $_ =~ /^${tag}x(..?)-(..?)$/uB= $x >= $1C= $x <= $2
ABCresult
0XX0
10X0
1100
1111
323 && $x >= $1
324 && $x <= $2
325 && length $1 == length $x
326 } sort keys %$fields;
327 }
328 }
329 }
330  
331 3099 ▶ return $occ ne '' ? "$tag/$occ" : $tag;
Branch: 1 branch
BranchTrueFalseTotal
$occ ne '' ? : 21 3078 3099
332 }
333  
334 sub TO_JSON {
335 3 ▶ my ($self) = @_;
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
336 3 return {map {$_ => $self->{$_}} keys %$self};
337 }
338  
339 sub abbreviated {
340 0 ▶ my ($self) = @_;
Subroutine abbreviated: not called CC=5 SCAR=34.0
Pod documented
341 0 my $abbr = dclone($self->TO_JSON);
342 0 delete $abbr->{total};
343 0 delete $_->{records};
344 0 ▶ for my $field (values %{$abbr->{fields} // {}}) {
Condition0%0/2
$abbr->{"fields"} // {}
l!l
00
MC/DC0%0/1
$abbr->{"fields"} // {}
$abbr->{"fields"}
Truth table0%0/2
A
A= $abbr->{"fields"}
Aresult
11
00
345 0 delete $field->{tag};
346 0 delete $field->{occurrence};
347 0 delete $field->{total};
348 0 ▶ for my $sf (values %{$field->{subfields} // {}}) {
Condition0%0/2
$field->{"subfields"} // {}
l!l
00
MC/DC0%0/1
$field->{"subfields"} // {}
$field->{"subfields"}
Truth table0%0/2
A
A= $field->{"subfields"}
Aresult
11
00
349 0 delete $_->{code};
350 0 delete $_->{records};
351 }
352 }
353 0 return $abbr;
354 }
355  
356 sub clean_pica {
357 88 ▶ my ($record, %options) = @_;
Subroutine clean_pica: called CC=22 SCAR=30.9
Pod documented
358  
359 88 my $ok = 1;
360 88 my $handler = $options{error};
361 my $error = exists $options{error}
362 ? sub {
363 17 ▶ if ($handler) {
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 17 0 17
364 17 ▶ $handler->(PICA::Error->new($_[1] || [], message => $_[0]));
Condition100%2/2
$_[1] || []
l!l
98
MC/DC100%1/1
$_[1] || []
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
365 }
366 17 $ok = 0;
367 }
368 88 ▶ : sub {say STDERR shift; $ok = 0};
Subroutine __ANON__: not called CC=1 SCAR=0.6
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'error'} ? : 45 43 88
369  
370 88 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 66 22 88
371  
372 88 ▶ if (reftype $record ne 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($record) ne 'ARRAY') { } 5 83 88
elsif (not @$record || $options{'ignore_empty_records'}) { } 1 82 83
373 5 $error->('PICA record must be array reference');
374 }
375 ▶ elsif (!@$record && !$options{ignore_empty_records}) {
Condition100%3/3
@$record || $options{'ignore_empty_records'}
l!l&&r!l&&!r
7841
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'}
ABresult
1X1
011
000
376 1 $error->('PICA record should not be empty');
377 }
378  
379 88 ▶ return unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 6 82 88
380  
381 82 my @filtered;
382  
383 82 for my $field (@$record) {
384 3134 ▶ if (reftype $field ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($field) ne "ARRAY") 2 3132 3134
385 2 $error->('PICA field must be array reference');
386 2 return;
387 }
388  
389 3132 my ($tag, $occ, @sf) = @$field;
390  
391 3132 ▶ if ($tag !~ /^[012.][0-9.][0-9.][A-Z@.]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag =~ /^[012.][0-9.][0-9.][A-Z\@.]$/u) 2 3130 3132
392 2 $error->("Malformed PICA tag: $tag", $field);
393 }
394  
395 3132 ▶ if ($occ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($occ) 2878 254 3132
396 2878 ▶ if ($occ !~ /^[0-9]{1,3}$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $occ =~ /^[0-9]{1,3}$/u) { } 1 2877 2878
elsif (substr($tag, 0, 1) ne '2' and length $occ eq 3) { } 1 5730 5731
397 1 $error->("Malformed occurrence: $occ", $field);
398 }
399 ▶ 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
!ll&&!rl&&r
2854221
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
ABresult
0X0
100
111
400 1 $error->(
401 "Three digit occurrences only allowed on PICA level 2",
402 $field
403 );
404 }
405 }
406  
407 3132 ▶ if (my $msg = check_annotation($field, %options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = check_annotation($field, %options)) 3 3129 3132
408 3 $error->($msg, $field);
409 }
410  
411 3132 ▶ next if $options{ignore_subfields};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"ignore_subfields"} 0 3132 3132
412  
413 3132 ▶ pop @sf if @sf % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @sf % 2 13 3119 3132
414 3132 ▶ while (@sf) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sf) 6820 3132 9952
415 6820 my ($code, $value) = splice @sf, 0, 2;
416  
417 6820 ▶ $error->("Malformed PICA subfield: $code", $field)
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /^[_A-Za-z0-9]$/u 1 6819 6820
418 if $code !~ /^[_A-Za-z0-9]$/;
419 $error->("PICA subfield \$$code must be non-empty string", $field)
420 6820 ▶ if $value !~ /^./ && !$options{allow_empty_subfields};
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^./u or $options{"allow_empty_subfields"} 1 6819 6820
Condition100%3/3
$value =~ /^./u or $options{"allow_empty_subfields"}
l!l&&r!l&&!r
681721
MC/DC100%2/2
$value =~ /^./u or $options{"allow_empty_subfields"}
$value =~ /^./u $options{"allow_empty_subfields"}
Truth table100%3/3
A or B
A= $value =~ /^./uB= $options{"allow_empty_subfields"}
ABresult
1X1
011
000
421 }
422 }
423  
424 80 ▶ return $record if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 74 6 80
425 }
426  
427 sub parse_subfield_schedule {
428 5 ▶ my %schedule;
Subroutine parse_subfield_schedule: called CC=8 SCAR=20.9
Pod documented
429  
430 5 my $order = 1;
431 5 for (pairs split "([*+?]?)", $_[0]) {
432 15 my ($code, $status) = @$_;
433 15 ▶ die "Malformed PICA subfield: $code" if $code !~ /[a-zA-Z0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /[a-zA-Z0-9]/u 0 15 15
434 die "Invalid subfield schedule string: subfield $code given twice!"
435 15 ▶ if $schedule{$code};
Branch: 1 branch
BranchTrueFalseTotal
if $schedule{$code} 0 15 15
436 15 ▶ $schedule{$code} = {
Branch: 2 branches
BranchTrueFalseTotal
$status eq '+' || $status eq '*' ? : 3 12 15
$status ne '?' && $status ne '*' ? : 13 2 15
Condition100%6/6
$status eq "+" or $status eq "*"
l!l&&r!l&&!r
2112
$status ne "?" and $status ne "*"
!ll&&!rl&&r
1113
MC/DC100%2/2
$status eq "+" or $status eq "*"
$status eq "+" $status eq "*"
MC/DC100%2/2
$status ne "?" and $status ne "*"
$status ne "?" $status ne "*"
Truth table100%3/3
A or B
A= $status eq "+"B= $status eq "*"
ABresult
1X1
011
000
Truth table100%3/3
A and B
A= $status ne "?"B= $status ne "*"
ABresult
0X0
100
111
437 code => $code,
438 repeatable => ($status eq '+' || $status eq '*' ? 1 : 0),
439 required => ($status ne '?' && $status ne '*' ? 1 : 0),
440 order => $order++,
441 };
442 }
443  
444 5 return \%schedule;
445 }
446  
447 16 1;
448 __END__
« blib/lib/PICA/Writer/XML.pm ↑ Summary blib/lib/PICA/Path.pm »