| 1 | package Params::Profile::Data_FormValidator; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Data::FormValidator; | ||
| 5 | |||
| 6 | our $VERSION = $Params::Profile::VERSION; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Params::Profile::Data_FormValidator - Backend module for Params::Profile | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | See C<Params::Profile> | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | C<Data::FormValidator> methods for C<Params::Profile> | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | ### params = HASHREF, profiles = ARRAYREF | ||
| 23 | sub validate { | ||
| 24 | my ($self, $params, %profile) = @_; | ||
| 25 | Data::FormValidator->check($params, \%profile)->is_success; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub _merge_profiles { | ||
| 29 | my ($self, @profiles) = @_; | ||
| 30 | |||
| 31 | ### Insert first profile in (tobe) merged_profile | ||
| 32 | my $merged_profile = shift(@profiles); | ||
| 33 | foreach my $profile (@profiles) { | ||
| 34 | foreach my $key (keys %{$profile}) { | ||
| 35 | if (UNIVERSAL::isa($profile->{$key}, 'ARRAY')) { | ||
| 36 | push( | ||
| 37 | @{ $merged_profile->{$key} }, | ||
| 38 | @{ $profile->{$key} } | ||
| 39 | ); | ||
| 40 | } elsif (UNIVERSAL::isa($profile->{$key}, 'HASH')) { | ||
| 41 | $merged_profile->{$key}->{$_} = $profile->{$key}->{$_} | ||
| 42 | for keys( %{ $profile->{$key} }); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | return $merged_profile; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub get_profile { | ||
| 51 | my ($self, $params, @profiles) = @_; | ||
| 52 | return $self->_merge_profiles(@profiles); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub check { | ||
| 56 | my ($self, $params, %profile) = @_; | ||
| 57 | Data::FormValidator->check($params, \%profile); | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | |||
| 62 | __END__ | ||
| 63 | |||
| 64 | =head1 AUTHOR | ||
| 65 | |||
| 66 | This module by | ||
| 67 | |||
| 68 | Michiel Ootjers E<lt>michiel@cpan.orgE<gt>. | ||
| 69 | |||
| 70 | and | ||
| 71 | |||
| 72 | Jos Boumans E<lt>kane@cpan.orgE<gt>. | ||
| 73 | |||
| 74 | =head1 ACKNOWLEDGEMENTS | ||
| 75 | |||
| 76 | Thanks to Jos Boumans for C<Params::Check>, and the authors of | ||
| 77 | C<Data::FormValidator> | ||
| 78 | |||
| 79 | =head1 COPYRIGHT | ||
| 80 | |||
| 81 | This module is | ||
| 82 | copyright (c) 2002 Michiel Ootjers E<lt>michiel@cpan.orgE<gt>. | ||
| 83 | All rights reserved. | ||
| 84 | |||
| 85 | This library is free software; | ||
| 86 | you may redistribute and/or modify it under the same | ||
| 87 | terms as Perl itself. | ||
| 88 | |||
| 89 | =cut |