blib/lib/IRI.pm

filter: Statementstmt 84.8% 252 / 297 Branchbran 78.3% 94 / 120 Conditioncond 64.7% 46 / 71 MC/DCmcdc 48.7% 19 / 39 Subroutinesub 83.3% 20 / 24 Podpod 83.3% 5 / 6 totaltotal 78.2% 436 / 557 CC 92 SCAR 27.6
CC
92
Cov
80%
rel
26.9
_resolved_components
25.7
_remove_dot_segments
15.2
CRAP 15.7
↑ Summary
1 =encoding utf8
2  
3 =head1 NAME
4  
5 IRI - Internationalized Resource Identifiers
6  
7 =head1 VERSION
8  
9 This document describes IRI version 0.16
10  
11 =head1 SYNOPSIS
12  
13 use IRI;
14
15 my $i = IRI->new(value => 'https://example.org:80/index#frag');
16 say $i->scheme; # 'https'
17 say $i->path; # '/index'
18  
19 my $base = IRI->new(value => "http://www.hestebedg\x{e5}rd.dk/");
20 my $i = IRI->new(value => '#frag', base => $base);
21 say $i->abs; # 'http://www.hestebedgÄrd.dk/#frag'
22  
23 # Defer parsing of the IRI until necessary
24 my $i = IRI->new(value => "http://www.hestebedg\x{e5}rd.dk/", lazy => 1);
25 say $i->path; # path is parsed here
26  
27 =head1 DESCRIPTION
28  
29 The IRI module provides an object representation for Internationalized
30 Resource Identifiers (IRIs) as defined by
31 L<RFC 3987|http://www.ietf.org/rfc/rfc3987.txt> and supports their parsing,
32 serializing, and base resolution.
33  
34 =head1 ATTRIBUTES
35  
36 =over 4
37  
38 =item C<< lazy >>
39  
40 A boolean value indicating whether the IRI should be parsed (and validated)
41 during object construction (false), or parsed only when an IRI component is
42 accessed (true). If no components are ever needed (e.g. an IRI is constructed
43 with a C<< value >> and C<< value >> is the only accessor ever called), no
44 parsing will take place.
45  
46 =back
47  
48 =head1 METHODS
49  
50 =over 4
51  
52 =item C<< as_string >>
53  
54 Returns the absolute IRI string resolved against the base IRI, if present;
55 the relative IRI string otherwise.
56  
57 =item C<< abs >>
58  
59 Returns the absolute IRI string (resolved against the base IRI if present).
60  
61 =item C<< scheme >>
62  
63 =item C<< host >>
64  
65 =item C<< port >>
66  
67 =item C<< user >>
68  
69 =item C<< path >>
70  
71 =item C<< fragment >>
72  
73 =item C<< query >>
74  
75 Returns the respective component of the parsed IRI.
76  
77 =cut
78  
79 {
80 package IRI;
81 5 ▶ use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 5 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 5 our $VERSION = '0.16';
84 5 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 5 ▶ use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 5 ▶ use Types::Standard qw(Str InstanceOf HashRef Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
87 5 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
88
89 # class_type 'URI';
90 # coerce 'IRI' => from 'Str' => via { IRI->new( value => $_ ) };
91 # coerce 'IRI' => from 'URI' => via { IRI->new( value => $_->as_string ) };
92  
93 5 has 'lazy' => (is => 'ro', isa => Bool, default => 0);
94 5 has '_initialized' => (is => 'rw', isa => Bool, default => 0, init_arg => undef);
95 31 ▶ has 'base' => (is => 'ro', isa => InstanceOf['IRI'], predicate => 'has_base', coerce => sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
96 31 my $base = shift;
97 31 ▶ if (blessed($base)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($base)) { } 31 0 31
98 31 ▶ if ($base->isa('IRI')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($base->isa('IRI')) { } 30 1 31
elsif ($base->isa('URI')) { } 1 0 1
99 30 return $base;
100 } elsif ($base->isa('URI')) {
101 1 return IRI->new( value => $base->as_string );
102 }
103 } else {
104 0 return IRI->new($base);
105 }
106 5 });
107 5 has 'value' => (is => 'ro', isa => Str, default => '');
108 5 has 'components' => (is => 'ro', writer => '_set_components');
109 5 has 'abs' => (is => 'ro', lazy => 1, builder => '_abs');
110 5 has 'resolved_components' => (
111 is => 'ro',
112 isa => HashRef,
113 lazy => 1,
114 builder => '_resolved_components',
115 predicate => 1,
116 handles_via => 'Hash',
117 handles => {
118 authority => [ accessor => 'authority' ],
119 scheme => [ accessor => 'scheme' ],
120 host => [ accessor => 'host' ],
121 port => [ accessor => 'port' ],
122 user => [ accessor => 'user' ],
123 path => [ accessor => 'path' ],
124 fragment => [ accessor => 'fragment' ],
125 query => [ accessor => 'query' ],
126 },
127 );
128  
129 189 ▶ around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
130 189 my $orig = shift;
131 189 my $class = shift;
132 189 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 0 189 189
133 0 return $class->$orig(value => shift);
134 }
135 189 return $class->$orig(@_);
136 5 };
137
138 189 ▶ sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
139 189 my $self = shift;
140 189 ▶ if ($self->has_resolved_components) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_resolved_components) { } 2 187 189
141 2 $self->_set_components($self->resolved_components);
142 2 $self->_initialized(1);
143 } else {
144 187 ▶ unless ($self->lazy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lazy) 187 0 187
145 187 my $comp = $self->_parse_components($self->value);
146 }
147 }
148 }
149
150 1030 ▶ before [qw(components as_string abs resolved_components scheme host port user path fragment query)] => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
151 1030 my $self = shift;
152 1030 ▶ if (not $self->_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_initialized) 2 1028 1030
153 # warn "Lazily initializing IRI";
154 2 my $comp = $self->_parse_components($self->value);
155 }
156 5 };
157  
158 # These regexes are (mostly) from the syntax grammar in RFC 3987
159 5 my $HEXDIG = qr<[0-9A-F]>o;
160 5 my $ALPHA = qr<[A-Za-z]>o;
161 5 my $subdelims = qr<[!\$&'()*+,;=]>xo;
162 5 my $gendelims = qr<[":/?#@] | \[ | \]>xo;
163 5 my $reserved = qr<${gendelims} | ${subdelims}>o;
164 5 my $unreserved = qr<${ALPHA} | [0-9] | [-._~]>xo;
165 5 my $pctencoded = qr<%[0-9A-Fa-f]{2}>o;
166 5 my $decoctet = qr<
167 [0-9] # 0-9
168 | [1-9][0-9] # 10-99
169 | 1 [0-9]{2} # 100-199
170 | 2 [0-4] [0-9] # 200-249
171 | 25 [0-5] # 250-255
172 >xo;
173 5 my $IPv4address = qr<
174 # IPv4address
175 ${decoctet}[.]${decoctet}[.]${decoctet}[.]${decoctet}
176 >xo;
177 5 my $h16 = qr<${HEXDIG}{1,4}>o;
178 5 my $ls32 = qr<
179 ( ${h16} : ${h16} )
180 | ${IPv4address}
181 >xo;
182 5 my $IPv6address = qr<
183 # IPv6address
184 ( ( ${h16} : ){6} ${ls32})
185 | ( :: ( ${h16} : ){5} ${ls32})
186 | (( ${h16} )? :: ( ${h16} : ){4} ${ls32})
187 | (( ( ${h16} : ){0,1} ${h16} )? :: ( ${h16} : ){3} ${ls32})
188 | (( ( ${h16} : ){0,2} ${h16} )? :: ( ${h16} : ){2} ${ls32})
189 | (( ( ${h16} : ){0,3} ${h16} )? :: ${h16} : ${ls32})
190 | (( ( ${h16} : ){0,4} ${h16} )? :: ${ls32})
191 | (( ( ${h16} : ){0,5} ${h16} )? :: ${h16})
192 | (( ( ${h16} : ){0,6} ${h16} )? ::)
193 >xo;
194 5 my $IPvFuture = qr<v (${HEXDIG})+ [.] ( ${unreserved} | ${subdelims} | : )+>xo;
195 5 my $IPliteral = qr<\[
196 # IPliteral
197 (${IPv6address} | ${IPvFuture})
198 \]
199 >xo;
200 5 my $port = qr<(?<port>[0-9]*)>o;
201 5 my $scheme = qr<(?<scheme>${ALPHA} ( ${ALPHA} | [0-9] | [+] | [-] | [.] )*)>xo;
202 5 my $iprivate = qr<[\x{E000}-\x{F8FF}] | [\x{F0000}-\x{FFFFD}] | [\x{100000}-\x{10FFFD}]>xo;
203 5 my $ucschar = qr<
204 [\x{a0}-\x{d7ff}] | [\x{f900}-\x{fdcf}] | [\x{fdf0}-\x{ffef}]
205 | [\x{10000}-\x{1FFFD}] | [\x{20000}-\x{2FFFD}] | [\x{30000}-\x{3FFFD}]
206 | [\x{40000}-\x{4FFFD}] | [\x{50000}-\x{5FFFD}] | [\x{60000}-\x{6FFFD}]
207 | [\x{70000}-\x{7FFFD}] | [\x{80000}-\x{8FFFD}] | [\x{90000}-\x{9FFFD}]
208 | [\x{A0000}-\x{AFFFD}] | [\x{B0000}-\x{BFFFD}] | [\x{C0000}-\x{CFFFD}]
209 | [\x{D0000}-\x{DFFFD}] | [\x{E1000}-\x{EFFFD}]
210 >xo;
211 5 my $iunreserved = qr<${ALPHA}|[0-9]|[-._~]|${ucschar}>o;
212 5 my $ipchar = qr<(${iunreserved})|(${pctencoded})|(${subdelims})|:|@>o;
213 5 my $ifragment = qr<(?<fragment>(${ipchar}|/|[?])*)>o;
214 5 my $iquery = qr<(?<query>(${ipchar}|${iprivate}|/|[?])*)>o;
215 5 my $isegmentnznc = qr<(${iunreserved}|${pctencoded}|${subdelims}|@)+ # non-zero-length segment without any colon ":"
216 >xo;
217 5 my $isegmentnz = qr<${ipchar}+>o;
218 5 my $isegment = qr<${ipchar}*>o;
219 5 my $ipathempty = qr<>o;
220 5 my $ipathrootless = qr<(?<path>${isegmentnz}(/${isegment})*)>o;
221 5 my $ipathnoscheme = qr<(?<path>${isegmentnznc}(/${isegment})*)>o;
222 5 my $ipathabsolute = qr<(?<path>/(${isegmentnz}(/${isegment})*)?)>o;
223 5 my $ipathabempty = qr<(?<path>(/${isegment})*)>o;
224 5 my $ipath = qr<
225 ${ipathabempty} # begins with "/" or is empty
226 | ${ipathabsolute} # begins with "/" but not "//"
227 | ${ipathnoscheme} # begins with a non-colon segment
228 | ${ipathrootless} # begins with a segment
229 | ${ipathempty} # zero characters
230 >xo;
231 5 my $iregname = qr<(${iunreserved}|${pctencoded}|${subdelims})*>o;
232 5 my $ihost = qr<(?<host>${IPliteral}|${IPv4address}|${iregname})>o;
233 5 my $iuserinfo = qr<(?<user>(${iunreserved}|${pctencoded}|${subdelims}|:)*)>o;
234 5 my $iauthority = qr<(?<authority>(${iuserinfo}@)?${ihost}(:${port})?)>o;
235 5 my $irelativepart = qr<
236 (//${iauthority}${ipathabempty})
237 | ${ipathabsolute}
238 | ${ipathnoscheme}
239 | ${ipathempty}
240 >xo;
241 5 my $irelativeref = qr<${irelativepart}([?]${iquery})?(#${ifragment})?>o;
242 5 my $ihierpart = qr<(//${iauthority}${ipathabempty})|(${ipathabsolute})|(${ipathrootless})|(${ipathempty})>o;
243 5 my $absoluteIRI = qr<${scheme}:${ihierpart}([?]${iquery})?>o;
244 5 my $IRI = qr<${scheme}:${ihierpart}([?]${iquery})?(#${ifragment})?>o;
245 5 my $IRIreference = qr<${IRI}|${irelativeref}>o;
246 189 ▶ sub _parse_components {
Subroutine _parse_components: called CC=4 SCAR=13.9
247 189 my $self = shift;
248 189 my $v = shift;
249 189 my $c;
250
251 189 ▶ if ($v =~ /\A(${IRIreference})\Z/mso and length($1) == length($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /\A($IRIreference)\Z/mos and length $1 == length $v) { } 188 1 189
Condition66%2/3
$v =~ /\A($IRIreference)\Z/mos and length $1 == length $v
!ll&&!rl&&r
01188
MC/DC50%1/2
$v =~ /\A($IRIreference)\Z/mos and length $1 == length $v
$v =~ /\A($IRIreference)\Z/mos length $1 == length $v
Truth table66%2/3
A and B
A= $v =~ /\A($IRIreference)\Z/mosB= length $1 == length $v
ABresult
0X0
100
111
252 188 %$c = %+;
253 } else {
254 5 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
255 1 die "Not a valid IRI? " . Dumper($v);
256 }
257
258 188 ▶ $c->{path} //= '';
Condition100%2/2
$c->{"path"} //= ""
l!l
17216
MC/DC100%1/1
$c->{"path"} //= ""
$c->{"path"}
Truth table100%2/2
A
A= $c->{"path"}
Aresult
11
00
259 188 $self->_set_components($c);
260 188 $self->_initialized(1);
261 }
262
263 21 ▶ sub _merge {
Subroutine _merge: called CC=5 SCAR=17.8
264 21 my $self = shift;
265 21 my $base = shift;
266
267 21 my $bc = $base->components;
268 21 my $c = $self->components;
269 21 ▶ my $base_has_authority = ($bc->{user} or $bc->{port} or defined($bc->{host}));
Condition33%2/6
$bc->{'user'} || $bc->{'port'} || defined $bc->{'host'}
l!l&&r!l&&!r
0210
$bc->{"user"} or $bc->{"port"}
l!l&&r!l&&!r
0021
MC/DC0%0/3
$bc->{'user'} || $bc->{'port'} || defined $bc->{'host'}
$bc->{"user"} $bc->{"port"} defined $bc->{'host'}
Truth table25%1/4
A or B || C
A= $bc->{"user"}B= $bc->{"port"}C= defined $bc->{'host'}
ABCresult
1XX1
01X1
0011
0000
270 21 ▶ if ($base_has_authority and not($bc->{path})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_has_authority and not $bc->{'path'}) { } 0 21 21
Condition33%1/3
$base_has_authority and not $bc->{"path"}
!ll&&!rl&&r
0210
MC/DC0%0/2
$base_has_authority and not $bc->{"path"}
$base_has_authority not $bc->{"path"}
Truth table33%1/3
A and not(B)
A= $base_has_authorityB= not $bc->{"path"}
ABresult
0X0
100
111
271 0 return "/" . $c->{path};
272 } else {
273 21 my $bp = $bc->{path};
274 21 my @pathParts = split('/', $bp, -1); # -1 limit means $path='/' splits into ('', '')
275 21 pop(@pathParts);
276 21 push(@pathParts, $c->{path});
277 21 my $path = join('/', @pathParts);
278 21 return $path;
279 }
280 }
281  
282 24 ▶ sub _remove_dot_segments {
Subroutine _remove_dot_segments: called CC=14 SCAR=27.2
283 24 my $self = shift;
284 24 my $input = shift;
285 24 my @output;
286 24 ▶ while (length($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input) 78 24 102
287 78 ▶ if ($input =~ m<^[.][.]/>) {
Branch: 8 branches
BranchTrueFalseTotal
if ($input =~ m[^[.][.]/]) { } 0 78 78
elsif ($input =~ m[^[.]/]) { } 0 78 78
elsif ($input =~ m[^/[.]/]) { } 2 76 78
elsif ($input eq '/.') { } 1 75 76
elsif ($input =~ m[^/[.][.]/]) { } 8 67 75
elsif ($input eq '/..') { } 2 65 67
elsif ($input eq '.') { } 0 65 65
elsif ($input eq '..') { } 0 65 65
288 0 substr($input, 0, 3) = '';
289 } elsif ($input =~ m<^[.]/>) {
290 0 substr($input, 0, 2) = '';
291 } elsif ($input =~ m<^/[.]/>) {
292 2 substr($input, 0, 3) = '/';
293 } elsif ($input eq '/.') {
294 1 $input = '/';
295 } elsif ($input =~ m<^/[.][.]/>) {
296 8 substr($input, 0, 4) = '/';
297 8 pop(@output);
298 } elsif ($input eq '/..') {
299 2 $input = '/';
300 2 pop(@output);
301 } elsif ($input eq '.') {
302 0 $input = '';
303 } elsif ($input eq '..') {
304 0 $input = '';
305 } else {
306 65 my $leadingSlash = ($input =~ m<^/>);
307 65 ▶ if ($leadingSlash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leadingSlash) 65 0 65
308 65 substr($input, 0, 1) = '';
309 }
310 65 my ($part, @parts) = split('/', $input, -1);
311 65 ▶ $part //= '';
Condition100%2/2
$part //= ""
l!l
569
MC/DC100%1/1
$part //= ""
$part
Truth table100%2/2
A
A= $part
Aresult
11
00
312 65 ▶ if (scalar(@parts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @parts) 42 23 65
313 42 unshift(@parts, '');
314 }
315 65 $input = join('/', @parts);
316 65 ▶ if ($leadingSlash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leadingSlash) 65 0 65
317 65 $part = "/$part";
318 }
319 65 push(@output, $part);
320 }
321 }
322 24 my $newPath = join('', @output);
323 24 return $newPath;
324 }
325  
326 139 ▶ sub _resolved_components {
Subroutine _resolved_components: called CC=23 SCAR=32.5
327 139 my $self = shift;
328 139 my $value = $self->value;
329 139 ▶ if ($self->has_base and not($self->components->{scheme})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_base and not $self->components->{"scheme"}) 30 109 139
Condition100%3/3
$self->has_base and not $self->components->{"scheme"}
!ll&&!rl&&r
108130
MC/DC100%2/2
$self->has_base and not $self->components->{"scheme"}
$self->has_base not $self->components->{"scheme"}
Truth table100%3/3
A and not(B)
A= $self->has_baseB= not $self->components->{"scheme"}
ABresult
0X0
100
111
330 # Resolve IRI relative to the base IRI
331 30 my $base = $self->base;
332 30 my $v = $self->value;
333 30 my $bv = $base->value;
334 # warn "resolving IRI <$v> relative to the base IRI <$bv>";
335 30 my %components = %{ $self->components };
336 30 my %base = %{ $base->components };
337 30 my %target;
338
339 30 ▶ if ($components{scheme}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($components{'scheme'}) { } 0 30 30
340 0 foreach my $k (qw(scheme user port host path query)) {
341 0 ▶ if (exists $components{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $components{$k}) 0 0 0
342 0 $target{$k} = $components{$k};
343 }
344 }
345 } else {
346 30 ▶ if ($components{user} or $components{port} or defined($components{host})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($components{'user'} or $components{'port'} or defined $components{'host'}) { } 1 29 30
Condition50%3/6
$components{"user"} or $components{"port"} or defined $components{"host"}
l!l&&r!l&&!r
0129
$components{"user"} or $components{"port"}
l!l&&r!l&&!r
0030
MC/DC33%1/3
$components{"user"} or $components{"port"} or defined $components{"host"}
$components{"user"} $components{"port"} defined $components{"host"}
Truth table50%2/4
A or B or C
A= $components{"user"}B= $components{"port"}C= defined $components{"host"}
ABCresult
1XX1
01X1
0011
0000
347 1 foreach my $k (qw(scheme user port host query)) {
348 5 ▶ if (exists $components{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $components{$k}) 1 4 5
349 1 $target{$k} = $components{$k};
350 }
351 }
352 1 my $path = $components{path};
353 1 $target{path} = $self->_remove_dot_segments($path);
354 } else {
355 29 ▶ if ($components{path} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($components{'path'} eq '') { } 6 23 29
356 6 $target{path} = $base{path};
357 6 ▶ if ($components{query}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($components{'query'}) { } 1 5 6
358 1 $target{query} = $components{query};
359 } else {
360 5 ▶ if ($base{query}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base{"query"}) 2 3 5
361 2 $target{query} = $base{query};
362 }
363 }
364 } else {
365 23 ▶ if ($components{path} =~ m<^/>) {
Branch: 1 branch
BranchTrueFalseTotal
if ($components{'path'} =~ m[^/]) { } 2 21 23
366 2 my $path = $components{path};
367 2 $target{path} = $self->_remove_dot_segments($path);
368 } else {
369 21 my $path = $self->_merge($base);
370 21 $target{path} = $self->_remove_dot_segments($path);
371 }
372 23 ▶ if (defined($components{query})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $components{"query"}) 3 20 23
373 3 $target{query} = $components{query};
374 }
375 }
376 29 ▶ if ($base{user} or $base{port} or defined($base{host})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base{"user"} or $base{"port"} or defined $base{"host"}) 29 0 29
Condition33%2/6
$base{"user"} or $base{"port"} or defined $base{"host"}
l!l&&r!l&&!r
0290
$base{"user"} or $base{"port"}
l!l&&r!l&&!r
0029
MC/DC0%0/3
$base{"user"} or $base{"port"} or defined $base{"host"}
$base{"user"} $base{"port"} defined $base{"host"}
Truth table25%1/4
A or B or C
A= $base{"user"}B= $base{"port"}C= defined $base{"host"}
ABCresult
1XX1
01X1
0011
0000
377 29 foreach my $k (qw(user port host)) {
378 87 ▶ if (exists $base{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $base{$k}) 29 58 87
379 29 $target{$k} = $base{$k};
380 }
381 }
382 }
383 }
384 30 ▶ if (defined($base{scheme})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base{"scheme"}) 30 0 30
385 30 $target{scheme} = $base{scheme};
386 }
387 }
388
389 30 ▶ if (defined($components{fragment})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $components{"fragment"}) 6 24 30
390 6 $target{fragment} = $components{fragment};
391 }
392
393 30 return \%target;
394 }
395 109 return $self->components;
396 }
397
398 132 ▶ sub _abs {
Subroutine _abs: called CC=1 SCAR=0.0
399 132 my $self = shift;
400 132 my $value = $self->_string_from_components( $self->resolved_components );
401 132 return $value;
402 }
403  
404 =item C<< rel ( $base ) >>
405  
406 Returns a new relative IRI object which, when resolved against the C<< $base >>
407 IRI, is equal to this IRI.
408  
409 =cut
410  
411 44 ▶ sub rel {
Subroutine rel: called CC=26 SCAR=32.9
Pod documented
412 # based on code in URI <https://metacpan.org/source/OALDERS/URI-1.76/lib/URI/_generic.pm#L191>
413 44 my $self = shift;
414 44 my $base = shift;
415 44 my $rel = IRI->new(value => $self->abs);
416
417 44 ▶ if (($base->scheme // '') ne ($rel->scheme // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (($base->scheme // "") ne ($rel->scheme // "")) 4 40 44
Condition50%2/4
$base->scheme // ""
l!l
440
$rel->scheme // ""
l!l
440
MC/DC0%0/1
$base->scheme // ""
$base->scheme
MC/DC0%0/1
$rel->scheme // ""
$rel->scheme
Truth table50%1/2
A
A= $base->scheme
Aresult
11
00
Truth table50%1/2
A
A= $rel->scheme
Aresult
11
00
418 4 return IRI->new(value => $rel->abs);
419 }
420  
421 40 my $scheme = $rel->scheme;
422 40 my $auth = $rel->authority;
423 40 my $path = $rel->path;
424
425 40 ▶ if (!defined($scheme) and !defined($auth)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scheme or defined $auth) 0 40 40
Condition33%1/3
defined $scheme or defined $auth
l!l&&r!l&&!r
4000
MC/DC0%0/2
defined $scheme or defined $auth
defined $scheme defined $auth
Truth table33%1/3
A or B
A= defined $schemeB= defined $auth
ABresult
1X1
011
000
426 0 return $rel;
427 }
428  
429 40 my $bscheme = $base->scheme;
430 40 my $bauth = $base->authority;
431 40 my $bpath = $base->path;
432  
433 40 for ($bscheme, $bauth, $auth) {
434 120 ▶ $_ = '' unless defined($_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 120 120
435 }
436
437 40 ▶ if ($scheme eq $bscheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq $bscheme) 40 0 40
438 40 $rel->scheme(undef);
439 }
440
441 40 ▶ unless ($scheme eq $bscheme and $auth eq $bauth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme eq $bscheme and $auth eq $bauth) 6 34 40
Condition66%2/3
$scheme eq $bscheme and $auth eq $bauth
!ll&&!rl&&r
0634
MC/DC50%1/2
$scheme eq $bscheme and $auth eq $bauth
$scheme eq $bscheme $auth eq $bauth
Truth table66%2/3
A and B
A= $scheme eq $bschemeB= $auth eq $bauth
ABresult
0X0
100
111
442 6 return IRI->new(value => $rel->_abs);
443 }
444  
445 34 for ($path, $bpath) {
446 68 ▶ $_ = "/$_" unless m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless m[^/] 0 68 68
447 }
448  
449 # Make it relative by eliminating:
450 # the scheme,
451 34 $rel->scheme(undef);
452  
453 # ... and authority
454 34 $rel->host(undef);
455 34 $rel->port(undef);
456 34 $rel->user(undef);
457
458
459 34 my @rparts = split('/', $path);
460 34 my @bparts = split('/', $bpath);
461 34 shift(@rparts);
462 34 shift(@bparts);
463 34 ▶ if (scalar(@rparts) and (scalar(@bparts) and $rparts[0] ne $bparts[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rparts and (scalar @bparts and $rparts[0] ne $bparts[0])) { } 2 32 34
Condition100%6/6
scalar @rparts and (scalar @bparts and $rparts[0] ne $bparts[0])
!ll&&!rl&&r
7252
scalar @bparts and $rparts[0] ne $bparts[0]
!ll&&!rl&&r
3222
MC/DC100%3/3
scalar @rparts and (scalar @bparts and $rparts[0] ne $bparts[0])
scalar @rparts scalar @bparts $rparts[0] ne $bparts[0]
Truth table100%4/4
A and B and C
A= scalar @rpartsB= scalar @bpartsC= $rparts[0] ne $bparts[0]
ABCresult
0XX0
10X0
1100
1111
464 # use an absolute path, because $rel differs from $base at the very beginning
465 } else {
466 # This loop is based on code from Nicolai Langfeldt <janl@ifi.uio.no>.
467 # First we calculate common initial path components length ($li).
468 32 my $li = 1;
469 32 while (1) {
470 65 my $i = index($path, '/', $li);
471 65 ▶ last if $i < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 or $i != index($bpath, "/", $li) or substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li) 32 33 65
Condition83%5/6
$i < 0 or $i != index($bpath, "/", $li) or substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li)
l!l&&r!l&&!r
32033
$i < 0 or $i != index($bpath, "/", $li)
l!l&&r!l&&!r
27533
MC/DC66%2/3
$i < 0 or $i != index($bpath, "/", $li) or substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li)
$i < 0 $i != index($bpath, "/", $li) substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li)
Truth table75%3/4
A or B or C
A= $i < 0B= $i != index($bpath, "/", $li)C= substr($path, $li, $i - $li) ne substr($bpath, $li, $i - $li)
ABCresult
1XX1
01X1
0011
0000
472 $i != index($bpath, '/', $li) ||
473 substr($path,$li,$i-$li) ne substr($bpath,$li,$i-$li);
474 33 $li=$i+1;
475 }
476
477 # then we nuke it from both paths
478 32 substr($path, 0,$li) = '';
479 32 substr($bpath,0,$li) = '';
480  
481  
482 32 ▶ if ($path eq $bpath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $bpath) { } 8 24 32
483 8 $rel->path('');
484 8 ▶ if (defined($rel->query) and defined($base->query)) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $rel->query and defined $base->query) { } 3 5 8
elsif (defined $rel->query) { } 1 4 5
elsif (defined $base->query) { } 1 3 4
Condition100%3/3
defined $rel->query and defined $base->query
!ll&&!rl&&r
413
MC/DC100%2/2
defined $rel->query and defined $base->query
defined $rel->query defined $base->query
Truth table100%3/3
A and B
A= defined $rel->queryB= defined $base->query
ABresult
0X0
100
111
485 3 ▶ if ($rel->query eq $base->query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->query eq $base->query) { } 2 1 3
486 2 $rel->query(undef);
487 } else {
488 #
489 }
490 } elsif (defined($rel->query)) {
491 #
492 } elsif (defined($base->query)) {
493 1 $rel->path($path);
494 } else {
495 #
496 }
497 } else {
498 # Add one "../" for each path component left in the base path
499 24 $path = ('../' x $bpath =~ tr|/|/|) . $path;
500 24 ▶ $path = "./" if $path eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 1 23 24
501 24 $rel->path($path);
502 }
503 }
504 34 return IRI->new(value => $rel->_abs);
505 }
506  
507 41 ▶ sub as_string {
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
508 41 my $self = shift;
509 41 ▶ if ($self->has_base || $self->has_resolved_components) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_base or $self->has_resolved_components) { } 3 38 41
Condition100%3/3
$self->has_base or $self->has_resolved_components
l!l&&r!l&&!r
1238
MC/DC100%2/2
$self->has_base or $self->has_resolved_components
$self->has_base $self->has_resolved_components
Truth table100%3/3
A or B
A= $self->has_baseB= $self->has_resolved_components
ABresult
1X1
011
000
510 3 return $self->abs;
511 } else {
512 38 return $self->value;
513 }
514 }
515
516 132 ▶ sub _string_from_components {
Subroutine _string_from_components: called CC=12 SCAR=24.9
517 132 my $self = shift;
518 132 my $components = shift;
519 132 my $iri = "";
520 132 ▶ if (my $s = $components->{scheme}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $components->{"scheme"}) 84 48 132
521 84 $iri .= "${s}:";
522 }
523
524 132 ▶ if ($components->{user} or $components->{port} or defined($components->{host})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($components->{"user"} or $components->{"port"} or defined $components->{"host"}) 90 42 132
Condition100%6/6
$components->{"user"} or $components->{"port"} or defined $components->{"host"}
l!l&&r!l&&!r
108042
$components->{"user"} or $components->{"port"}
l!l&&r!l&&!r
19122
MC/DC100%3/3
$components->{"user"} or $components->{"port"} or defined $components->{"host"}
$components->{"user"} $components->{"port"} defined $components->{"host"}
Truth table100%4/4
A or B or C
A= $components->{"user"}B= $components->{"port"}C= defined $components->{"host"}
ABCresult
1XX1
01X1
0011
0000
525 # has authority
526 90 $iri .= "//";
527 90 ▶ if (my $u = $components->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $u = $components->{"user"}) 1 89 90
528 1 $iri .= sprintf('%s@', $u);
529 }
530 90 ▶ if (defined(my $h = $components->{host})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $h = $components->{"host"})) 90 0 90
531 90 ▶ $iri .= $h // '';
Condition50%1/2
$h // ""
l!l
900
MC/DC0%0/1
$h // ""
$h
Truth table50%1/2
A
A= $h
Aresult
11
00
532 }
533 90 ▶ if (my $p = $components->{port}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $components->{"port"}) 10 80 90
534 10 $iri .= ":$p";
535 }
536 }
537
538 132 ▶ if (defined(my $p = $components->{path})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $p = $components->{"path"})) 132 0 132
539 132 $iri .= $p;
540 }
541
542 132 ▶ if (defined(my $q = $components->{query})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $q = $components->{"query"})) 26 106 132
543 26 $iri .= '?' . $q;
544 }
545
546 132 ▶ if (defined(my $f = $components->{fragment})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = $components->{"fragment"})) 38 94 132
547 38 $iri .= '#' . $f;
548 }
549
550 132 return $iri;
551 }
552
553 0 ▶ sub _encode {
Subroutine _encode: not called CC=1 SCAR=6.9
554 0 my $str = shift;
555 0 $str =~ s~([%])~'%' . sprintf('%02x', ord($1))~ge; # gen-delims
556 0 $str =~ s~([/:?#@]|\[|\])~'%' . sprintf('%02x', ord($1))~ge; # gen-delims
557 0 $str =~ s~([$!&'()*+,;=])~'%' . sprintf('%02x', ord($1))~ge; # sub-delims
558 0 return $str;
559 }
560
561 0 ▶ sub _unencode {
Subroutine _unencode: not called CC=2 SCAR=17.9
562 0 my $str = shift;
563 0 ▶ if (defined($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 0 0 0
564 0 $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
565 }
566 0 return $str;
567 }
568
569 =item C<< is_absolute >>
570  
571 Returns true if the IRI is absolute, false otherwise.
572  
573 =cut
574  
575 2 ▶ sub is_absolute {
Subroutine is_absolute: called CC=1 SCAR=0.0
Pod documented
576 2 my $self = shift;
577 2 return exists $self->components->{'scheme'};
578 }
579
580 =item C<< query_form >>
581  
582 Returns a HASH of key-value mappings for the unencoded, parsed query form data.
583  
584 =cut
585  
586 0 ▶ sub query_form {
Subroutine query_form: not called CC=2 SCAR=17.9
Pod documented
587 0 my $self = shift;
588 0 ▶ my $q = $self->query // return;
Condition0%0/2
$self->query // (return)
l!l
00
MC/DC0%0/1
$self->query // (return)
$self->query
Truth table0%0/2
A
A= $self->query
Aresult
11
00
589 0 my @pairs = split(/&/, $q);
590 0 return map { _unencode($_) } map { split(/=/, $_) } @pairs;
591 }
592  
593 =item C<< set_query_param ( $key => $value ) >>
594  
595 sets the respective query form value and returns a new L<IRI> object.
596  
597 =cut
598  
599 0 ▶ sub set_query_param {
Subroutine set_query_param: not called CC=3 SCAR=24.8
Pod documented
600 0 my $self = shift;
601 0 ▶ my $q = $self->query // return;
Condition0%0/2
$self->query // (return)
l!l
00
MC/DC0%0/1
$self->query // (return)
$self->query
Truth table0%0/2
A
A= $self->query
Aresult
11
00
602 0 my %map = map { _unencode($_) } map { split(/=/, $_) } split(/&/, $q);
603 0 ▶ while (my ($k, $v) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
604 0 $map{$k} = $v;
605 }
606
607 0 my %c = %{ $self->components };
608 0 my @pairs = map { join('=', (_encode($_), _encode($map{$_}))) } keys %map;
609 0 warn Dumper(\@pairs);
610 0 $c{query} = join('&', @pairs);
611
612 0 my $v = $self->_string_from_components(\%c);
613 0 return $self->new( value => $v );
614 }
615 }
616  
617 5 1;
618  
619 __END__
↑ Summary