blib/lib/PHP/ParseStr.pm

filter: Statementstmt 100% 32 / 32 Branchbran 100% 4 / 4 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 97.9% 48 / 49 CC 6 SCAR 13.2
CC
6
Cov
100%
php_parse_str
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.7
↑ Summary
1 package PHP::ParseStr;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = "0.0.2";
8  
9 1 ▶use Exporter::Easy (
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 OK => [qw( php_parse_str )]
11 1 );
12  
13 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 PHP::ParseStr - Implements PHP's parse_str function
18  
19 =for markdown [![Build Status](https://travis-ci.org/abayliss/php-parsestr.svg?branch=master)](https://travis-ci.org/abayliss/php-parsestr)
20  
21 =head1 SYNOPSIS
22  
23 use PHP::ParseStr qw(php_parse_str);
24 my $hr = php_parse_str("stuff[0]=things&stuff[1]=otherthings&widgit[name]=thing&widgit[id]=123");
25  
26 =head1 DESCRIPTION
27  
28 A simple implementation of PHP's C<parse_str> function. The inverse of
29 C<http_build_query> (implemented by L<PHP::HTTPBuildQuery>).
30  
31 =head1 USAGE
32  
33 Pass your query string into C<php_parse_str> and get a hash ref back.
34  
35 my $hr = php_parse_str("stuff[0]=things&stuff[1]=otherthings&widgit[name]=thing&widgit[id]=123");
36 # {
37 # stuff => [ 'things', 'otherthings' ],
38 # widgit => {
39 # id => '123',
40 # name => 'thing'
41 # }
42 # }
43  
44 Note that unlike PHP's C<parse_str>, we return a hash ref, rather than
45 automagically creating variables in the passing scope, or filling a hash passed
46 in by reference. This is A Good Thing.
47  
48 =head1 BUGS / LIMITATIONS
49  
50 Currently I assume that anything where the "key" is numeric will be an array.
51 This will cause problems if you get structures with mixed numeric and
52 alphanumeric keys if a numeric one is encountered first.
53  
54 This module worked well enough for my purposes. YMMV. Patches welcome.
55  
56 =head1 SEE ALSO
57  
58 L<PHP::HTTPBuildQuery> does the inverse of this module.
59  
60 =head1 AUTHOR
61  
62 Andrew Bayliss <abayliss@gmail.com>
63  
64 =head1 COPYRIGHT
65  
66 This program is free software; you can redistribute
67 it and/or modify it under the same terms as Perl itself.
68  
69 The full text of the license can be found in the
70 LICENSE file included with this module.
71  
72 =cut
73  
74 # inspired by http://www.perlmonks.org/?node_id=872864
75 4 ▶sub php_parse_str {
Subroutine php_parse_str: called CC=6 SCAR=17.9
Pod undocumented
76 4 my ( $str ) = @_;
77  
78 4 my $u = URI->new;
79 4 $u->query($str);
80  
81 4 my %in = $u->query_form;
82 4 my $out = {};
83 4 ▶ while ( my ( $k, $v ) = each %in ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %in) 17 4 21
84 17 my $level = \$out;
85  
86 17 my @parts = $k =~ /([^\[\]]+)/g;
87 17 foreach (@parts) {
88 37 ▶ if ( $_ =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\d+$/) { } 12 25 37
89 12 ▶ $$level //= [];
Condition100%2/2
$$level //= []
l!l
48
MC/DC100%1/1
$$level //= []
$$level
Truth table100%2/2
A
A= $$level
Aresult
11
00
90 12 $level = \($$level->[$_]);
91 } else {
92 25 ▶ $$level //= {};
Condition100%2/2
$$level //= {}
l!l
178
MC/DC100%1/1
$$level //= {}
$$level
Truth table100%2/2
A
A= $$level
Aresult
11
00
93 25 $level = \($$level->{$_});
94 }
95 }
96 17 $$level = $v;
97 }
98  
99 4 return $out;
100 }
101  
102 1 return "Perfectly Horrible Poppycock";
↑ Summary