line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Transpose::Validator::Base; |
2
|
|
|
|
|
|
|
|
3
|
14
|
|
|
14
|
|
8699
|
use strict; |
|
14
|
|
|
|
|
24
|
|
|
14
|
|
|
|
|
467
|
|
4
|
14
|
|
|
14
|
|
56
|
use warnings; |
|
14
|
|
|
|
|
19
|
|
|
14
|
|
|
|
|
397
|
|
5
|
|
|
|
|
|
|
|
6
|
14
|
|
|
14
|
|
83
|
use Moo; |
|
14
|
|
|
|
|
19
|
|
|
14
|
|
|
|
|
104
|
|
7
|
14
|
|
|
14
|
|
3748
|
use MooX::Types::MooseLike::Base qw(:all); |
|
14
|
|
|
|
|
25
|
|
|
14
|
|
|
|
|
5487
|
|
8
|
14
|
|
|
14
|
|
79
|
use namespace::clean; |
|
14
|
|
|
|
|
20
|
|
|
14
|
|
|
|
|
78
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Data::Transpose::Validator::Base - Base class for Data::Transpose::Validator |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $v = Data::Transpose::Validator::Base->new; |
17
|
|
|
|
|
|
|
ok($v->is_valid("string"), "A string is valid"); |
18
|
|
|
|
|
|
|
ok($v->is_valid([]), "Empty array is valid"); |
19
|
|
|
|
|
|
|
ok($v->is_valid({}), "Empty hash is valid"); |
20
|
|
|
|
|
|
|
ok(!$v->is_valid(undef), "undef is not valid"); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 METHODS (to be overwritten by the subclasses) |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 new() |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Constructor. It accepts an hash with the options. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head2 required |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Set or retrieve the required option. Returns true if required, false |
33
|
|
|
|
|
|
|
otherwise. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has required => (is => 'rw', |
38
|
|
|
|
|
|
|
isa => Bool, |
39
|
|
|
|
|
|
|
default => sub { 0 }); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head2 dtv_options |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Set or retrieve the Data::Transpose::Validator options. Given that the |
44
|
|
|
|
|
|
|
various classes have a different way to initialize the objects, this |
45
|
|
|
|
|
|
|
should be done only once the object has been built. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
E.g. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $obj = $class->new(%classoptions); |
50
|
|
|
|
|
|
|
$obj->dtv_options(\%dtv_options); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has dtv_options => (is => 'rw', |
55
|
|
|
|
|
|
|
isa => Maybe[HashRef]); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 dtv_value |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
On transposing, the value of the field is stored here. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
has dtv_value => (is => 'rw'); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
around dtv_value => sub { |
66
|
|
|
|
|
|
|
my $orig = shift; |
67
|
|
|
|
|
|
|
my $ret = $orig->(@_); |
68
|
|
|
|
|
|
|
defined $ret ? return $ret : return ''; |
69
|
|
|
|
|
|
|
}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
has _error => (is => 'rw', |
72
|
|
|
|
|
|
|
isa => ArrayRef, |
73
|
|
|
|
|
|
|
default => sub { [] }, |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
has _warnings => (is => 'rw', |
78
|
|
|
|
|
|
|
isa => ArrayRef, |
79
|
|
|
|
|
|
|
default => sub { [] }); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 reset_dtv_value |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Delete the dtv_value from the object |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub reset_dtv_value { |
89
|
357
|
|
|
357
|
1
|
8163
|
shift->dtv_value(undef); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 is_valid($what) |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Main method. Return true if the variable passed is defined, false if |
96
|
|
|
|
|
|
|
it's undefined, storing an error. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub is_valid { |
102
|
54
|
|
|
54
|
1
|
127
|
my ($self, $arg) = @_; |
103
|
54
|
|
|
|
|
120
|
$self->reset_errors; |
104
|
54
|
100
|
|
|
|
4906
|
if (defined $arg) { |
105
|
53
|
|
|
|
|
195
|
return 1 |
106
|
|
|
|
|
|
|
} else { |
107
|
1
|
|
|
|
|
4
|
$self->error("undefined"); |
108
|
1
|
|
|
|
|
5
|
return undef; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 error |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Main method to check why the validator returned false. When an |
115
|
|
|
|
|
|
|
argument is provided, set the error. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
In scalar context it returns a human-readable string with the errors. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
In list context it returns the raw error list, where each element is a |
120
|
|
|
|
|
|
|
pair of code and strings. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub error { |
125
|
2992
|
|
|
2992
|
1
|
6726
|
my ($self, $error) = @_; |
126
|
2992
|
100
|
|
|
|
5182
|
if ($error) { |
127
|
168
|
|
|
|
|
169
|
my $error_code_string; |
128
|
168
|
100
|
|
|
|
554
|
if (ref($error) eq "") { |
|
|
50
|
|
|
|
|
|
129
|
15
|
|
|
|
|
43
|
$error_code_string = [ $error => $error ]; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
elsif (ref($error) eq 'ARRAY') { |
132
|
153
|
|
|
|
|
181
|
$error_code_string = $error; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
else { |
135
|
0
|
|
|
|
|
0
|
die "Wrong usage: error accepts strings or arrayrefs\n"; |
136
|
|
|
|
|
|
|
} |
137
|
168
|
|
|
|
|
162
|
push @{$self->_error}, $error_code_string; |
|
168
|
|
|
|
|
4083
|
|
138
|
|
|
|
|
|
|
} |
139
|
2992
|
|
|
|
|
3505
|
my @errors = @{$self->_error}; |
|
2992
|
|
|
|
|
56249
|
|
140
|
2992
|
100
|
|
|
|
20707
|
return unless @errors; |
141
|
|
|
|
|
|
|
|
142
|
527
|
|
|
|
|
797
|
my $errorstring = join("; ", map { $_->[1] } @errors); |
|
791
|
|
|
|
|
1853
|
|
143
|
|
|
|
|
|
|
# in scalar context, we stringify |
144
|
527
|
100
|
|
|
|
2499
|
return wantarray ? @errors : $errorstring; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 reset_errors |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Clear the errors stored. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub reset_errors { |
154
|
584
|
|
|
584
|
1
|
12320
|
shift->_error([]); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 error_codes |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Returns the list of the error codes for the current validation. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub error_codes { |
165
|
10
|
|
|
10
|
1
|
14
|
my $self = shift; |
166
|
10
|
|
|
|
|
20
|
my @errors = $self->error; |
167
|
10
|
|
|
|
|
35
|
my @out; |
168
|
10
|
|
|
|
|
18
|
for (@errors) { |
169
|
10
|
|
|
|
|
22
|
push @out, $_->[0]; |
170
|
|
|
|
|
|
|
} |
171
|
10
|
|
|
|
|
20
|
return @out; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 warnings |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Set or retrieve a list of warnings issued by the validator. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 reset_warnings |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Reset the warning list. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub warnings { |
185
|
4
|
|
|
4
|
1
|
565
|
my ($self, @warn) = @_; |
186
|
4
|
100
|
|
|
|
14
|
if (@warn) { |
187
|
3
|
|
|
|
|
4
|
push @{$self->_warnings}, @warn; |
|
3
|
|
|
|
|
61
|
|
188
|
|
|
|
|
|
|
} |
189
|
4
|
|
|
|
|
35
|
return @{ $self->_warnings }; |
|
4
|
|
|
|
|
101
|
|
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub reset_warnings { |
193
|
14
|
|
|
14
|
1
|
275
|
shift->_warnings([]); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
1; |