line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Transpose::Validator::Set; |
2
|
2
|
|
|
2
|
|
973
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
59
|
|
3
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
52
|
|
4
|
2
|
|
|
2
|
|
7
|
use Scalar::Util qw/looks_like_number/; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
109
|
|
5
|
2
|
|
|
2
|
|
8
|
use Moo; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
8
|
|
6
|
|
|
|
|
|
|
extends 'Data::Transpose::Validator::Base'; |
7
|
2
|
|
|
2
|
|
493
|
use MooX::Types::MooseLike::Base qw(:all); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
564
|
|
8
|
2
|
|
|
2
|
|
9
|
use namespace::clean; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
12
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Data::Transpose::Validator::Set - Validate a string inside a set of values |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 METHODS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head2 new(list => \@list, multiple => 1) |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Constructor to set the list in which the value to be validated must be |
19
|
|
|
|
|
|
|
present. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has list => (is => 'rw', |
24
|
|
|
|
|
|
|
isa => ArrayRef, |
25
|
|
|
|
|
|
|
required => 1, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has multiple => (is => 'rw', |
29
|
|
|
|
|
|
|
isa => Bool, |
30
|
|
|
|
|
|
|
default => sub { 0 }); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 is_valid($value, [ $value, $value, ... ] ) |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
The validator. Returns a true value if the value (or the values) |
36
|
|
|
|
|
|
|
passed are all present in the set. Multiple values validate only if |
37
|
|
|
|
|
|
|
the C option is set in the constructor. It also accept an |
38
|
|
|
|
|
|
|
arrayref as single argument, if the C option is set. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub is_valid { |
44
|
27
|
|
|
27
|
1
|
5118
|
my ($self, @args) = @_; |
45
|
27
|
|
|
|
|
89
|
$self->reset_errors; |
46
|
27
|
|
|
|
|
1089
|
my @input; |
47
|
27
|
100
|
|
|
|
63
|
if (@args == 1) { |
|
|
50
|
|
|
|
|
|
48
|
21
|
|
|
|
|
34
|
my $arg = shift @args; |
49
|
21
|
100
|
|
|
|
63
|
if (ref($arg) eq 'ARRAY') { |
|
|
50
|
|
|
|
|
|
50
|
4
|
100
|
|
|
|
52
|
if ($self->multiple) { |
51
|
3
|
|
|
|
|
19
|
push @input, @$arg |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
1
|
|
|
|
|
661
|
$self->error([nomulti => "No multiple values are allowed"]) |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif (ref($arg) ne '') { |
58
|
0
|
|
|
|
|
0
|
die "Bad argument\n"; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
17
|
|
|
|
|
27
|
push @input, $arg; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
elsif (@args > 1) { |
65
|
6
|
100
|
|
|
|
107
|
if ($self->multiple) { |
66
|
5
|
|
|
|
|
52
|
push @input, @args; |
67
|
|
|
|
|
|
|
} else { |
68
|
1
|
|
|
|
|
11
|
$self->error([nomulti => "No multiple values are allowed"]); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
else { |
72
|
0
|
|
|
|
|
0
|
$self->error([noinput => "No value passed"]); |
73
|
|
|
|
|
|
|
} |
74
|
27
|
100
|
|
|
|
87
|
return undef if $self->error; |
75
|
25
|
|
|
|
|
65
|
return $self->_check_set(@input); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _check_set { |
80
|
25
|
|
|
25
|
|
44
|
my ($self, @input) = @_; |
81
|
25
|
|
|
|
|
49
|
my %list = $self->list_as_hash; |
82
|
25
|
|
|
|
|
55
|
foreach my $val (@input) { |
83
|
|
|
|
|
|
|
$self->error(["missinginset", "No match in the allowed values"]) |
84
|
35
|
100
|
|
|
|
124
|
unless exists $list{$val}; |
85
|
|
|
|
|
|
|
} |
86
|
25
|
100
|
|
|
|
63
|
$self->error ? return 0 : return 1; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 INTERNAL METHODS |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head2 multiple |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Accessor to the C option |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 list |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Accessor to the C option |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 list_as_hash |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Accessor to the list of values, as an hash. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub list_as_hash { |
107
|
25
|
|
|
25
|
1
|
28
|
my $self = shift; |
108
|
25
|
|
|
|
|
28
|
my %list = map { $_ => 1 } @{$self->list}; |
|
73
|
|
|
|
|
1315
|
|
|
25
|
|
|
|
|
460
|
|
109
|
25
|
|
|
|
|
104
|
return %list; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |