| 1 | package Validator::Custom::Validation; | ||||||||||
| 2 | 5 | ▶ | use Object::Simple -base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 5 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 18 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 7 | 18 | my $self = shift->SUPER::new(@_); | |||||||||
| 8 | |||||||||||
| 9 | 18 | $self->{_failed_infos} = {}; | |||||||||
| 10 | |||||||||||
| 11 | 18 | return $self; | |||||||||
| 12 | } | ||||||||||
| 13 | |||||||||||
| 14 | 14 | ▶ | sub is_valid { | ||||||||
|
Subroutine
is_valid: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 15 | 14 | my ($self, $name) = @_; | |||||||||
| 16 | |||||||||||
| 17 | 14 | ▶ | if (defined $name) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 9 | ▶ | return exists $self->{_failed_infos}->{$name} ? 0 : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | } | ||||||||||
| 20 | else { | ||||||||||
| 21 | 5 | ▶ | return !(keys %{$self->{_failed_infos}}) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | } | ||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 32 | ▶ | sub add_failed { | ||||||||
|
Subroutine
add_failed: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 26 | 32 | my ($self, $name, $message) = @_; | |||||||||
| 27 | |||||||||||
| 28 | 32 | my $failed_infos = $self->{_failed_infos}; | |||||||||
| 29 | |||||||||||
| 30 | 32 | ▶ | if ($failed_infos->{$name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 0 | croak "\"$name\" is already exists"; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 32 | my @failed_names = keys %$failed_infos; | |||||||||
| 35 | 32 | my $pos; | |||||||||
| 36 | 32 | ▶ | if (@failed_names) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 20 | my $max_pos = 0; | |||||||||
| 38 | 20 | for my $failed_name (@failed_names) { | |||||||||
| 39 | 47 | my $pos = $failed_infos->{$failed_name}{pos}; | |||||||||
| 40 | 47 | ▶ | if ($pos > $max_pos) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 21 | $max_pos = $pos; | |||||||||
| 42 | } | ||||||||||
| 43 | } | ||||||||||
| 44 | 20 | $pos = $max_pos + 1; | |||||||||
| 45 | } | ||||||||||
| 46 | else { | ||||||||||
| 47 | 12 | $pos = 0; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 32 | $failed_infos->{$name}{pos} = $pos; | |||||||||
| 51 | |||||||||||
| 52 | 32 | ▶ | unless (defined $message) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 28 | $message = "$name is invalid"; | |||||||||
| 54 | } | ||||||||||
| 55 | 32 | $failed_infos->{$name}{message} = $message; | |||||||||
| 56 | 32 | $failed_infos->{$name}{pos} = $pos; | |||||||||
| 57 | |||||||||||
| 58 | 32 | return $self; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 17 | ▶ | sub failed { | ||||||||
|
Subroutine
failed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 62 | 17 | my $self = shift; | |||||||||
| 63 | |||||||||||
| 64 | 17 | my $failed_infos = $self->{_failed_infos}; | |||||||||
| 65 | 17 | my @failed = sort { $failed_infos->{$a}{pos} <=> | |||||||||
| 66 | 41 | $failed_infos->{$b}{pos} } keys %$failed_infos; | |||||||||
| 67 | |||||||||||
| 68 | 17 | return \@failed; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 8 | ▶ | sub message { | ||||||||
|
Subroutine
message: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 72 | 8 | my ($self, $name) = @_; | |||||||||
| 73 | |||||||||||
| 74 | # Parameter name not specified | ||||||||||
| 75 | 8 | ▶ | croak 'Parameter name must be specified' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | unless $name; | ||||||||||
| 77 | |||||||||||
| 78 | 8 | return $self->{_failed_infos}{$name}{message}; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 5 | ▶ | sub messages { | ||||||||
|
Subroutine
messages: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 5 | my $self = shift; | |||||||||
| 83 | |||||||||||
| 84 | 5 | my $failed_infos = $self->{_failed_infos}; | |||||||||
| 85 | |||||||||||
| 86 | # Messages | ||||||||||
| 87 | 5 | my $messages = []; | |||||||||
| 88 | 5 | for my $name (@{$self->failed}) { | |||||||||
| 89 | 8 | my $message = $failed_infos->{$name}{message}; | |||||||||
| 90 | 8 | push @$messages, $message; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 5 | return $messages; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 5 | ▶ | sub messages_to_hash { | ||||||||
|
Subroutine
messages_to_hash: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 97 | 5 | my $self = shift; | |||||||||
| 98 | |||||||||||
| 99 | 5 | my $failed_infos = $self->{_failed_infos}; | |||||||||
| 100 | |||||||||||
| 101 | # Name and message hash | ||||||||||
| 102 | 5 | my $messages = {}; | |||||||||
| 103 | 5 | for my $name (keys %$failed_infos) { | |||||||||
| 104 | 8 | $messages->{$name} = $failed_infos->{$name}{message}; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 5 | return $messages; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 5 | 1; | |||||||||
| 111 | |||||||||||
| 112 | =head1 NAME | ||||||||||
| 113 | |||||||||||
| 114 | Validator::Custom::Validation - a result of validation | ||||||||||
| 115 | |||||||||||
| 116 | =head1 SYNOPSYS | ||||||||||
| 117 | |||||||||||
| 118 | my $validation = $vc->validation; | ||||||||||
| 119 | |||||||||||
| 120 | $validation->add_failed(title => 'title is invalid'); | ||||||||||
| 121 | $validation->add_failed(name => 'name is invalid'); | ||||||||||
| 122 | |||||||||||
| 123 | # Is valid | ||||||||||
| 124 | my $is_valid = $validation->is_valid; | ||||||||||
| 125 | my $title_is_valid = $validation->is_valid('title'); | ||||||||||
| 126 | |||||||||||
| 127 | # Get all failed parameter names | ||||||||||
| 128 | my $failed = $validation->failed; | ||||||||||
| 129 | |||||||||||
| 130 | # Message | ||||||||||
| 131 | my $messages = $validation->messages; | ||||||||||
| 132 | my $title_message = $validation->message('title'); | ||||||||||
| 133 | my $messages_h = $validation->messages_to_hash; | ||||||||||
| 134 | |||||||||||
| 135 | =head1 METHODS | ||||||||||
| 136 | |||||||||||
| 137 | L<Validator::Custom::Validation> inherits all methods from L<Object::Simple> | ||||||||||
| 138 | and implements the following new ones. | ||||||||||
| 139 | |||||||||||
| 140 | =head2 new | ||||||||||
| 141 | |||||||||||
| 142 | my $validation = Validator::Custom::Validation->new; | ||||||||||
| 143 | |||||||||||
| 144 | Create a L<Validator::Custom::Validation> object. | ||||||||||
| 145 | |||||||||||
| 146 | Generally this method is not used. You should use C<validation> method of L<Validator::Custom>. | ||||||||||
| 147 | |||||||||||
| 148 | my $validation = $vc->validation; | ||||||||||
| 149 | |||||||||||
| 150 | =head2 is_valid | ||||||||||
| 151 | |||||||||||
| 152 | my $is_valid = $validation->is_valid; | ||||||||||
| 153 | my $is_valid = $validation->is_valid('title'); | ||||||||||
| 154 | |||||||||||
| 155 | Check if the result of validation is valid. | ||||||||||
| 156 | If name is specified, check if the parameter corresponding to the name is valid. | ||||||||||
| 157 | |||||||||||
| 158 | =head2 add_failed | ||||||||||
| 159 | |||||||||||
| 160 | $validation->add_failed('title' => 'title is invalid value'); | ||||||||||
| 161 | $validation->add_failed('title'); | ||||||||||
| 162 | |||||||||||
| 163 | Add a failed parameter name and message. | ||||||||||
| 164 | If message is omitted, default message is set automatically. | ||||||||||
| 165 | |||||||||||
| 166 | =head2 failed | ||||||||||
| 167 | |||||||||||
| 168 | my $failed = $validation->failed; | ||||||||||
| 169 | |||||||||||
| 170 | Get all failed parameter names. | ||||||||||
| 171 | |||||||||||
| 172 | =head2 message | ||||||||||
| 173 | |||||||||||
| 174 | my $message = $validation->message('title'); | ||||||||||
| 175 | |||||||||||
| 176 | Get a failed message corresponding to the name. | ||||||||||
| 177 | |||||||||||
| 178 | =head2 messages | ||||||||||
| 179 | |||||||||||
| 180 | my $messgaes = $validation->messages; | ||||||||||
| 181 | |||||||||||
| 182 | Get all failed messages. | ||||||||||
| 183 | |||||||||||
| 184 | =head2 messages_to_hash | ||||||||||
| 185 | |||||||||||
| 186 | my $messages_h = $validation->messages_to_hash; | ||||||||||
| 187 | |||||||||||
| 188 | Get all failed parameter names and messages as hash reference. | ||||||||||