QBit/Validator/Type/array.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 145 CC 18 SCAR 29.4
CC
18
Cov
0%
optional
42.0
all
20.0
contents
20.0
CRAP 18.9
1 package QBit::Validator::Type::array;
2 $QBit::Validator::Type::array::VERSION = '0.011';
3 use qbit;
4  
5 use base qw(QBit::Validator::Type);
6  
7 use Exception::Validator;
8  
9 #order is important
10 my $OPTIONS = [
11 {name => 'optional', required => TRUE},
12 {name => 'size_min'},
13 {name => 'size'},
14 {name => 'size_max'},
15 {name => 'all'},
16 {name => 'contents'},
17 ];
18  
19 sub _get_options {
20 my ($self) = @_;
21  
22 return clone($OPTIONS);
23 }
24  
25 sub _get_options_name {
26 return map {$_->{'name'}} @$OPTIONS;
27 }
28  
29 sub optional {
30 my ($self, $qv, $data, $template, $option, @path_field) = @_;
31  
32 if ($template->{$option}) {
33 if (defined($data)) {
34 unless (ref($data) eq 'ARRAY') {
35 $qv->_add_error($template, gettext('Data must be ARRAY'), \@path_field);
36  
37 return FALSE;
38 }
39 } else {
40 $qv->_add_ok(\@path_field);
41  
42 return FALSE;
43 }
44 } else {
45 if (!defined($data)) {
46 $qv->_add_error($template, gettext('Data must be defined'), \@path_field);
47  
48 return FALSE;
49 } else {
50 unless (ref($data) eq 'ARRAY') {
51 $qv->_add_error($template, gettext('Data must be ARRAY'), \@path_field);
52  
53 return FALSE;
54 }
55 }
56 }
57  
58 return TRUE;
59 }
60  
61 sub size_min {
62 my ($self, $qv, $data, $template, $option, @path_field) = @_;
63  
64 throw Exception::Validator gettext('Option "%s" must be positive number', $option)
65 if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/;
66  
67 if (@$data < $template->{$option}) {
68 $qv->_add_error($template, gettext('Data size "%s" less then "%s"', scalar(@$data), $template->{$option}),
69 \@path_field);
70  
71 return FALSE;
72 }
73  
74 return TRUE;
75 }
76  
77 sub size {
78 my ($self, $qv, $data, $template, $option, @path_field) = @_;
79  
80 throw Exception::Validator gettext('Key "%s" must be positive number', $option)
81 if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/;
82  
83 unless (@$data == $template->{$option}) {
84 $qv->_add_error($template, gettext('Data size "%s" not equal "%s"', scalar(@$data), $template->{$option}),
85 \@path_field);
86  
87 return FALSE;
88 }
89  
90 return TRUE;
91 }
92  
93 sub size_max {
94 my ($self, $qv, $data, $template, $option, @path_field) = @_;
95  
96 throw Exception::Validator gettext('Key "%s" must be positive number', $option)
97 if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/;
98  
99 if (@$data > $template->{$option}) {
100 $qv->_add_error($template, gettext('Data size "%s" more than "%s"', scalar(@$data), $template->{$option}),
101 \@path_field);
102  
103 return FALSE;
104 }
105  
106 return TRUE;
107 }
108  
109 sub all {
110 my ($self, $qv, $data, $template, $option, @path_field) = @_;
111  
112 throw Exception::Validator gettext('Options "all" and "contents" can not be used together')
113 if exists($template->{$option}) && exists($template->{'contents'});
114  
115 throw Exception::Validator gettext('Option "%s" must be HASH')
116 if !defined($template->{$option}) || ref($template->{$option}) ne 'HASH';
117  
118 my $num = 0;
119 foreach (@$data) {
120 my @path = (@path_field, $num);
121  
122 $qv->_validation($_, $template->{'all'}, undef, @path);
123  
124 return FALSE if $qv->has_error(\@path);
125  
126 $num++;
127 }
128  
129 return TRUE;
130 }
131  
132 sub contents {
133 my ($self, $qv, $data, $template, $option, @path_field) = @_;
134  
135 throw Exception::Validator gettext('Option "%s" must be ARRAY', $option)
136 if !defined($template->{$option}) || ref($template->{$option}) ne 'ARRAY';
137  
138 if (@$data != @{$template->{$option}}) {
139 $qv->_add_error($template,
140 gettext('Data size "%s" no equal "%s"', scalar(@$data), scalar(@{$template->{$option}})), \@path_field);
141  
142 return FALSE;
143 }
144  
145 my $num = 0;
146 foreach (@$data) {
147 my @path = (@path_field, $num);
148  
149 $qv->_validation($_, $template->{$option}[$num], undef, @path);
150  
151 return FALSE if $qv->has_error(\@path);
152  
153 $num++;
154 }
155  
156 return TRUE;
157 }
158  
159 TRUE;