File: FakeQuery.pm

package info (click to toggle)
libhtml-formfu-perl 0.09007-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,184 kB
  • sloc: perl: 13,186; makefile: 9; sql: 5
file content (72 lines) | stat: -rw-r--r-- 1,587 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package HTML::FormFu::FakeQuery;
use strict;
use Scalar::Util qw( reftype );
use Carp qw( croak );

sub new {
    my ( $class, $form, $param ) = @_;

    croak 'argument must be a hashref'
        if reftype( $param ) ne 'HASH';

    # handle pre-expanded input

    my @names
        = grep {defined}
        map    { $_->nested_name } @{ $form->get_fields };

    for my $name (@names) {
        next if exists $param->{$name};

        if ( $form->nested_hash_key_exists( $param, $name ) ) {
            $param->{$name} = $form->get_nested_hash_value( $param, $name );
        }
    }

    my $self = { _params => $param };

    return bless $self, $class;
}

sub param {
    my $self = shift;

    if ( !@_ ) {
        return keys %{ $self->{_params} };
    }
    elsif ( @_ == 2 ) {
        my ( $param, $value ) = @_;

        $self->{$param} = $value;
        return $self->{_params}{$param};
    }
    elsif ( @_ == 1 ) {
        my ($param) = @_;

        if ( !exists $self->{_params}{$param} ) {
            return wantarray ? () : undef;
        }

        if ( ref $self->{_params}{$param} eq 'ARRAY' ) {
            return (wantarray)
                ? @{ $self->{_params}{$param} }
                : $self->{_params}{$param}->[0];
        }
        else {
            return (wantarray)
                ? ( $self->{_params}{$param} )
                : $self->{_params}{$param};
        }
    }

    croak 'require arguments [$name, [$value]]';
}

# dummy methods, so FakeQuery doesn't cause fatal errors
# if a form is submitted

sub upload { }

sub uploadInfo { {} }

1;