Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/Mojo/Parameters.pm
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,37 @@ sub to_hash {
return \%hash;
}

sub to_deep_hash {
my $self = shift;

my %hash;
my $pairs = $self->pairs;
for (my $i = 0; $i < @$pairs; $i += 2) {
my ($name, $value) = @{$pairs}[$i, $i + 1];
my @parts;
if ($name =~ m{^([^\[\]]+)((?:\[[^\[\]]+\])+)\z}) {
@parts = ($1);
push @parts, $2 =~ /\[([^\[\]]+)\]/g;
}
else { @parts = ($name) }
my $target = \%hash;
while (@parts > 1) {
my $part = shift @parts;
$target->{$part} = {} unless ref $target->{$part} eq 'HASH';
$target = $target->{$part};
}

my $part = $parts[-1];
if (exists $target->{$part}) {
$target->{$part} = [$target->{$part}] if ref $target->{$part} ne 'ARRAY';
push @{$target->{$part}}, $value;
}
else { $target->{$part} = $value }
}

return \%hash;
}

sub to_string {
my $self = shift;

Expand Down Expand Up @@ -335,6 +366,22 @@ Turn parameters into a hash reference. Note that this method will normalize the
# "baz"
Mojo::Parameters->new('foo=bar&foo=baz')->to_hash->{foo}[1];

=head2 to_deep_hash

my $hash = $params->to_deep_hash;

Turn bracket notation into nested hash references. Parameter names without valid, non-empty bracket segments remain
unchanged, and repeated values at the same path are represented as arrays.

# {foo => {bar => 'baz'}}
Mojo::Parameters->new('foo[bar]=baz')->to_deep_hash;

# {foo => {bar => {baz => 'yada'}}}
Mojo::Parameters->new('foo[bar][baz]=yada')->to_deep_hash;

Unlike L</"to_hash">, this method interprets bracket notation. Numeric bracket segments are hash keys, not array
indexes.

=head2 to_string

my $str = $params->to_string;
Expand Down
14 changes: 14 additions & 0 deletions t/mojo/parameters.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ subtest 'Basic functionality' => sub {
is $params->remove('a')->to_string, 'foo=b%3Bar&baz=23&b=6&b=7&c=f%3Boo', 'right format';
};

subtest 'Deep hash' => sub {
my $params = Mojo::Parameters->new('foo[bar]=baz&foo[bar]=yada&foo[baz][qux]=23&foo[0]=zero');
is_deeply $params->to_hash, {'foo[bar]' => ['baz', 'yada'], 'foo[baz][qux]' => 23, 'foo[0]' => 'zero'},
'right shallow structure';
is_deeply $params->to_deep_hash, {foo => {bar => ['baz', 'yada'], baz => {qux => 23}, 0 => 'zero'}},
'right deep structure';
$params = Mojo::Parameters->new(foo => 'bar');
is_deeply $params->to_deep_hash, {foo => 'bar'}, 'right shallow structure';
$params = Mojo::Parameters->new('foo%5Bbar%5D=baz');
is_deeply $params->to_deep_hash, {foo => {bar => 'baz'}}, 'right decoded structure';
$params = Mojo::Parameters->new('foo[]=baz&foo[bar=baz');
is_deeply $params->to_deep_hash, {'foo[]' => 'baz', 'foo[bar' => 'baz'}, 'right malformed structure';
};

subtest 'Clone' => sub {
my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23');
my $clone = $params->clone;
Expand Down
Loading