File Coverage

blib/lib/Form/Factory/Feature/Control/Length.pm
Criterion Covered Total %
statement 24 26 92.3
branch 11 14 78.5
condition 9 12 75.0
subroutine 5 5 100.0
pod 3 3 100.0
total 52 60 86.6


line stmt bran cond sub pod time code
1             package Form::Factory::Feature::Control::Length;
2             $Form::Factory::Feature::Control::Length::VERSION = '0.022';
3 1     1   649 use Moose;
  1         2  
  1         7  
4              
5             with qw(
6             Form::Factory::Feature
7             Form::Factory::Feature::Role::Check
8             Form::Factory::Feature::Role::Control
9             Form::Factory::Feature::Role::CustomControlMessage
10             );
11              
12 1     1   4953 use Carp ();
  1         1  
  1         310  
13              
14             # ABSTRACT: A control feature for checking length
15              
16              
17             has minimum => (
18             is => 'ro',
19             isa => 'Int',
20             predicate => 'has_minimum',
21             );
22              
23              
24             has maximum => (
25             is => 'ro',
26             isa => 'Int',
27             predicate => 'has_maximum',
28             );
29              
30             sub BUILDARGS {
31 6     6 1 11 my $class = shift;
32 6 50       61 my $args = @_ == 1 ? $_[0] : { @_ };
33              
34 6 50 66     78 if (defined $args->{minimum} and defined $args->{maximum}
      66        
35             and $args->{minimum} >= $args->{maximum}) {
36 0         0 Carp::croak('length minimum must be less than maximum');
37             }
38              
39 6         43 return $class->SUPER::BUILDARGS(@_);
40             }
41              
42              
43             sub check_control {
44 6     6 1 8 my ($self, $control) = @_;
45              
46 6 50       17 return if $control->does('Form::Factory::Control::Role::ScalarValue');
47              
48 0         0 Carp::croak("the length feature only works with scalar values\n");
49             }
50              
51              
52             sub check {
53 15     15 1 20 my $self = shift;
54 15         486 my $value = $self->control->current_value;
55              
56 15 100       73 return if length($value) == 0;
57              
58 14 100 100     621 if ($self->has_minimum and length($value) < $self->minimum) {
59 3         5 $self->control_error(
60 3         88 "the %s must be at least @{[$self->minimum]} characters long"
61             );
62 3         89 $self->result->is_valid(0);
63             }
64              
65 14 100 66     561 if ($self->has_maximum and length($value) > $self->maximum) {
66 4         6 $self->control_error(
67 4         114 "the %s must be no longer than @{[$self->maximum]} characters"
68             );
69 4         171 $self->result->is_valid(0);
70             }
71              
72 14 100       446 $self->result->is_valid(1) unless $self->result->is_validated;
73             }
74              
75             __PACKAGE__->meta->make_immutable;
76              
77             __END__
78              
79             =pod
80              
81             =encoding UTF-8
82              
83             =head1 NAME
84              
85             Form::Factory::Feature::Control::Length - A control feature for checking length
86              
87             =head1 VERSION
88              
89             version 0.022
90              
91             =head1 SYNOPSIS
92              
93             has_control login_name => (
94             control => 'text',
95             feature => {
96             length => {
97             minimum => 3,
98             maximum => 15,
99             },
100             },
101             );
102              
103             =head1 DESCRIPTION
104              
105             Linked to a control, it checks to see that the string is not too short or too long.
106              
107             =head1 ATTRIBUTES
108              
109             =head2 minumum
110              
111             The optional minimum length permitted for the string.
112              
113             =head2 maximum
114              
115             The optional maximum length permitted for the string.
116              
117             =head1 METHODS
118              
119             =head2 check_control
120              
121             Makes sure the value is a L<Form::Factory::Control::Role::ScalarValue>.
122              
123             =head2 check
124              
125             Verifies that the value of the control is not too short or too long.
126              
127             =head1 AUTHOR
128              
129             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is copyright (c) 2015 by Qubling Software LLC.
134              
135             This is free software; you can redistribute it and/or modify it under
136             the same terms as the Perl 5 programming language system itself.
137              
138             =cut