File Coverage

blib/lib/Input/Validator/Group.pm
Criterion Covered Total %
statement 43 45 95.5
branch 8 10 80.0
condition 2 4 50.0
subroutine 11 11 100.0
pod 6 7 85.7
total 70 77 90.9


line stmt bran cond sub pod time code
1             package Input::Validator::Group;
2              
3 7     7   892 use strict;
  7         10  
  7         246  
4 7     7   39 use warnings;
  7         13  
  7         217  
5              
6 7     7   34 use base 'Input::Validator::Base';
  7         15  
  7         1122  
7              
8 7     7   600 use Input::Validator::ConstraintBuilder;
  7         13  
  7         4311  
9              
10             sub BUILD {
11 4     4 0 6 my $self = shift;
12              
13 4   50     34 $self->{constraints} ||= [];
14 4   50     9 $self->{fields} ||= [];
15              
16 4         9 return $self;
17             }
18              
19             sub name {
20 4     4 1 5 my $self = shift;
21              
22 4 50       19 return $self->{name} unless @_;
23              
24 0         0 $self->{name} = $_[0];
25              
26 0         0 return $self;
27             }
28              
29             sub error {
30 22     22 1 115 my $self = shift;
31              
32 22 100       73 return $self->{error} unless @_;
33              
34 12         20 $self->{error} = $_[0];
35              
36 12         27 return $self;
37             }
38              
39 2     2 1 25 sub unique { shift->constraint('unique') }
40 2     2 1 5 sub equal { shift->constraint('equal') }
41              
42             sub constraint {
43 4     4 1 6 my $self = shift;
44              
45 4         28 my $constraint = Input::Validator::ConstraintBuilder->build(@_);
46              
47 4         6 push @{$self->{constraints}}, $constraint;
  4         9  
48              
49 4         9 return $self;
50             }
51              
52             sub is_valid {
53 10     10 1 14 my $self = shift;
54              
55             # Don't check if some field already has an error
56 10 100       14 return 0 if grep {$_->error} @{$self->{fields}};
  20         47  
  10         19  
57              
58             # Get all the values
59 8         14 my $values = [map { $_->value } @{$self->{fields}}];
  16         36  
  8         14  
60              
61 8         14 foreach my $c (@{$self->{constraints}}) {
  8         14  
62 8         24 my ($ok, $error) = $c->is_valid($values);
63              
64 8 100       22 unless ($ok) {
65 4 50       31 $self->error( $error ? $error : $c->error);
66 4         19 return 0;
67             }
68             }
69              
70 4         18 return 1;
71             }
72              
73             1;
74             __END__