File: Results.pm

package info (click to toggle)
libformvalidator-simple-perl 0.29-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: perl: 3,043; makefile: 4
file content (376 lines) | stat: -rw-r--r-- 8,772 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package FormValidator::Simple::Results;
use strict;
use base qw/Class::Accessor::Fast/;
use FormValidator::Simple::Result;
use FormValidator::Simple::Exception;
use FormValidator::Simple::Constants;
use Tie::IxHash;
use List::MoreUtils;

__PACKAGE__->mk_accessors(qw/_records message/);

sub new {
    my $class = shift;
    my $self  = bless { }, $class;
    $self->_init(@_);
    return $self;
}

sub _init {
    my ($self, %args) = @_;
    my %hash = ();
    tie (%hash, 'Tie::IxHash');
    $self->_records(\%hash);

    my $messages = delete $args{messages};
    $self->message($messages);
}

sub messages {
    my ($self, $action) = @_;
    my @messages = ();
    my $keys = $self->error;
    foreach my $key ( @$keys ) {
        my $types = $self->error($key);
        foreach my $type ( @$types ) {
            push @messages,
                $self->message->get($action, $key, $type);
        }
    }
    @messages = List::MoreUtils::uniq(@messages);
    return \@messages;
}

sub field_messages {
    my ($self, $action) = @_;
    my $messages = {};
    my $keys = $self->error;
    foreach my $key ( @$keys ) {
        $messages->{$key} = [];
        my $types = $self->error($key);
        foreach my $type ( @$types ) {
            my $message = $self->message->get($action, $key, $type);
            unless ( List::MoreUtils::any { $_ eq $message } @{ $messages->{$key} } ) {
                push @{ $messages->{$key} }, $message;
            }
        }
    }
    return $messages;
}

sub register {
    my ($self, $name) = @_;
    $self->_records->{$name}
        ||= FormValidator::Simple::Result->new($name);
}

sub record {
    my ($self, $name) = @_;
    $self->register($name)
    unless exists $self->_records->{$name};
    return $self->_records->{$name};
}

sub set_result {
    my ($self, $name, $type, $result) = @_;
    $self->register($name);
    $self->record($name)->set($type, $result);
}

sub set_invalid {
    my ($self, $name, $type) = @_;
    unless ($name && $type) {
        FormValidator::Simple::Exception->throw(
            qq/set_invalid needs two arguments./
        );
    }
    $self->set_result($name, $type, FALSE);
}

sub success {
    my $self = shift;
    return ($self->has_missing or $self->has_invalid) ? FALSE : TRUE;
}

sub has_error {
    my $self = shift;
    return ($self->has_missing or $self->has_invalid) ? TRUE : FALSE;
}

sub has_blank {
    my $self = shift;
    foreach my $record ( values %{ $self->_records } ) {
        return TRUE if $record->is_blank;
    }
    return FALSE;
}

*has_missing = \&has_blank;

sub has_invalid {
    my $self = shift;
    foreach my $record ( values %{ $self->_records } ) {
        return TRUE if $record->is_invalid;
    }
    return FALSE;
}

sub valid {
    my ($self, $name) = @_;
    if ($name) {
        return unless exists $self->_records->{$name};
        return $self->record($name)->is_valid
             ? $self->record($name)->data : FALSE;
    }
    else {
        my %valids
            = map { ( $_->name, $_->data ) } grep { $_->is_valid } values %{ $self->_records };
        return \%valids;
    }
}

sub error {
    my ($self, $name, $constraint) = @_;
    if ($name) {
        if ($constraint) {
            if ($constraint eq 'NOT_BLANK') {
            return $self->record($name)->is_blank
                ? TRUE
                : FALSE
                ;
            }
            return $self->record($name)->is_invalid_for($constraint)
                ? TRUE
                : FALSE
                ;
        }
        else {
            if ($self->record($name)->is_blank) {
                return wantarray ? 'NOT_BLANK' : ['NOT_BLANK'];
            }
            elsif ($self->record($name)->is_invalid) {
                my $constraints = $self->record($name)->constraints;
                my @invalids = grep { !$constraints->{$_} } keys %$constraints;
                return wantarray ? @invalids : \@invalids;
            }
            else {
                return FALSE;
            }
        }
    }
    else {
        my @errors = 
        map { $_->name } grep { $_->is_blank or $_->is_invalid } values %{ $self->_records };
        return wantarray ? @errors : \@errors;
    }
}

sub blank {
    my ($self, $name) = @_;
    if ($name) {
        return $self->record($name)->is_blank ? TRUE : FALSE;
    }
    else {
        my @blanks
            = map { $_->name } grep { $_->is_blank } values %{ $self->_records };
        return wantarray ? @blanks : \@blanks;
    }
}

*missing = \␣

sub invalid {
    my ($self, $name, $constraint) = @_;
    if ($name) {
        if ($constraint) {
            $self->record($name)->is_invalid_for($constraint)
                ? TRUE : FALSE;
        }
        else {
            if ($self->record($name)->is_invalid) {
                my $constraints = $self->record($name)->constraints;
                my @invalids = grep { !$constraints->{$_} } keys %$constraints;
                return wantarray ? @invalids : \@invalids;
            }
            else {
                return FALSE;
            }
        }
    }
    else {
        my @invalids
            = map { $_->name } grep { $_->is_invalid } values %{ $self->_records };
        return wantarray ? @invalids : \@invalids;
    }
}

sub clear {
  %{shift->_records} = ();
}

1;
__END__

=head1 NAME

FormValidator::Simple::Results - results of validation

=head1 SYNOPSIS

    my $results = FormValidator::Simple->check( $req => [
        name  => [qw/NOT_BLANK ASCII/, [qw/LENGTH 0 10/] ],
        email => [qw/NOT_BLANK EMAIL_LOOSE/, [qw/LENGTH 0 30/] ],
    ] );

    if ( $results->has_error ) {
        foreach my $key ( @{ $results->error() } ) {
            foreach my $type ( @{ $results->erorr($key) } ) {
                print "invalid: $key - $type \n";
            }
        }
    }

=head1 DESCRIPTION

This is for handling resuls of FormValidator::Simple's check.

This object behaves like Data::FormValidator's results object, but
has some specific methods.

=head1 CHECK RESULT

=over 4

=item has_missing

If there are missing values ( failed in validation 'NOT_BLANK' ), this method returns true.

    if ( $results->has_missing ) {
        ...
    }

=item has_invalid

If there are invalid values ( failed in some validations except 'NOT_BLANK' ), this method returns true.

    if ( $results->has_invalid ) {
        ...
    }

=item has_error

If there are missing or invalid values, this method returns true.

    if ( $results->has_error ) {
        ...
    }

=item success

inverse of has_error

    unless ( $resuls->success ) {
        ...
    }

=back

=head1 ANALYZING RESULTS

=head2 missing

=over 4

=item no argument

When you call this method with no argument, it returns keys failed 'NOT_BLANK' validation.

    my $missings = $results->missing;
    foreach my $missing_data ( @$missings ) {
        print $missing_data, "\n";
    }
    # -- print out, for example --
    # name
    # email

=item key

When you call this method with key-name, it returnes true if the value of the key is missing.

    if ( $results->missing('name') ) {
        print "name is empty! \n";
    }

=back

=head2 invalid

=over 4

=item no argument

When you call this method with no argument, it returns keys that failed some validation except 'NOT_BLANK'.

    my $invalids = $results->invalid;
    foreach my $invalid_data ( @$invalids ) {
        print $invalid_data, "\n";
    }
    # -- print out, for example --
    # name
    # email

=item key

When you call this method with key-name, it returns names of failed validation.

    my $failed_validations = $results->invalid('name');
    foreach my $validation ( @$failed_validations ) {
        print $validation, "\n";
    }
    # -- print out, for example --
    # ASCII
    # LENGTH

=item key and validation-name

When you call this method with key-name, it returns false if the value has passed the validation.

    if ( $results->invalid( name => 'LENGTH' ) ) {
        print "name is wrong length! \n";
    }

=back

=head2 error

This doesn't distinguish 'missing' and 'invalid'. You can use this like 'invalid' method,
but this consider 'NOT_BLANK' same as other validations.

    my $error_keys = $results->error;

    my $failed_validation = $resuls->error('name');
    # this includes 'NOT_BLANK'

    if ( $results->error( name => 'NOT_BLANK' ) ) {
        print "name is missing! \n";
    }

    if ( $results->error( name => 'ASCII' ) ) {
        print "name should be ascii code! \n";
    }

=head1 SEE ALSO

L<FormValidator::Simple>

=head1 AUTHOR

Lyo Kato E<lt>lyo.kato@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

This library is free software.
You can redistribute it and/or modify it under the same terms as perl itself.

=cut