X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FRequest.pm;h=b16e59ab155434b63677247e0a72a39838bdde39;hp=429cadbf67dab3e1ef4b1ee7b7fae917687705d0;hb=a375a2063f27382237250ad957bea5ea6ead9296;hpb=723409756875e4156d215c7348faa4b2804ed389 diff --git a/lib/Catalyst/Request.pm b/lib/Catalyst/Request.pm index 429cadb..b16e59a 100644 --- a/lib/Catalyst/Request.pm +++ b/lib/Catalyst/Request.pm @@ -571,19 +571,35 @@ L objects. Returns a URI object for the current request. Stringifies to the URI text. -=head2 $req->uri_with( { key => 'value' } ); +=head2 $req->mangle_params( { key => 'value' }, $appendmode); -Returns a rewritten URI object for the current request. Key/value pairs -passed in will override existing parameters. You can remove an existing -parameter by passing in an undef value. Unmodified pairs will be -preserved. +Returns a hashref of parameters stemming from the current request's params, +plus the ones supplied. Keys for which no current param exists will be +added, keys with undefined values will be removed and keys with existing +params will be replaced. Note that you can supply a true value as the final +argument to change behavior with regards to existing parameters, appending +values rather than replacing them. + +A quick example: + + # URI query params foo=1 + my $hashref = $req->mangle_params({ foo => 2 }); + # Result is query params of foo=2 + +versus append mode: + + # URI query params foo=1 + my $hashref = $req->mangle_params({ foo => 2 }, 1); + # Result is query params of foo=1&foo=2 + +This is the code behind C. =cut -sub uri_with { - my( $self, $args ) = @_; +sub mangle_params { + my ($self, $args, $append) = @_; - carp( 'No arguments passed to uri_with()' ) unless $args; + carp('No arguments passed to mangle_params()') unless $args; foreach my $value ( values %$args ) { next unless defined $value; @@ -593,13 +609,66 @@ sub uri_with { } }; - my $uri = $self->uri->clone; - my %query = ( %{ $uri->query_form_hash }, %$args ); + my %params = %{ $self->uri->query_form_hash }; + foreach my $key (keys %{ $args }) { + my $val = $args->{$key}; + if(defined($val)) { + + if($append && exists($params{$key})) { + + # This little bit of heaven handles appending a new value onto + # an existing one regardless if the existing value is an array + # or not, and regardless if the new value is an array or not + $params{$key} = [ + ref($params{$key}) eq 'ARRAY' ? @{ $params{$key} } : $params{$key}, + ref($val) eq 'ARRAY' ? @{ $val } : $val + ]; + + } else { + $params{$key} = $val; + } + } else { + + # If the param wasn't defined then we delete it. + delete($params{$key}); + } + } + + + return \%params; +} + +=head2 $req->uri_with( { key => 'value' } ); + +Returns a rewritten URI object for the current request. Key/value pairs +passed in will override existing parameters. You can remove an existing +parameter by passing in an undef value. Unmodified pairs will be +preserved. + +You may also pass an optional second parameter that puts C into +append mode: + + $req->uri_with( { key => 'value' }, { mode => 'append' } ); + +See C for an explanation of this behavior. + +=cut + +sub uri_with { + my( $self, $args, $behavior) = @_; + + carp( 'No arguments passed to uri_with()' ) unless $args; + + my $append = 0; + if((ref($behavior) eq 'HASH') && defined($behavior->{mode}) && ($behavior->{mode} eq 'append')) { + $append = 1; + } + + my $params = $self->mangle_params($args, $append); + + my $uri = $self->uri->clone; + $uri->query_form($params); - $uri->query_form( { - # remove undef values - map { defined $query{ $_ } ? ( $_ => $query{ $_ } ) : () } keys %query - } ); return $uri; }