DAIA/Object.pm

filter: Statementstmt 86.2% 219 / 254 Branchbran 70.6% 113 / 160 Conditioncond 63.2% 43 / 68 MC/DCmcdc 50.0% 20 / 40 Subroutinesub 84.6% 22 / 26 Podpod 90.9% 10 / 11 totaltotal 76.3% 427 / 559 CC 114 SCAR 35.6
CC
114
Cov
78%
serialize
182.0
AUTOLOAD
46.2
_enable_utf8_layer
20.0
CRAP 35.2
« DAIA.pm ↑ Summary DAIA/Availability.pm »
1 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package DAIA::Object;
4 #ABSTRACT: Abstract base class of all DAIA classes
5 12 our $VERSION = '0.43'; #VERSION
6  
7 12 ▶use Carp::Clan;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Data::Validate::URI qw(is_uri is_web_uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Scalar::Util qw(refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 our $AUTOLOAD;
14 12 our @HIDDEN_PROPERTIES =
15 qw(to format xmlns cgi header xmlheader xslt pi callback exitif noutf8);
16  
17  
18 211 ▶sub new {
Subroutine new: called CC=13 SCAR=25.6
Pod documented
19 211 my $class = shift;
20 211 my $self = bless { }, $class;
21  
22 211 my %hash;
23 211 ▶ if ( @_ == 1 and ref($_[0]) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 42 169 211
elsif (@_ == 1 and ref $_[0] eq $class) { } 10 159 169
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
858442
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
24 42 %hash = %{$_[0]};
25 ▶ } elsif ( @_ == 1 and ref($_[0]) eq $class ) {
Condition100%3/3
@_ == 1 and ref $_[0] eq $class
!ll&&!rl&&r
857410
MC/DC100%2/2
@_ == 1 and ref $_[0] eq $class
@_ == 1 ref $_[0] eq $class
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq $class
ABresult
0X0
100
111
26 10 %hash = %{$_[0]->struct}; # copy constructor
27 } else {
28 159 %hash = $self->_buildargs(@_);
29 }
30  
31 # abstract class handling
32 211 ▶ if ( $class eq 'DAIA::Availability' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "DAIA::Availability") 12 199 211
33 12 ▶ croak "Availability status missing" unless exists $hash{status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{"status"} 2 10 12
34 10 $self->status( $hash{status} );
35 10 delete $hash{status};
36 10 $class = ref($self);
37 }
38  
39 209 my %hidden;
40 209 foreach ( @HIDDEN_PROPERTIES ) {
41 2299 ▶ next unless exists $hash{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{$_} 2288 11 2299
42 11 $hidden{$_} = $hash{$_};
43 11 delete $hash{$_};
44 }
45 209 ▶ $self->{_hidden} = \%hidden if %hidden;
Branch: 1 branch
BranchTrueFalseTotal
if %hidden 6 203 209
46  
47 12 ▶ no strict 'refs'; ##no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
48 209 my $PROPERTIES = \%{$class."::PROPERTIES"};
49 209 foreach my $property (keys %{$PROPERTIES}) {
50 915 ▶ $self->$property( undef ) unless exists $hash{$property};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{$property} 631 284 915
51 }
52  
53 208 foreach my $property (keys %hash) {
54 305 $self->$property( $hash{$property} );
55 }
56  
57 #use Data::Dumper; print Dumper($self)."\n";
58 206 return $self;
59 }
60  
61  
62 18 ▶sub add {
Subroutine add: called CC=5 SCAR=16.1
Pod documented
63 18 my $self = shift;
64  
65 #print "APPEND: " . ref($self) . "\n";
66  
67 18 foreach my $value (@_) {
68 17 ▶ next unless defined $value; # ignore undefined values
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 16 17
69  
70 #print "- " . ref($value) . "\n";
71  
72 16 ▶ croak "Cannot add $value to " . ref($self)
Branch: 1 branch
BranchTrueFalseTotal
unless ref($value) =~ /^DAIA::([A-Z][a-z]+)$/ 0 16 16
73 unless ref($value) =~ /^DAIA::([A-Z][a-z]+)$/;
74 16 my $property = lc($1);
75
76 #no strict 'refs';
77 #my $PROPERTIES = \%{$class."::PROPERTIES"};
78
79 # repeatable
80 16 ▶ if ( ref($self->{$property}) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$property} eq 'ARRAY') { } 11 5 16
81 11 push @{$self->{$property}}, $value;
82 } else {
83 5 $self->$property( $value );
84 }
85 }
86 }
87  
88  
89 12 ▶sub xml {
Subroutine xml: called CC=12 SCAR=25.6
Pod documented
90 12 my ($self, %param) = @_;
91 12 $self->_hidden_prop( \%param );
92  
93 12 ▶ my $xmlns = $param{xmlns} || ($param{xslt} or $param{header});
Condition83%5/6
$param{'xmlns'} || ($param{'xslt'} || $param{'header'})
l!l&&r!l&&!r
417
$param{"xslt"} or $param{"header"}
l!l&&r!l&&!r
017
MC/DC66%2/3
$param{'xmlns'} || ($param{'xslt'} || $param{'header'})
$param{'xmlns'} $param{"xslt"} $param{"header"}
Truth table75%3/4
A || B or C
A= $param{'xmlns'}B= $param{"xslt"}C= $param{"header"}
ABCresult
1XX1
01X1
0011
0000
94 12 ▶ my $pi = $param{pi} || [ ];
Condition50%1/2
$param{'pi'} || []
l!l
012
MC/DC0%0/1
$param{'pi'} || []
$param{'pi'}
Truth table50%1/2
A
A= $param{'pi'}
Aresult
11
00
95 12 ▶ $pi = [$pi] unless (reftype($pi) || '') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($pi) || "") eq "ARRAY" 0 12 12
Condition50%1/2
builtin::reftype($pi) || ''
l!l
120
MC/DC0%0/1
builtin::reftype($pi) || ''
builtin::reftype($pi)
Truth table50%1/2
A
A= builtin::reftype($pi)
Aresult
11
00
96  
97 push @$pi, 'xml-stylesheet type="text/xsl" href="' . xml_escape_value($param{xslt}) . '"'
98 12 ▶ if $param{xslt};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"xslt"} 0 12 12
99 0 ▶ @$pi = map { $_ =~ /^<\?.*\?>$/ ? "$_\n" : "<?$_?>\n" } @$pi;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^<\?.*\?>$/ ? : 0 0 0
100  
101 12 my $name = lc(ref($self));
102 12 $name =~ s/^daia:://;
103 12 ▶ $name = 'daia' if $name eq 'response';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "response" 1 11 12
104  
105 12 my $struct = $self->struct;
106 12 ▶ $struct->{xmlns} = "http://ws.gbv.de/daia/" if $xmlns;
Branch: 1 branch
BranchTrueFalseTotal
if $xmlns 5 7 12
107 12 my $xml = xml_write( $name, $struct, 0 );
108 12 ▶ delete $struct->{xmlns} if $xmlns;
Branch: 1 branch
BranchTrueFalseTotal
if $xmlns 5 7 12
109  
110 12 $xml = join('', @$pi ) . $xml;
111 12 ▶ $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n$xml" if $param{header};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"header"} 1 11 12
112  
113 12 return $xml;
114 }
115  
116  
117 134 ▶sub struct {
Subroutine struct: called CC=10 SCAR=23.7
Pod documented
118 134 my ($self, $json) = @_;
119 134 my $struct = { };
120 134 foreach my $property (keys %$self) {
121 263 ▶ next unless $property =~ /^[a-z]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $property =~ /^[a-z]+$/ 3 260 263
122 260 ▶ if (ref $self->{$property} eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $self->{$property} eq 'ARRAY') { } 21 239 260
elsif (UNIVERSAL::isa($self->{$property}, 'DAIA::Object')) { } 12 227 239
elsif (UNIVERSAL::isa($self->{$property}, 'JSON::Boolean') and not $json) { } 0 227 227
elsif ($property eq 'label' and $self->{$property} eq '') { } 15 212 227
123 21 $struct->{$property} = [ map { $_->struct($json) } @{$self->{$property}} ];
124 } elsif ( UNIVERSAL::isa( $self->{$property}, "DAIA::Object" ) ) {
125 12 $struct->{$property} = $self->{$property}->struct;
126 ▶ } elsif ( UNIVERSAL::isa( $self->{$property}, 'JSON::Boolean' ) and not $json ) {
Condition33%1/3
UNIVERSAL::isa($self->{$property}, "JSON::Boolean") and not $json
!ll&&!rl&&r
22700
MC/DC0%0/2
UNIVERSAL::isa($self->{$property}, "JSON::Boolean") and not $json
UNIVERSAL::isa($self->{$property}, "JSON::Boolean") not $json
Truth table33%1/3
A and not(B)
A= UNIVERSAL::isa($self->{$property}, "JSON::Boolean")B= not $json
ABresult
0X0
100
111
127 0 ▶ $struct->{$property} = $self->{$property} ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$self->{$property} ? : 0 0 0
128 ▶ } elsif( $property eq 'label' and $self->{$property} eq '' ) {
Condition100%3/3
$property eq "label" and $self->{$property} eq ""
!ll&&!rl&&r
210215
MC/DC100%2/2
$property eq "label" and $self->{$property} eq ""
$property eq "label" $self->{$property} eq ""
Truth table100%3/3
A and B
A= $property eq "label"B= $self->{$property} eq ""
ABresult
0X0
100
111
129 # ignore empty string label
130 } else {
131 # remove characters not allowed in XML 1.0
132 212 my $value = $self->{$property};
133 212 $value =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;
134 212 $struct->{$property} = $value;
135 }
136 }
137 134 return $struct;
138 }
139  
140  
141 11 ▶sub json {
Subroutine json: called CC=6 SCAR=18.0
Pod documented
142 11 my ($self, $callback) = @_;
143 $callback = $self->{_hidden}->{callback}
144 11 ▶ if @_ < 2 and $self->{_hidden} and exists $self->{_hidden}->{callback};
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 and $self->{"_hidden"} and exists $self->{"_hidden"}{"callback"} 1 10 11
Condition83%5/6
@_ < 2 and $self->{"_hidden"} and exists $self->{"_hidden"}{"callback"}
!ll&&!rl&&r
1001
@_ < 2 and $self->{"_hidden"}
!ll&&!rl&&r
281
MC/DC66%2/3
@_ < 2 and $self->{"_hidden"} and exists $self->{"_hidden"}{"callback"}
@_ < 2 $self->{"_hidden"} exists $self->{"_hidden"}{"callback"}
Truth table75%3/4
A and B and C
A= @_ < 2B= $self->{"_hidden"}C= exists $self->{"_hidden"}{"callback"}
ABCresult
0XX0
10X0
1100
1111
145 11 my $json = JSON->new->pretty->encode( $self->struct(1) );
146 11 ▶ if ( defined $callback and $callback =~ /^[a-z][a-z0-9._\[\]]*$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback and $callback =~ /^[a-z][a-z0-9._\[\]]*$/i) { } 2 9 11
Condition66%2/3
defined $callback and $callback =~ /^[a-z][a-z0-9._\[\]]*$/i
!ll&&!rl&&r
902
MC/DC50%1/2
defined $callback and $callback =~ /^[a-z][a-z0-9._\[\]]*$/i
defined $callback $callback =~ /^[a-z][a-z0-9._\[\]]*$/i
Truth table66%2/3
A and B
A= defined $callbackB= $callback =~ /^[a-z][a-z0-9._\[\]]*$/i
ABresult
0X0
100
111
147 2 return "$callback($json);"
148 } else {
149 9 return $json;
150 }
151 }
152  
153  
154 0 ▶sub rdfhash {
Subroutine rdfhash: not called CC=1 SCAR=6.9
Pod documented
155 0 return { };
156 }
157  
158  
159 0 ▶sub serialize {
Subroutine serialize: not called CC=13 SCAR=52.0
Pod documented
160 0 my ($self, $format) = @_;
161 0 ▶ return unless $format and grep { $_ eq $format } DAIA->formats;
Branch: 1 branch
BranchTrueFalseTotal
unless $format and grep {$_ eq $format;} "DAIA"->formats 0 0 0
Condition0%0/3
$format and grep {$_ eq $format;} "DAIA"->formats
!ll&&!rl&&r
000
MC/DC0%0/2
$format and grep {$_ eq $format;} "DAIA"->formats
$format grep {$_ eq $format;} "DAIA"->formats
Truth table0%0/3
A and B
A= $formatB= grep {$_ eq $format;} "DAIA"->formats
ABresult
0X0
100
111
162  
163 0 my $content = '';
164  
165 0 ▶ if ($format eq 'xml') {
Branch: 4 branches
BranchTrueFalseTotal
if ($format eq 'xml') { } 0 0 0
elsif ($format eq 'json') { } 0 0 0
elsif ($format eq 'rdfjson') { } 0 0 0
elsif ($DAIA::TRINE_SERIALIZER) { } 0 0 0
166 0 $content = $self->xml(xmnls => 1);
167 } elsif ($format eq 'json') {
168 0 $content = $self->json;
169 } elsif ($format eq 'rdfjson') {
170 0 $content = JSON->new->pretty->encode($self->rdfhash());
171 } elsif ( $DAIA::TRINE_SERIALIZER ) {
172 0 my %opt;
173 # NOTE: RDF/XML dumps all namespaces, so avoid it
174 0 ▶ $opt{namespaces} = $DAIA::RDF_NS if $DAIA::RDF_NS and $format ne 'rdfxml';
Branch: 1 branch
BranchTrueFalseTotal
if $DAIA::RDF_NS and $format ne "rdfxml" 0 0 0
Condition0%0/3
$DAIA::RDF_NS and $format ne "rdfxml"
!ll&&!rl&&r
000
MC/DC0%0/2
$DAIA::RDF_NS and $format ne "rdfxml"
$DAIA::RDF_NS $format ne "rdfxml"
Truth table0%0/3
A and B
A= $DAIA::RDF_NSB= $format ne "rdfxml"
ABresult
0X0
100
111
175 0 my $ser;
176 0 ▶ if ( $DAIA::GRAPHVIZ and $DAIA::TRINE_MODEL and $format =~ /^(dot|svg)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DAIA::GRAPHVIZ and $DAIA::TRINE_MODEL and $format =~ /^(dot|svg)$/) { } 0 0 0
Condition0%0/6
$DAIA::GRAPHVIZ and $DAIA::TRINE_MODEL and $format =~ /^(dot|svg)$/
!ll&&!rl&&r
000
$DAIA::GRAPHVIZ and $DAIA::TRINE_MODEL
!ll&&!rl&&r
000
MC/DC0%0/3
$DAIA::GRAPHVIZ and $DAIA::TRINE_MODEL and $format =~ /^(dot|svg)$/
$DAIA::GRAPHVIZ $DAIA::TRINE_MODEL $format =~ /^(dot|svg)$/
Truth table0%0/4
A and B and C
A= $DAIA::GRAPHVIZB= $DAIA::TRINE_MODELC= $format =~ /^(dot|svg)$/
ABCresult
0XX0
10X0
1100
1111
177 0 $ser = $DAIA::GRAPHVIZ->new( as => $format, %opt );
178 } else {
179 0 $ser = eval { $DAIA::TRINE_SERIALIZER->new( $format, %opt ); };
180 }
181 0 ▶ if ($ser) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ser) 0 0 0
182 # NOTE: We could get rid of RDF::Trine::Model if hashref converted directly to iterator
183 0 my $model = $DAIA::TRINE_MODEL->temporary_model;
184 0 $model->add_hashref( $self->rdfhash );
185 0 $content = $ser->serialize_model_to_string( $model );
186 }
187 }
188  
189 0 return $content;
190 }
191  
192  
193 4 ▶sub rdfuri {
Subroutine rdfuri: called CC=2 SCAR=6.9
Pod documented
194 4 my $self = shift;
195 4 ▶ return $self->{id} if $self->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"id"} 3 1 4
196 1 my $id = lc(ref($self)).refaddr($self);
197 1 $id =~ s/.*::/_:/;
198 1 return $id;
199 }
200  
201 0 ▶sub rdftype { }
Subroutine rdftype: not called CC=1 SCAR=0.0
Pod undocumented
202  
203  
204  
205 1443 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=38 SCAR=38.3
206 1443 my $self = shift;
207 1443 ▶ my $class = ref($self) or croak "$self is not an object";
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or croak("$self is not an object") 1443 0 1443
208  
209 1443 my $method = $AUTOLOAD;
210 1443 $method =~ s/.*:://;
211 1443 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 211 1232 1443
212  
213 1232 my $property = $method;
214 1232 ▶ $property = lc($2) if $property =~ /^(add|provide)([A-Z][a-z]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $property =~ /^(add|provide)([A-Z][a-z]+)$/ 9 1223 1232
215  
216 12 ▶ no strict 'refs'; ##no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
217 1232 my $PROPERTIES = \%{$class."::PROPERTIES"};
218  
219 croak "Method $class->$method ($property) does not exist"
220 1232 ▶ unless exists $PROPERTIES->{$property};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $PROPERTIES->{$property} 1 1231 1232
221  
222 1231 my $opt = $PROPERTIES->{$property};
223  
224 # TODO: conflicting properties?
225
226 1231 ▶ if ( $method =~ /^add/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method =~ /^add/) { } 9 1222 1231
elsif ($method =~ /^provide/) { } 0 1222 1222
227 croak "$class->$property is not repeatable or has no type"
228 9 ▶ unless $opt->{repeatable} and $opt->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"repeatable"} and $opt->{"type"} 0 9 9
Condition33%1/3
$opt->{"repeatable"} and $opt->{"type"}
!ll&&!rl&&r
009
MC/DC0%0/2
$opt->{"repeatable"} and $opt->{"type"}
$opt->{"repeatable"} $opt->{"type"}
Truth table33%1/3
A and B
A= $opt->{"repeatable"}B= $opt->{"type"}
ABresult
0X0
100
111
229 9 my $value = $_[0];
230 9 ▶ if ( not UNIVERSAL::isa( $_[0], $opt->{type} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($_[0], $opt->{"type"})) 8 1 9
231 8 $value = eval $opt->{type}."->new( \@_ )"; ##no critic
232 8 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
233 }
234 9 return $self->add( $value );
235 } elsif( $method =~ /^provide/ ) { # set only if not set
236 0 ▶ if ( defined $self->{$property} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$property}) { } 0 0 0
237 # getter
238 0 ▶ return $opt->{repeatable} ? @{$self->{$property} || []} : $self->{$property}
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'repeatable'} ? : 0 0 0
Condition0%0/2
$self->{$property} || []
l!l
00
MC/DC0%0/1
$self->{$property} || []
$self->{$property}
Truth table0%0/2
A
A= $self->{$property}
Aresult
11
00
239 } else {
240 0 return eval "\$self->$property(\@_)"; ##no critic
241 }
242 }
243  
244 # called as getter
245 1222 ▶ return $opt->{repeatable} ? @{$self->{$property} || []} : $self->{$property}
Branch: 2 branches
BranchTrueFalseTotal
if @_ == 0 158 1064 1222
$opt->{'repeatable'} ? : 74 84 158
Condition100%2/2
$self->{$property} || []
l!l
5816
MC/DC100%1/1
$self->{$property} || []
$self->{$property}
Truth table100%2/2
A
A= $self->{$property}
Aresult
11
00
246 if ( @_ == 0 );
247  
248 1064 my $value = $_[0];
249  
250 # called as clearer (may imply setting the default value)
251 1064 ▶ if (not defined $value or (ref($value) eq 'ARRAY' and @{$value} == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value eq "ARRAY" and @{$value;} == 0) 670 394 1064
Condition100%6/6
not defined $value or ref $value eq "ARRAY" and @{$value;} == 0
l!l&&r!l&&!r
6664394
ref $value eq "ARRAY" and @{$value;} == 0
!ll&&!rl&&r
371234
MC/DC100%3/3
not defined $value or ref $value eq "ARRAY" and @{$value;} == 0
not defined $value ref $value eq "ARRAY" @{$value;} == 0
Truth table100%4/4
not(A) or B and C
A= not defined $valueB= ref $value eq "ARRAY"C= @{$value;} == 0
ABCresult
1XX1
0111
00X0
0100
252 670 ▶ if ( exists $opt->{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"default"}) 162 508 670
253 $value = ref($opt->{default}) eq 'CODE'
254 162 ▶ ? $opt->{default}() : $opt->{default};
Branch: 1 branch
BranchTrueFalseTotal
ref $opt->{'default'} eq 'CODE' ? : 50 112 162
255 }
256 669 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 97 572 669
257 97 $self->{$property} = $value;
258 } else {
259 572 ▶ delete $self->{$property} if exists $self->{$property};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$property} 18 554 572
260 }
261 669 return;
262 }
263  
264 394 ▶ if ( $opt->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'type'}) { } 85 309 394
265 # set one or more typed values
266  
267 # arguments must be either an array ref or a list of types or a simple list
268 85 my @args;
269  
270 85 ▶ if ( ref($_[0]) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 22 63 85
elsif (UNIVERSAL::isa($_[0], $opt->{'type'})) { } 31 32 63
271 22 ▶ croak "too many arguments" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 22 22
272 22 @args = @{$_[0]};
273 } elsif ( UNIVERSAL::isa( $_[0], $opt->{type} ) ) {
274 # treat ( $obj, ... ) as ( [ $obj, ... ] )
275 31 @args = @_;
276 } else {
277 32 @args = ( [ @_ ] ); # one element
278 }
279  
280 croak "$class->$property is not repeatable"
281 85 ▶ if ( @args > 1 and not $opt->{repeatable});
Branch: 1 branch
BranchTrueFalseTotal
if @args > 1 and not $opt->{"repeatable"} 0 85 85
Condition66%2/3
@args > 1 and not $opt->{"repeatable"}
!ll&&!rl&&r
73120
MC/DC0%0/2
@args > 1 and not $opt->{"repeatable"}
@args > 1 not $opt->{"repeatable"}
Truth table66%2/3
A and not(B)
A= @args > 1B= not $opt->{"repeatable"}
ABresult
0X0
100
111
282  
283 my @values = map {
284 85 my $v;
285 97 ▶ if ( ref($_) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 32 65 97
elsif (UNIVERSAL::isa($_, $opt->{'type'})) { } 56 9 65
286 32 $v = eval $opt->{type}.'->new( @{$_} )'; ##no critic
287 32 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 32 32
288 } elsif ( UNIVERSAL::isa( $_, $opt->{type} ) ) {
289 56 $v = $_;
290 } else {
291 9 $v = eval $opt->{type}.'->new( $_ )'; ##no critic
292 9 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
293 }
294 97 $v;
295 } @args;
296  
297 85 ▶ $self->{$property} = $opt->{repeatable} ? \@values : $values[0];
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'repeatable'} ? : 63 22 85
298  
299 } else {
300 # set an untyped value (never repeatable, stringified unless filtered)
301 309 ▶ if( $opt->{fixed} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->{'fixed'}) { } 0 309 309
elsif ($opt->{'filter'}) { } 309 0 309
302 0 $value = $opt->{fixed};
303 } elsif( $opt->{filter} ) {
304 309 $value = $opt->{filter}( @_ );
305 309 ▶ croak "$class->$property did not pass value constraint: " . join(',',@_)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 12 297 309
306 unless defined $value;
307 } else {
308 0 $value = "$value";
309 }
310  
311 297 $self->{$property} = $value;
312 }
313  
314 382 $self; # if called as setter, return the object for chaining
315 }
316  
317  
318 25 ▶sub xml_write {
Subroutine xml_write: called CC=12 SCAR=24.8
Pod documented
319 25 my ($name, $struct, $level) = @_;
320  
321 25 my $indent = (' ' x $level);
322 25 my $tag = "$indent<$name";
323  
324 25 my $content = '';
325 25 ▶ if (defined $struct->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $struct->{"content"}) 13 12 25
326 13 $content = $struct->{content};
327 13 delete $struct->{content};
328 }
329  
330 25 ▶ my @attr = grep { ! ref($struct->{$_}) and $_ ne 'label' } keys %$struct;
Condition100%3/3
!ref($struct->{$_}) && $_ ne 'label'
!ll&&!rl&&r
13223
MC/DC100%2/2
!ref($struct->{$_}) && $_ ne 'label'
!ref($struct->{$_}) $_ ne 'label'
Truth table100%3/3
not(A) && B
A= !ref($struct->{$_})B= $_ ne 'label'
ABresult
0X0
100
111
331 25 @attr = map { "$_=\"".xml_escape_value($struct->{$_}).'"' } @attr;
332 25 ▶ $tag .= " " . join(" ", @attr) if @attr;
Branch: 1 branch
BranchTrueFalseTotal
if @attr 18 7 25
333  
334 # get the right order
335 25 my @order = qw(message institution document label department storage available unavailable);
336 25 my @children = grep { defined $struct->{$_} } @order;
337 25 my %has = map { $_ => 1 } @children;
338 # append additional children
339 25 ▶ push @children, grep { ref($struct->{$_}) and not $has{$_} } keys %$struct;
Condition100%3/3
ref $struct->{$_} && !$has{$_}
!ll&&!rl&&r
25112
MC/DC100%2/2
ref $struct->{$_} && !$has{$_}
ref $struct->{$_} !$has{$_}
Truth table100%3/3
A && not(B)
A= ref $struct->{$_}B= !$has{$_}
ABresult
0X0
100
111
340  
341 25 my @lines;
342 25 ▶ if (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) { } 6 19 25
343 6 push @lines, "$tag>";
344 6 foreach my $k (@children) {
345 15 $k =~ s/^\d//;
346 15 ▶ if ( $k eq 'label' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($k eq 'label') { } 2 13 15
elsif (ref $struct->{$k} eq 'HASH') { } 3 10 13
elsif (ref $struct->{$k} eq 'ARRAY') { } 10 0 10
347 2 push @lines, "$indent <label>".xml_escape_value($struct->{label})."</label>";
348 } elsif ( ref($struct->{$k}) eq 'HASH' ) {
349 3 push @lines, xml_write($k, $struct->{$k}, $level+1);
350 } elsif ( ref($struct->{$k}) eq 'ARRAY' ) {
351 10 foreach my $v (@{$struct->{$k}}) {
352 10 push @lines, xml_write($k, $v, $level+1);
353 }
354 }
355 }
356 6 push @lines, "$indent</$name>";
357 } else {
358 19 ▶ if ( $content ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content ne '') { } 13 6 19
359 13 push @lines, "$tag>" . xml_escape_value($content) . "</$name>";
360 } else {
361 6 push @lines, "$tag />";
362 }
363 }
364
365 25 return join("\n", @lines);
366 }
367  
368  
369 38 ▶sub xml_escape_value {
Subroutine xml_escape_value: called CC=2 SCAR=7.0
Pod documented
370 38 my($data) = @_;
371 38 ▶ return '' unless defined($data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 38 38
372 38 $data =~ s/&/&amp;/sg;
373 38 $data =~ s/</&lt;/sg;
374 38 $data =~ s/>/&gt;/sg;
375 38 $data =~ s/"/&quot;/sg;
376 38 return $data;
377 }
378  
379  
380 31 ▶sub _buildargs {
Subroutine _buildargs: called CC=2 SCAR=7.1
381 31 shift;
382 31 ▶ croak "uneven parameter list" if (@_ % 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 31 31
383 31 @_;
384 };
385  
386  
387 12 ▶sub _hidden_prop {
Subroutine _hidden_prop: called CC=5 SCAR=16.1
388 12 my $self = shift;
389 12 ▶ return unless $self->{_hidden};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_hidden"} 11 1 12
390  
391 1 my $hashref = shift;
392 1 foreach ( @HIDDEN_PROPERTIES ) {
393 11 ▶ next if exists $hashref->{$_} or not exists $self->{_hidden}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hashref->{$_} or not exists $self->{"_hidden"}{$_} 9 2 11
Condition66%2/3
exists $hashref->{$_} or not exists $self->{"_hidden"}{$_}
l!l&&r!l&&!r
092
MC/DC50%1/2
exists $hashref->{$_} or not exists $self->{"_hidden"}{$_}
exists $hashref->{$_} not exists $self->{"_hidden"}{$_}
Truth table66%2/3
A or not(B)
A= exists $hashref->{$_}B= not exists $self->{"_hidden"}{$_}
ABresult
1X1
011
000
394 2 $hashref->{$_} = $self->{_hidden}->{$_};
395 }
396 }
397  
398  
399 0 ▶sub _enable_utf8_layer {
Subroutine _enable_utf8_layer: not called CC=4 SCAR=30.0
400 0 my $fh = shift;
401 0 ▶ return unless eval{ can($fh, 'binmode'); };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { can($fh, "binmode") } } 0 0 0
402 0 foreach my $layer ( PerlIO::get_layers( $fh ) ) {
403 0 ▶ return if $layer =~ /^encoding|^utf8/;
Branch: 1 branch
BranchTrueFalseTotal
if $layer =~ /^encoding|^utf8/ 0 0 0
404 }
405 0 binmode $fh, ':encoding(UTF-8)';
406 }
407  
408 our %COMMON_PROPERTIES =(
409 id => {
410 14 ▶ filter => sub { my $v = "$_[0]"; $v =~ s/^\s+|\s$//g; is_uri($v) ? $v : undef; }
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
is_uri($v) ? : 14 0 14
411 },
412 href => {
413 17 ▶ filter => sub { my $v = "$_[0]"; is_web_uri($v) ? $v : undef; },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
is_web_uri($v) ? : 17 0 17
414 12 predicate => 'http://xmlns.com/foaf/0.1/page',
415 rdftype => 'resource'
416 },
417 message => {
418 type => 'DAIA::Message',
419 repeatable => 1,
420 predicate => 'http://purl.org/dc/terms/description',
421 },
422 );
423  
424 12 1;
425  
426 __END__
« DAIA.pm ↑ Summary DAIA/Availability.pm »