File Coverage

blib/lib/Syccess/Validator/Length.pm
Criterion Covered Total %
statement 32 34 94.1
branch 30 34 88.2
condition 14 18 77.7
subroutine 7 7 100.0
pod 0 2 0.0
total 83 95 87.3


line stmt bran cond sub pod time code
1             package Syccess::Validator::Length;
2             BEGIN {
3 2     2   3619 $Syccess::Validator::Length::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: A validator to check the length of the value in chars
6             $Syccess::Validator::Length::VERSION = '0.103';
7 2     2   37 use Moo;
  2         3  
  2         10  
8 2     2   612 use Carp qw( croak );
  2         3  
  2         782  
9              
10             with qw(
11             Syccess::ValidatorSimple
12             );
13              
14             has min => (
15             is => 'ro',
16             predicate => 1,
17             );
18              
19             has max => (
20             is => 'ro',
21             predicate => 1,
22             );
23              
24             sub BUILD {
25 4     4 0 1701 my ( $self ) = @_;
26 4 50 33     138 croak __PACKAGE__.' cant have arg (specific length) and min/max'
      66        
27             if $self->has_arg && ( $self->has_min || $self->has_max );
28             }
29              
30             has message => (
31             is => 'lazy',
32             );
33              
34             sub _build_message {
35 4     4   653 my ( $self ) = @_;
36 4 100       20 if ($self->has_arg) {
37 1         17 return [ $self->format, $self->arg ];
38             } else {
39 3 100 100     23 if ($self->has_min && $self->has_max) {
    100          
    50          
40 1         16 return [ $self->format, $self->min, $self->max ];
41             } elsif ($self->has_min) {
42 1         22 return [ $self->format, $self->min ];
43             } elsif ($self->has_max) {
44 1         16 return [ $self->format, $self->max ];
45             }
46             }
47 0         0 croak __PACKAGE__.' needs an arg, min or max value';
48             }
49              
50             has format => (
51             is => 'lazy',
52             );
53              
54             sub _build_format {
55 4     4   484 my ( $self ) = @_;
56 4 100       10 if ($self->has_arg) {
57 1         11 return '%s must be exactly %s characters.';
58             } else {
59 3 100 100     25 if ($self->has_min && $self->has_max) {
    100          
    50          
60 1         11 return '%s must be between %s and %s characters.';
61             } elsif ($self->has_min) {
62 1         11 return '%s must be at least %s characters.';
63             } elsif ($self->has_max) {
64 1         9 return '%s is not allowed to be more than %s characters.';
65             }
66             }
67 0         0 croak __PACKAGE__.' needs an arg, min or max value';
68             }
69              
70             sub validator {
71 10     10 0 13 my ( $self, $value ) = @_;
72 10         14 my $length = length($value);
73 10 100       23 if ($self->has_arg) {
74 3 100       32 return $self->message if $length != $self->arg;
75             } else {
76 7 100 100     54 if ($self->has_min && $self->has_max) {
    100          
    50          
77 2 100 66     28 return $self->message if $length < $self->min || $length > $self->max;
78             } elsif ($self->has_min) {
79 3 100       53 return $self->message if $length < $self->min;
80             } elsif ($self->has_max) {
81 2 100       27 return $self->message if $length > $self->max;
82             }
83             }
84 4         21 return;
85             }
86              
87             1;
88              
89             __END__
90              
91             =pod
92              
93             =head1 NAME
94              
95             Syccess::Validator::Length - A validator to check the length of the value in chars
96              
97             =head1 VERSION
98              
99             version 0.103
100              
101             =head1 SYNOPSIS
102              
103             Syccess->new(
104             fields => [
105             pin => [ length => 4 ],
106             username => [ length => {
107             min => 3,
108             max => 12,
109             message => 'Username must be between %s and %s characters.'
110             } ],
111             tweet => [ length => { max => 140 } ],
112             ],
113             );
114              
115             =head1 DESCRIPTION
116              
117             This validator allows to check for the amount of characters in the value.
118             The default error message depends on the parameter given. The default
119             functionality is using the parameter as the required length for the value.
120             Longer or shorter would be denied. This can't be combined with L</min> or
121             L</max>.
122              
123             =head1 ATTRIBUTES
124              
125             =head2 min
126              
127             Given this parameter, allows to define a minimum length for the value. This
128             can be combined with L</max>.
129              
130             =head2 max
131              
132             Given this parameter, allows to define a maximum length for the value. This
133             can be combined with L</min>.
134              
135             =head2 message
136              
137             This contains the error message or the format for the error message
138             generation. See L<Syccess::Error/validator_message>.
139              
140             =encoding utf8
141              
142             =head1 SUPPORT
143              
144             IRC
145              
146             Join #sycontent on irc.perl.org. Highlight Getty for fast reaction :).
147              
148             Repository
149              
150             http://github.com/SyContent/Syccess
151             Pull request and additional contributors are welcome
152              
153             Issue Tracker
154              
155             http://github.com/SyContent/Syccess/issues
156              
157             =cut
158              
159             =head1 AUTHOR
160              
161             Torsten Raudssus <torsten@raudss.us>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2014 by Torsten Raudssus.
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =cut