File: Profile.pm

package info (click to toggle)
libformvalidator-simple-perl 0.29-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: perl: 3,043; makefile: 4
file content (114 lines) | stat: -rw-r--r-- 3,005 bytes parent folder | download | duplicates (5)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package FormValidator::Simple::Profile;
use strict;
use base qw/FormValidator::Simple::ArrayList/;
use FormValidator::Simple::Exception;
use FormValidator::Simple::Iterator;

sub _init {
    my($self, $prof) = @_;
    for (my $i = 0; $i <= $#{$prof}; $i += 2) {
        my ($key, $constraints) = ($prof->[$i], $prof->[$i + 1]);
        my $record = FormValidator::Simple::Profile::Record->new;
        $record->set_keys($key);
        $record->set_constraints($constraints);
        $self->append($record);
    }
}

sub iterator {
    my $self = shift;
    return FormValidator::Simple::Profile::Iterator->new($self);
}

package FormValidator::Simple::Profile::Record;
use base qw/Class::Accessor::Fast/;
use FormValidator::Simple::Exception;
use FormValidator::Simple::Constants;
use FormValidator::Simple::Constraints;
use FormValidator::Simple::Constraint;

__PACKAGE__->mk_accessors(qw/name keys constraints/);

sub new {
    my $class = shift;
    my $self  = bless { }, $class;
    $self->_init(@_);
    return $self;
}

sub _init {
    my $self = shift;
    $self->name( q{ } );
    $self->keys( [] );
    $self->constraints( FormValidator::Simple::Constraints->new );
}

sub set_keys {
    my ($self, $keys) = @_;
    if (ref $keys) {
        if (ref $keys eq 'HASH') {
            my ($name) = keys %$keys;
            my $params = $keys->{$name};
            $self->name($name);
            if(ref $params) {
                $self->keys( $params   );
            }
            else {
                $self->keys( [$params] );
            }
        }
        else {
            FormValidator::Simple::Exception->throw(
                qq/set keys of profile as hashref or single scalar./
            );
        }
    }
    else {
        $self->name( $keys   );
        $self->keys( [$keys] );
    }
}

sub set_constraints {
    my ($self, $constraints) = @_;
    $self->constraints( FormValidator::Simple::Constraints->new );
    if (ref $constraints) {

        if (ref $constraints eq 'ARRAY') {

            SETTING:
            foreach my $setting ( @$constraints ) {
                my $const = FormValidator::Simple::Constraint->new($setting);
                if ($const->name eq 'NOT_BLANK') {
                    $self->constraints->needs_blank_check( TRUE );
                    next SETTING;
                }
                else {
                    $self->constraints->append($const);
                }
            }

        }
        else {
            FormValidator::Simple::Exception->throw(
                qq/set constraints as arrayref or single scalar./
            );
        }
    }
    else {
        my $const = FormValidator::Simple::Constraint->new($constraints);
        if ($const->name eq 'NOT_BLANK') {
            $self->constraints->needs_blank_check( TRUE );
        }
        else {
            $self->constraints->append($const);
        }
    }
}

package FormValidator::Simple::Profile::Iterator;
use base qw/FormValidator::Simple::Iterator/;

1;
__END__