GX/HTTP/Parameters.pm

filter: Statementstmt 92.8% 129 / 139 Branchbran 80.6% 50 / 62 Conditioncond 63.1% 12 / 19 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 90.0% 18 / 20 Podpod 100% 11 / 11 totaltotal 85.5% 224 / 262 CC 46 SCAR 19.3
CC
46
Cov
87%
parse
13.4
as_string
10.0
decode
7.0
CRAP 6.9
« GX/Route.pm ↑ Summary GX/Dispatcher.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/HTTP/Parameters.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::HTTP::Parameters;
7  
8 38 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use GX::HTTP::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 38 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 # ----------------------------------------------------------------------------------------------------------------------
16 # Class setup
17 # ----------------------------------------------------------------------------------------------------------------------
18  
19 38 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 38 has 'data' => (
22 isa => 'Hash::Ordered',
23 accessors => {
24 '_get_data' => { type => 'get_reference' }
25 }
26 );
27  
28 38 has 'encoding' => (
29 isa => 'Scalar'
30 );
31  
32 38 build;
33  
34  
35 # ----------------------------------------------------------------------------------------------------------------------
36 # Overloading
37 # ----------------------------------------------------------------------------------------------------------------------
38  
39 use overload
40 668 ▶ 'bool' => sub { 1 },
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine __ANON__: called CC=1 SCAR=0.0
41 0 ▶ '0+' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 ▶ '""' => sub { $_[0]->as_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 38 'fallback' => 1;
44  
45  
46 # ----------------------------------------------------------------------------------------------------------------------
47 # Public methods
48 # ----------------------------------------------------------------------------------------------------------------------
49  
50 1070 ▶sub add {
Subroutine add: called CC=4 SCAR=14.6
Pod documented
51  
52 1070 my $self = shift;
53 1070 my $key = shift;
54  
55 1070 ▶ if ( ! defined $key || ! length $key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and length $key) 0 1070 1070
Condition33%1/3
defined $key and length $key
!ll&&!rl&&r
001070
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table33%1/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
56 0 complain "Invalid parameter name";
57 }
58  
59 1070 ▶ push @{$self->_get_data->{$key}}, map { defined $_ ? "$_" : undef } @_;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1082 14 1096
60  
61 1070 return;
62  
63 }
64  
65 4 ▶sub as_string {
Subroutine as_string: called CC=10 SCAR=23.0
Pod documented
66  
67 4 my $self = shift;
68  
69 4 my $data = $self->_get_data;
70  
71 4 my @pairs;
72  
73 4 ▶ if ( defined( my $encoding = $self->encoding ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $encoding = $self->encoding)) { } 1 3 4
74  
75 1 my $encoder = Encode::find_encoding( $encoding );
76  
77 1 ▶ if ( ! $encoder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($encoder) 0 1 1
78 0 complain "Cannot encode parameters (unsupported encoding)";
79 }
80  
81 1 eval {
82  
83 1 ▶ while ( my ( $key, $values ) = each %$data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 4 1 5
84  
85 4 my $encoded_key = GX::HTTP::Util::url_encode( $encoder->encode( $key, Encode::FB_CROAK ) );
86  
87 4 for ( @$values ) {
88 7 ▶ push @pairs, $encoded_key . '=' . (
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 5 2 7
89 defined $_
90 ? GX::HTTP::Util::url_encode( $encoder->encode( $_, Encode::FB_CROAK ) )
91 : ''
92 );
93 }
94  
95 }
96  
97 };
98  
99 }
100 else {
101  
102 3 eval {
103  
104 3 ▶ while ( my ( $key, $values ) = each %$data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 6 2 8
105  
106 6 my $encoded_key = GX::HTTP::Util::url_encode( $key );
107  
108 5 for ( @$values ) {
109 8 ▶ push @pairs, $encoded_key . '=' . ( defined $_ ? GX::HTTP::Util::url_encode( $_ ) : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 7 1 8
110 }
111  
112 }
113  
114 };
115  
116 }
117  
118 4 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 3 4
119 1 GX::Exception->complain(
120 message => "Cannot encode parameters",
121 subexception => $@
122 );
123 }
124  
125 3 return join( '&', @pairs );
126  
127 }
128  
129 4 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
130  
131 4 return scalar keys %{$_[0]->_get_data};
132  
133 }
134  
135 4 ▶sub decode {
Subroutine decode: called CC=7 SCAR=19.5
Pod documented
136  
137 4 my $self = shift;
138 4 my $encoding = shift;
139  
140 4 ▶ $encoding //= $self->encoding // 'utf8';
Condition100%4/4
$encoding //= $self->encoding // "utf8"
l!l
13
$self->encoding // "utf8"
l!l
12
MC/DC100%1/1
$encoding //= $self->encoding // "utf8"
$encoding
Truth table100%2/2
A
A= $encoding
Aresult
11
00
141  
142 4 my $decoder = Encode::find_encoding( $encoding );
143  
144 4 ▶ if ( ! $decoder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($decoder) 0 4 4
145 0 complain "Cannot decode parameters (unsupported encoding)";
146 }
147  
148 4 my $data = $self->_get_data;
149  
150 4 my @data;
151  
152 4 eval {
153  
154 4 ▶ while ( my ( $key, $values ) = each %$data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 13 3 16
155  
156 push @data, [
157 $decoder->decode( $key, Encode::FB_CROAK ),
158 13 ▶ [ map { defined $_ ? $decoder->decode( $_, Encode::FB_CROAK ) : undef } @$values ]
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 16 6 22
159 ];
160  
161 }
162  
163 };
164  
165 4 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 3 4
166 1 GX::Exception->complain(
167 message => "Cannot decode parameters",
168 subexception => $@
169 );
170 }
171  
172 3 %$data = map { $_->[0] => $_->[1] } @data;
173  
174 3 return;
175  
176 }
177  
178 5 ▶sub exists {
Subroutine exists: called CC=2 SCAR=7.0
Pod documented
179  
180 5 my $self = shift;
181 5 my $key = shift;
182  
183 5 ▶ return defined $key && exists $self->_get_data->{$key};
Condition66%2/3
defined $key && exists $self->_get_data->{$key}
!ll&&!rl&&r
032
MC/DC50%1/2
defined $key && exists $self->_get_data->{$key}
defined $key exists $self->_get_data->{$key}
Truth table66%2/3
A && B
A= defined $keyB= exists $self->_get_data->{$key}
ABresult
0X0
100
111
184  
185 }
186  
187 840 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod documented
188  
189 840 my $self = shift;
190 840 my $key = shift;
191  
192 840 ▶ defined $key or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $key or return 840 0 840
193  
194 840 ▶ my $values = $self->_get_data->{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $values = $self->_get_data->{$key} or return 815 25 840
195  
196 815 ▶ return wantarray ? @$values : $values->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 809 6 815
197  
198 }
199  
200 403 ▶sub keys {
Subroutine keys: called CC=1 SCAR=0.0
Pod documented
201  
202 403 return keys %{$_[0]->_get_data};
203  
204 }
205  
206 9 ▶sub merge {
Subroutine merge: called CC=6 SCAR=18.6
Pod documented
207  
208 9 my $invocant = shift;
209  
210 9 ▶ my $self = ref $invocant ? $invocant : $invocant->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 1 8 9
211 9 my $class = ref $self;
212  
213 9 my $data = $self->_get_data;
214  
215 9 for my $container ( @_ ) {
216  
217 29 ▶ if ( ! blessed $container || ! $container->isa( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($container) and $container->isa($class)) 0 29 29
Condition33%1/3
builtin::blessed($container) and $container->isa($class)
!ll&&!rl&&r
0029
MC/DC0%0/2
builtin::blessed($container) and $container->isa($class)
builtin::blessed($container) $container->isa($class)
Truth table33%1/3
A and B
A= builtin::blessed($container)B= $container->isa($class)
ABresult
0X0
100
111
218 0 complain "Invalid argument (arguments must be $class objects)";
219 }
220  
221 29 for my $key ( $container->keys ) {
222 39 push @{$data->{$key}}, $container->get( $key );
223 }
224  
225 }
226  
227 9 return $self;
228  
229 }
230  
231 18 ▶sub parse {
Subroutine parse: called CC=13 SCAR=26.0
Pod documented
232  
233 18 ▶ my $self = ref $_[0] ? shift : shift->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 15 18
234 18 my $string = shift;
235  
236 18 ▶ return $self unless defined $string && length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and length $string 5 13 18
Condition100%3/3
defined $string and length $string
!ll&&!rl&&r
4113
MC/DC100%2/2
defined $string and length $string
defined $string length $string
Truth table100%3/3
A and B
A= defined $stringB= length $string
ABresult
0X0
100
111
237  
238 13 ▶ if ( utf8::is_utf8( $string ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($string)) 0 13 13
239 0 complain "Invalid argument (argument must be a byte string)";
240 }
241  
242 13 my @pairs;
243  
244 13 for my $pair ( split( /&/, $string ) ) {
245  
246 75 my ( $key, $value ) = split( /=/, $pair, 2 );
247  
248 75 ▶ length $key or next;
Branch: 1 branch
BranchTrueFalseTotal
length $key or next 75 0 75
249  
250 75 $key =~ tr/+/ /;
251 75 $key =~ s/%([0-9a-fA-F]{2})/chr( hex( $1 ) )/eg;
252  
253 75 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 63 12 75
254 63 $value =~ tr/+/ /;
255 63 $value =~ s/%([0-9a-fA-F]{2})/chr( hex( $1 ) )/eg;
256 }
257 else {
258 12 $value = '';
259 }
260  
261 75 push @pairs, [ $key, $value ];
262  
263 }
264  
265 13 ▶ if ( defined( my $encoding = $self->encoding ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $encoding = $self->encoding)) 1 12 13
266  
267 1 my $decoder = Encode::find_encoding( $encoding );
268  
269 1 ▶ if ( ! $decoder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($decoder) 0 1 1
270 0 complain "Cannot decode parameters (unsupported encoding)";
271 }
272  
273 1 eval {
274  
275 1 for ( @pairs ) {
276 7 $_->[0] = $decoder->decode( $_->[0], Encode::FB_CROAK );
277 7 $_->[1] = $decoder->decode( $_->[1], Encode::FB_CROAK );
278 }
279  
280 };
281  
282 1 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
283 0 GX::Exception->complain(
284 message => "Cannot decode parameters",
285 subexception => $@
286 );
287 }
288  
289 }
290  
291 13 my $data = $self->_get_data;
292  
293 13 for ( @pairs ) {
294 75 push @{$data->{$_->[0]}}, $_->[1];
295 }
296  
297 13 return $self;
298  
299 }
300  
301 3 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.4
Pod documented
302  
303 3 my $self = shift;
304 3 my $key = shift;
305  
306 3 ▶ defined $key or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $key or return 3 0 3
307  
308 3 ▶ return delete $self->_get_data->{$key} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $self->_get_data->{$key} ? : 3 0 3
309  
310 }
311  
312 5 ▶sub set {
Subroutine set: called CC=5 SCAR=16.6
Pod documented
313  
314 5 my $self = shift;
315 5 my $key = shift;
316  
317 5 ▶ if ( ! defined $key || ! length $key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and length $key) 0 5 5
Condition33%1/3
defined $key and length $key
!ll&&!rl&&r
005
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table33%1/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
318 0 complain "Invalid parameter name";
319 }
320  
321 5 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 1 5
322 4 ▶ $self->_get_data->{$key} = [ map { defined $_ ? "$_" : undef } @_ ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 4 1 5
323 }
324 else {
325 1 delete $self->_get_data->{$key};
326 }
327  
328 5 return;
329  
330 }
331  
332  
333 38 1;
334  
335 __END__
« GX/Route.pm ↑ Summary GX/Dispatcher.pm »