PICA/Schema.pm

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