Mojo/Parameters.pm

filter: Statementstmt 100% 109 / 109 Branchbran 98.3% 61 / 62 Conditioncond 78.5% 11 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 18 / 18 Podpod 100% 12 / 12 totaltotal 95.9% 214 / 223 CC 43 SCAR 18.2
CC
43
Cov
98%
pairs
13.1
to_string
8.0
append
7.0
CRAP 6.2
« Mojolicious/Routes.pm ↑ Summary Mojo/Home.pm »
1 package Mojo::Parameters;
2 33 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use overload '@{}' => sub { shift->pairs }, bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4  
5 33 ▶use Mojo::Util qw(decode encode url_escape url_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 33 has charset => 'UTF-8';
8  
9 144 ▶sub append {
Subroutine append: called CC=7 SCAR=19.5
Pod documented
10 144 my $self = shift;
11  
12 144 my $old = $self->pairs;
13 144 ▶ my @new = @_ == 1 ? @{shift->pairs} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 47 97 144
14 144 ▶ while (my ($name, $value) = splice @new, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@new, 0, 2)) 137 144 281
15  
16 # Multiple values
17 137 ▶ if (ref $value eq 'ARRAY') { push @$old, $name => $_ // '' for @$value }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 36 101 137
elsif (defined $value) { } 99 2 101
Condition50%1/2
$_ // ""
l!l
540
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
18  
19 # Single value
20 99 elsif (defined $value) { push @$old, $name => $value }
21 }
22  
23 144 return $self;
24 }
25  
26 163 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
27 163 my $self = shift;
28  
29 163 my $clone = $self->new;
30 163 ▶ if (exists $self->{charset}) { $clone->{charset} = $self->{charset} }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"charset"}) 118 45 163
31 163 ▶ if (defined $self->{string}) { $clone->{string} = $self->{string} }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'string'}) { } 52 111 163
32 111 else { $clone->{pairs} = [@{$self->pairs}] }
33  
34 163 return $clone;
35 }
36  
37 518 ▶sub every_param {
Subroutine every_param: called CC=3 SCAR=11.0
Pod documented
38 518 my ($self, $name) = @_;
39  
40 518 my @values;
41 518 my $pairs = $self->pairs;
42 518 ▶ for (my $i = 0; $i < @$pairs; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pairs) 372 518 890
43 372 ▶ push @values, $pairs->[$i + 1] if $pairs->[$i] eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $pairs->[$i] eq $name 114 258 372
44 }
45  
46 518 return \@values;
47 }
48  
49 14 ▶sub merge {
Subroutine merge: called CC=4 SCAR=13.9
Pod documented
50 14 my $self = shift;
51  
52 14 ▶ my $merge = @_ == 1 ? shift->to_hash : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 5 9 14
53 14 for my $name (sort keys %$merge) {
54 19 my $value = $merge->{$name};
55 19 ▶ defined $value ? $self->param($name => $value) : $self->remove($name);
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 15 4 19
56 }
57  
58 14 return $self;
59 }
60  
61 43 ▶sub names { [sort keys %{shift->to_hash}] }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
62  
63 972 ▶sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new }
Subroutine new: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 45 927 972
64  
65 1807 ▶sub pairs {
Subroutine pairs: called CC=13 SCAR=25.7
Pod documented
66 1807 my $self = shift;
67  
68 # Replace parameters
69 1807 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 1803 1807
70 4 $self->{pairs} = shift;
71 4 delete $self->{string};
72 4 return $self;
73 }
74  
75 # Parse string
76 1803 ▶ if (defined(my $str = delete $self->{string})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $str = delete $self->{"string"})) 54 1749 1803
77 54 my $pairs = $self->{pairs} = [];
78 54 ▶ return $pairs unless length $str;
Branch: 1 branch
BranchTrueFalseTotal
unless length $str 1 53 54
79  
80 53 my $charset = $self->charset;
81 53 for my $pair (split /&/, $str) {
82 99 ▶ next unless $pair =~ /^([^=]+)(?:=(.*))?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pair =~ /^([^=]+)(?:=(.*))?$/u 0 99 99
83 99 ▶ my ($name, $value) = ($1, $2 // '');
Condition100%2/2
$2 // ""
l!l
945
MC/DC100%1/1
$2 // ""
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
84  
85 # Replace "+" with whitespace, unescape and decode
86 99 s/\+/ /g for $name, $value;
87 99 $name = url_unescape $name;
88 99 ▶ $name = decode($charset, $name) // $name if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 96 3 99
Condition66%2/3
decode($charset, $name) // $name
l!l&&r!l&&!r
9420
MC/DC0%0/2
decode($charset, $name) // $name
decode($charset, $name) $name
Truth table66%2/3
A // B
A= decode($charset, $name)B= $name
ABresult
1X1
011
000
89 99 $value = url_unescape $value;
90 99 ▶ $value = decode($charset, $value) // $value if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 96 3 99
Condition66%2/3
decode($charset, $value) // $value
l!l&&r!l&&!r
9330
MC/DC0%0/2
decode($charset, $value) // $value
decode($charset, $value) $value
Truth table66%2/3
A // B
A= decode($charset, $value)B= $value
ABresult
1X1
011
000
91  
92 99 push @$pairs, $name, $value;
93 }
94 }
95  
96 1802 ▶ return $self->{pairs} //= [];
Condition100%2/2
$self->{"pairs"} //= []
l!l
1121681
MC/DC100%1/1
$self->{"pairs"} //= []
$self->{"pairs"}
Truth table100%2/2
A
A= $self->{"pairs"}
Aresult
11
00
97 }
98  
99 474 ▶sub param {
Subroutine param: called CC=3 SCAR=11.0
Pod documented
100 474 my ($self, $name) = (shift, shift);
101 474 ▶ return $self->every_param($name)->[-1] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 449 25 474
102 25 $self->remove($name);
103 25 ▶ return $self->append($name => ref $_[0] eq 'ARRAY' ? $_[0] : [@_]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 7 18 25
104 }
105  
106 154 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
107 154 my $self = shift;
108  
109 # Pairs
110 154 ▶ return $self->append(@_) if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 22 132 154
111  
112 # String
113 132 ▶ $self->{string} = shift // '';
Condition100%2/2
shift() // ""
l!l
1311
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
114 132 return $self;
115 }
116  
117 31 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
118 31 my ($self, $name) = @_;
119 31 my $pairs = $self->pairs;
120 31 my $i = 0;
121 31 ▶ $pairs->[$i] eq $name ? splice @$pairs, $i, 2 : ($i += 2) while $i < @$pairs;
Branch: 2 branches
BranchTrueFalseTotal
$i < @$pairs and do { $pairs->[$i] eq $name ? splice(@$pairs, $i, 2) : ($i += 2) } 102 31 133
$pairs->[$i] eq $name ? : 15 87 102
122 31 return $self;
123 }
124  
125 111 ▶sub to_hash {
Subroutine to_hash: called CC=4 SCAR=13.9
Pod documented
126 111 my $self = shift;
127  
128 111 my %hash;
129 111 my $pairs = $self->pairs;
130 111 ▶ for (my $i = 0; $i < @$pairs; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pairs) 128 111 239
131 128 my ($name, $value) = @{$pairs}[$i, $i + 1];
132  
133 # Array
134 128 ▶ if (exists $hash{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash{$name}) { } 29 99 128
135 29 ▶ $hash{$name} = [$hash{$name}] if ref $hash{$name} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash{$name} ne "ARRAY" 25 4 29
136 29 push @{$hash{$name}}, $value;
137 }
138  
139 # String
140 99 else { $hash{$name} = $value }
141 }
142  
143 111 return \%hash;
144 }
145  
146 970 ▶sub to_string {
Subroutine to_string: called CC=8 SCAR=20.8
Pod documented
147 970 my $self = shift;
148  
149 # String (RFC 3986)
150 970 my $charset = $self->charset;
151 970 ▶ if (defined(my $str = $self->{string})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $str = $self->{"string"})) 143 827 970
152 143 ▶ $str = encode $charset, $str if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 142 1 143
153 143 return url_escape $str, '^A-Za-z0-9\-._~%!$&\'()*+,;=:@/?';
154 }
155  
156 # Build pairs (HTML Living Standard)
157 827 my $pairs = $self->pairs;
158 827 ▶ return '' unless @$pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$pairs 768 59 827
159 59 my @pairs;
160 59 ▶ for (my $i = 0; $i < @$pairs; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pairs) 147 59 206
161 147 my ($name, $value) = @{$pairs}[$i, $i + 1];
162  
163 # Escape and replace whitespace with "+"
164 147 ▶ $name = encode $charset, $name if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 143 4 147
165 147 $name = url_escape $name, '^*\-.0-9A-Z_a-z';
166 147 ▶ $value = encode $charset, $value if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 143 4 147
167 147 $value = url_escape $value, '^*\-.0-9A-Z_a-z';
168 147 s/\%20/\+/g for $name, $value;
169  
170 147 push @pairs, "$name=$value";
171 }
172  
173 59 return join '&', @pairs;
174 }
175  
176 33 1;
177  
178 =encoding utf8
179  
180 =head1 NAME
181  
182 Mojo::Parameters - Parameters
183  
184 =head1 SYNOPSIS
185  
186 use Mojo::Parameters;
187  
188 # Parse
189 my $params = Mojo::Parameters->new('foo=bar&baz=23');
190 say $params->param('baz');
191  
192 # Build
193 my $params = Mojo::Parameters->new(foo => 'bar', baz => 23);
194 push @$params, i => '♥ mojolicious';
195 say "$params";
196  
197 =head1 DESCRIPTION
198  
199 L<Mojo::Parameters> is a container for form parameters used by L<Mojo::URL>, based on L<RFC
200 3986|https://tools.ietf.org/html/rfc3986> and the L<HTML Living Standard|https://html.spec.whatwg.org>.
201  
202 =head1 ATTRIBUTES
203  
204 L<Mojo::Parameters> implements the following attributes.
205  
206 =head2 charset
207  
208 my $charset = $params->charset;
209 $params = $params->charset('UTF-8');
210  
211 Charset used for encoding and decoding parameters, defaults to C<UTF-8>.
212  
213 # Disable encoding and decoding
214 $params->charset(undef);
215  
216 =head1 METHODS
217  
218 L<Mojo::Parameters> inherits all methods from L<Mojo::Base> and implements the following new ones.
219  
220 =head2 append
221  
222 $params = $params->append(foo => 'ba&r');
223 $params = $params->append(foo => ['ba&r', 'baz']);
224 $params = $params->append(foo => ['bar', 'baz'], bar => 23);
225 $params = $params->append(Mojo::Parameters->new);
226  
227 Append parameters. Note that this method will normalize the parameters.
228  
229 # "foo=bar&foo=baz"
230 Mojo::Parameters->new('foo=bar')->append(Mojo::Parameters->new('foo=baz'));
231  
232 # "foo=bar&foo=baz"
233 Mojo::Parameters->new('foo=bar')->append(foo => 'baz');
234  
235 # "foo=bar&foo=baz&foo=yada"
236 Mojo::Parameters->new('foo=bar')->append(foo => ['baz', 'yada']);
237  
238 # "foo=bar&foo=baz&foo=yada&bar=23"
239 Mojo::Parameters->new('foo=bar')->append(foo => ['baz', 'yada'], bar => 23);
240  
241 =head2 clone
242  
243 my $params2 = $params->clone;
244  
245 Return a new L<Mojo::Parameters> object cloned from these parameters.
246  
247 =head2 every_param
248  
249 my $values = $params->every_param('foo');
250  
251 Similar to L</"param">, but returns all values sharing the same name as an array reference. Note that this method will
252 normalize the parameters.
253  
254 # Get first value
255 say $params->every_param('foo')->[0];
256  
257 =head2 merge
258  
259 $params = $params->merge(foo => 'ba&r');
260 $params = $params->merge(foo => ['ba&r', 'baz']);
261 $params = $params->merge(foo => ['bar', 'baz'], bar => 23);
262 $params = $params->merge(Mojo::Parameters->new);
263  
264 Merge parameters. Note that this method will normalize the parameters.
265  
266 # "foo=baz"
267 Mojo::Parameters->new('foo=bar')->merge(Mojo::Parameters->new('foo=baz'));
268  
269 # "yada=yada&foo=baz"
270 Mojo::Parameters->new('foo=bar&yada=yada')->merge(foo => 'baz');
271  
272 # "yada=yada"
273 Mojo::Parameters->new('foo=bar&yada=yada')->merge(foo => undef);
274  
275 =head2 names
276  
277 my $names = $params->names;
278  
279 Return an array reference with all parameter names.
280  
281 # Names of all parameters
282 say for @{$params->names};
283  
284 =head2 new
285  
286 my $params = Mojo::Parameters->new;
287 my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23');
288 my $params = Mojo::Parameters->new(foo => 'b&ar');
289 my $params = Mojo::Parameters->new(foo => ['ba&r', 'baz']);
290 my $params = Mojo::Parameters->new(foo => ['bar', 'baz'], bar => 23);
291  
292 Construct a new L<Mojo::Parameters> object and L</"parse"> parameters if necessary.
293  
294 =head2 pairs
295  
296 my $array = $params->pairs;
297 $params = $params->pairs([foo => 'b&ar', baz => 23]);
298  
299 Parsed parameter pairs. Note that this method will normalize the parameters.
300  
301 # Remove all parameters
302 $params->pairs([]);
303  
304 =head2 param
305  
306 my $value = $params->param('foo');
307 $params = $params->param(foo => 'ba&r');
308 $params = $params->param(foo => qw(ba&r baz));
309 $params = $params->param(foo => ['ba;r', 'baz']);
310  
311 Access parameter values. If there are multiple values sharing the same name, and you want to access more than just the
312 last one, you can use L</"every_param">. Note that this method will normalize the parameters.
313  
314 =head2 parse
315  
316 $params = $params->parse('foo=b%3Bar&baz=23');
317  
318 Parse parameters.
319  
320 =head2 remove
321  
322 $params = $params->remove('foo');
323  
324 Remove parameters. Note that this method will normalize the parameters.
325  
326 # "bar=yada"
327 Mojo::Parameters->new('foo=bar&foo=baz&bar=yada')->remove('foo');
328  
329 =head2 to_hash
330  
331 my $hash = $params->to_hash;
332  
333 Turn parameters into a hash reference. Note that this method will normalize the parameters.
334  
335 # "baz"
336 Mojo::Parameters->new('foo=bar&foo=baz')->to_hash->{foo}[1];
337  
338 =head2 to_string
339  
340 my $str = $params->to_string;
341  
342 Turn parameters into a string.
343  
344 # "foo=bar&baz=23"
345 Mojo::Parameters->new->pairs([foo => 'bar', baz => 23])->to_string;
346  
347 =head1 OPERATORS
348  
349 L<Mojo::Parameters> overloads the following operators.
350  
351 =head2 array
352  
353 my @pairs = @$params;
354  
355 Alias for L</"pairs">. Note that this will normalize the parameters.
356  
357 say $params->[0];
358 say for @$params;
359  
360 =head2 bool
361  
362 my $bool = !!$params;
363  
364 Always true.
365  
366 =head2 stringify
367  
368 my $str = "$params";
369  
370 Alias for L</"to_string">.
371  
372 =head1 SEE ALSO
373  
374 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
375  
376 =cut
« Mojolicious/Routes.pm ↑ Summary Mojo/Home.pm »