| 1 | package Params::Profile; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Params::Profile::Data_FormValidator; | ||
| 6 | use Params::Profile::Params_Check; | ||
| 7 | |||
| 8 | use base qw/Class::Data::Inheritable/; | ||
| 9 | |||
| 10 | our $VERSION = '0.11'; | ||
| 11 | |||
| 12 | use constant NO_ARGS => {}; | ||
| 13 | use constant NO_PROFILE => undef; | ||
| 14 | |||
| 15 | my %Cache; | ||
| 16 | my $Mod_DV = __PACKAGE__ . '::Data_FormValidator'; | ||
| 17 | my $Mod_PC = __PACKAGE__ . '::Params_Check'; | ||
| 18 | |||
| 19 | __PACKAGE__->mk_classdata(qw/Profiles/); | ||
| 20 | |||
| 21 | __PACKAGE__->Profiles({}); | ||
| 22 | |||
| 23 | =head1 NAME | ||
| 24 | |||
| 25 | Params::Profile - module for registering Parameter profiles | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | package Foo::Bar; | ||
| 30 | |||
| 31 | use Params::Profile; | ||
| 32 | |||
| 33 | ### Single profile | ||
| 34 | Params::Profile->register_profile( | ||
| 35 | 'method' => 'subroto', | ||
| 36 | 'profile' => { | ||
| 37 | testkey1 => { required => 1 }, | ||
| 38 | testkey2 => { | ||
| 39 | required => 1, | ||
| 40 | allow => qr/^\d+$/, | ||
| 41 | }, | ||
| 42 | testkey3 => { | ||
| 43 | allow => qr/^\w+$/, | ||
| 44 | }, | ||
| 45 | }, | ||
| 46 | ); | ||
| 47 | |||
| 48 | sub subroto { | ||
| 49 | my (%params) = @_; | ||
| 50 | |||
| 51 | return unlesss Params::Profile->validate('params' => \%params); | ||
| 52 | ### DO SOME STUFF HERE ... | ||
| 53 | } | ||
| 54 | |||
| 55 | my $profile = Params::Profile->get_profile('method' => 'subroto'); | ||
| 56 | |||
| 57 | ### Multiple Profile | ||
| 58 | Params::Profile->register_profile( | ||
| 59 | 'method' => 'subalso', | ||
| 60 | 'profile' => [ | ||
| 61 | 'subroto', | ||
| 62 | { | ||
| 63 | testkey4 => { required => 1 }, | ||
| 64 | testkey5 => { | ||
| 65 | required => 1, | ||
| 66 | allow => qr/^\d+$/, | ||
| 67 | }, | ||
| 68 | testkey6 => { | ||
| 69 | allow => qr/^\w+$/, | ||
| 70 | }, | ||
| 71 | }, | ||
| 72 | ], | ||
| 73 | ); | ||
| 74 | |||
| 75 | |||
| 76 | sub subalso { | ||
| 77 | my (%params) = @_; | ||
| 78 | |||
| 79 | ### Checks parameters agains profile of subroto and above registered | ||
| 80 | ### profile | ||
| 81 | return unlesss Params::Profile->validate('params' => \%params); | ||
| 82 | |||
| 83 | ### DO SOME STUFF HERE ... | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | =head1 DESCRIPTION | ||
| 88 | |||
| 89 | Params::Profile provides a mechanism for a centralised Params::Check or a | ||
| 90 | Data::FormValidater profile. You can bind a profile to a class::subroutine, | ||
| 91 | then, when you are in a subroutine you can simply call | ||
| 92 | Params::Profile->check($params) of Params::Profile->validate($params) to | ||
| 93 | validate against this profile. Validate will return true or false on | ||
| 94 | successfull or failed validation. Check will return what C<Data::FormValidator> | ||
| 95 | or C<Params::Check> would return. (For C<Params::Check> this is simply a hash | ||
| 96 | with the validated parameters , for C<Data::FormValidator>, this is a | ||
| 97 | C<Data::FormValidator::Results> object) | ||
| 98 | |||
| 99 | =head1 Object Methods | ||
| 100 | |||
| 101 | =head2 Params::Profile->register_profile('method' => $method, 'profile' => | ||
| 102 | $profile [, caller => $callerclass ) | ||
| 103 | |||
| 104 | Register a new profile for method for the called-from caller class. Instead of | ||
| 105 | a profile, you could give a STRING containing the method from which you want to | ||
| 106 | use the profile...or simpler saying: make an alias to a profile. You can also | ||
| 107 | give an ARRAYREF containing both strings (defining the aliases) and HASHREFS, | ||
| 108 | defining profiles which then will be combined (See second example in SYNOPSYS). | ||
| 109 | When you provide the optional caller option, you define the class where the | ||
| 110 | given method is defined. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub register_profile { | ||
| 115 | my ($class, %args) = @_; | ||
| 116 | my ($method, $new_profiles, $caller, @profiles, $type); | ||
| 117 | |||
| 118 | my $tpl = { | ||
| 119 | method => { | ||
| 120 | required => 1, | ||
| 121 | store => \$method, | ||
| 122 | }, | ||
| 123 | profile => { | ||
| 124 | required => 1, | ||
| 125 | ### Allow hashref or plain text defining alias, | ||
| 126 | allow => sub { | ||
| 127 | UNIVERSAL::isa($_[0], 'HASH') || | ||
| 128 | UNIVERSAL::isa($_[0], 'ARRAY') || | ||
| 129 | !ref($_[0]) || $_[0] eq NO_ARGS | ||
| 130 | || $_[0] eq NO_PROFILE | ||
| 131 | }, | ||
| 132 | store => \$new_profiles, | ||
| 133 | }, | ||
| 134 | 'caller' => { | ||
| 135 | required => 0, | ||
| 136 | allow => qr/^[\w0-9:-]+$/, | ||
| 137 | default => $class->_get_caller_class, | ||
| 138 | store => \$caller, | ||
| 139 | }, | ||
| 140 | }; | ||
| 141 | |||
| 142 | Params::Check::check($tpl, \%args) or ( | ||
| 143 | $class->_raise_warning('Failed validating input parameters'), | ||
| 144 | return | ||
| 145 | ); | ||
| 146 | |||
| 147 | my $subname = $class->_full_method_name( $method, $caller ); | ||
| 148 | |||
| 149 | ### Create an array of profiles for easyer checking | ||
| 150 | @profiles = UNIVERSAL::isa($new_profiles, 'ARRAY') ? @$new_profiles : ($new_profiles); | ||
| 151 | |||
| 152 | ### Check given profiles | ||
| 153 | for (my $i=0; $i<@profiles; $i++) { | ||
| 154 | ### Check if alias exists | ||
| 155 | if (!ref($profiles[$i]) && !__PACKAGE__->Profiles->{ | ||
| 156 | $class->_full_method_name( $profiles[$i], $caller ) | ||
| 157 | } | ||
| 158 | ) { | ||
| 159 | $class->_raise_warning ( | ||
| 160 | 'Cannot alias (' . $subname . ') to missing profile: ' | ||
| 161 | . $class->_full_method_name( $profiles[$i], $caller )); | ||
| 162 | return; | ||
| 163 | } | ||
| 164 | |||
| 165 | ### Check if profiles match the chosen validator system (DV or PC) | ||
| 166 | if (ref($profiles[$i])) { | ||
| 167 | if ( | ||
| 168 | UNIVERSAL::isa($profiles[$i]->{required},'ARRAY') || | ||
| 169 | UNIVERSAL::isa($profiles[$i]->{optional},'ARRAY') | ||
| 170 | ) { | ||
| 171 | ( | ||
| 172 | $class->_raise_warning ( | ||
| 173 | 'Profile type clash for: ' | ||
| 174 | . $method | ||
| 175 | ), | ||
| 176 | return | ||
| 177 | ) if ($type && $type ne 'dv'); | ||
| 178 | $type = 'dv'; | ||
| 179 | } else { | ||
| 180 | ( | ||
| 181 | $class->_raise_warning ( | ||
| 182 | 'Profile type clash for: ' | ||
| 183 | . $class->_full_method_name($profiles[$i], $caller) | ||
| 184 | ), | ||
| 185 | return | ||
| 186 | ) if ($type && $type ne 'pc'); | ||
| 187 | $type = 'pc'; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | ### Set full name on aliases | ||
| 192 | $profiles[$i] = $class->_full_method_name( | ||
| 193 | $profiles[$i], | ||
| 194 | $class->_get_caller_class | ||
| 195 | ) if !ref($profiles[$i]); | ||
| 196 | } | ||
| 197 | |||
| 198 | ### Joy, all went fine, let's register this profile | ||
| 199 | __PACKAGE__->Profiles->{$subname} = { type => $type, | ||
| 200 | profiles => \@profiles, | ||
| 201 | }; | ||
| 202 | |||
| 203 | return 1; | ||
| 204 | |||
| 205 | } | ||
| 206 | |||
| 207 | sub _full_method_name { | ||
| 208 | my $class = shift; | ||
| 209 | my $method = shift; | ||
| 210 | my $caller = shift || $class->_get_caller_class; | ||
| 211 | |||
| 212 | $method = $method =~ /::/ | ||
| 213 | ? $method | ||
| 214 | : join( '::', $caller, $method ); | ||
| 215 | |||
| 216 | return $method; | ||
| 217 | } | ||
| 218 | |||
| 219 | =head2 Params::Profile->get_profile( method => $method [, caller => $caller ]); | ||
| 220 | |||
| 221 | Returns the profile registered for $method, or when no $method is given, | ||
| 222 | returns the profile for caller. | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | sub get_profile { | ||
| 227 | my ($class, %args) = @_; | ||
| 228 | my ($method, $caller); | ||
| 229 | |||
| 230 | my $tpl = { | ||
| 231 | method => { | ||
| 232 | required => 1, | ||
| 233 | store => \$method, | ||
| 234 | }, | ||
| 235 | 'caller' => { | ||
| 236 | required => 0, | ||
| 237 | allow => qr/^[\w0-9:-]+$/, | ||
| 238 | default => $class->_get_caller_class, | ||
| 239 | store => \$caller, | ||
| 240 | }, | ||
| 241 | }; | ||
| 242 | |||
| 243 | Params::Check::check($tpl, \%args) or ($class->_raise_warning('Failed validating input parameters'), return); | ||
| 244 | return $class->_get_profile( | ||
| 245 | $class->_full_method_name( $method, $caller ) | ||
| 246 | ); | ||
| 247 | } | ||
| 248 | |||
| 249 | =head2 Params::Profile->verify_profiles( \@methods ); | ||
| 250 | |||
| 251 | Verifies for each method in list, if the profile exists. Returns undef | ||
| 252 | when it doesn't. Also checks for aliases which point to no existing | ||
| 253 | profiles. | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | sub verify_profiles { | ||
| 258 | my $class = shift; | ||
| 259 | my @methods = @_ ? @_ : keys %{ __PACKAGE__->Profiles }; | ||
| 260 | |||
| 261 | my $fail; | ||
| 262 | for my $method ( @methods ) { | ||
| 263 | my $profile = $class->get_profile( method => $method ); | ||
| 264 | |||
| 265 | $fail++ unless $profile; | ||
| 266 | |||
| 267 | ### XXX validate the profile? | ||
| 268 | } | ||
| 269 | |||
| 270 | return if $fail; | ||
| 271 | return 1; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub _get_profile { | ||
| 275 | my ($class, $method) = @_; | ||
| 276 | my (%profile,@profiles); | ||
| 277 | |||
| 278 | ### No profile exists | ||
| 279 | unless ( exists __PACKAGE__->Profiles->{ $method } ) { | ||
| 280 | $class->_raise_warning( "No profile for '$method'" ); | ||
| 281 | return; | ||
| 282 | } | ||
| 283 | |||
| 284 | ### Alias of another profile | ||
| 285 | if ( !__PACKAGE__->Profiles->{$method}->{type} ) { | ||
| 286 | ### return profile of alias | ||
| 287 | return $class->_get_profile( | ||
| 288 | __PACKAGE__->Profiles->{$method}->{profiles}->[0] | ||
| 289 | ); | ||
| 290 | } else { | ||
| 291 | ### Create array of profiles for easyer handling | ||
| 292 | push(@profiles, !ref($_) ? | ||
| 293 | $class->_get_profile( | ||
| 294 | $_ | ||
| 295 | ) : $_ | ||
| 296 | ) for (@{ __PACKAGE__->Profiles->{$method}->{'profiles'} }); | ||
| 297 | |||
| 298 | ### No alias, return profile | ||
| 299 | if (__PACKAGE__->Profiles->{$method}->{type} eq 'dv') { | ||
| 300 | return $Mod_DV->get_profile( | ||
| 301 | $method, | ||
| 302 | @profiles, | ||
| 303 | ); | ||
| 304 | } else { | ||
| 305 | return $Mod_PC->get_profile( | ||
| 306 | $method, | ||
| 307 | @profiles, | ||
| 308 | ); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | return; | ||
| 312 | } | ||
| 313 | |||
| 314 | =head2 Params::Profile->clear_profiles(); | ||
| 315 | |||
| 316 | Clear the loaded profiles. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | sub clear_profiles { __PACKAGE__->Profiles({}); return 1; } | ||
| 321 | |||
| 322 | =head2 Params::Profile->get_profiles() | ||
| 323 | |||
| 324 | Just return a hash containing all the registered profiles, it is in the form: | ||
| 325 | method => [ \%profile ] | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | sub get_profiles { return __PACKAGE__->Profiles; } | ||
| 330 | |||
| 331 | =head2 Params::Profile->validate( params => %params [, method => $method ] ) | ||
| 332 | |||
| 333 | When given an hash of key->value pairs, this sub will check the values against | ||
| 334 | the loaded profile. Returns true when it validates, otherwise returns false. | ||
| 335 | It will check against the loaded profile for the given method, or when method | ||
| 336 | doesn't exist, against the caller | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | sub validate { | ||
| 341 | my ($class, %args) = @_; | ||
| 342 | my ($params, $method); | ||
| 343 | |||
| 344 | my $tpl = { | ||
| 345 | 'params' => { | ||
| 346 | required => 1, | ||
| 347 | store => \$params, | ||
| 348 | }, | ||
| 349 | 'method' => { | ||
| 350 | required => 0, | ||
| 351 | allow => qr/^[\w0-9:-]+$/, | ||
| 352 | default => | ||
| 353 | $class->_full_method_name( | ||
| 354 | $class->_get_caller_method | ||
| 355 | ), | ||
| 356 | store => \$method, | ||
| 357 | }, | ||
| 358 | }; | ||
| 359 | |||
| 360 | Params::Check::check($tpl, \%args) or ( | ||
| 361 | $class->_raise_warning('Failed validating input parameters'), | ||
| 362 | return | ||
| 363 | ); | ||
| 364 | |||
| 365 | my $profile = $class->_get_profile($method) or return; | ||
| 366 | |||
| 367 | ### Data::FormValidator or Params::Check template | ||
| 368 | my ($ok, $vclass); | ||
| 369 | if ( | ||
| 370 | UNIVERSAL::isa($profile->{required},'ARRAY') || | ||
| 371 | UNIVERSAL::isa($profile->{optional},'ARRAY') | ||
| 372 | ) { | ||
| 373 | ### Data::FormValidator | ||
| 374 | $ok = $Mod_DV->check($params, %{ $profile })->success; | ||
| 375 | } else { | ||
| 376 | $ok = Params::Check::check($profile, $params) ? 1 : 0; | ||
| 377 | } | ||
| 378 | return $ok; | ||
| 379 | } | ||
| 380 | |||
| 381 | =head2 Params::Profile->check( params => %params [, method => $method ] ) | ||
| 382 | |||
| 383 | When given an hash of key->value pairs, this sub will check the values against | ||
| 384 | the loaded profile. It will check against the loaded profile for the given | ||
| 385 | method, or when method doesn't exist, against the caller. | ||
| 386 | |||
| 387 | Depending on the used profile, it will return %hash with values for a | ||
| 388 | Params::Check profile. Or an object Data::FormValidator::Results when the | ||
| 389 | laoded profile is a Data::FormValidator profile. | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | sub check { | ||
| 394 | my ($class, %args) = @_; | ||
| 395 | my ($params, $method); | ||
| 396 | |||
| 397 | my $tpl = { | ||
| 398 | 'params' => { | ||
| 399 | required => 1, | ||
| 400 | store => \$params, | ||
| 401 | }, | ||
| 402 | 'method' => { | ||
| 403 | required => 0, | ||
| 404 | allow => qr/^[\w0-9:-]+$/, | ||
| 405 | default => | ||
| 406 | $class->_full_method_name( | ||
| 407 | $class->_get_caller_method, | ||
| 408 | ), | ||
| 409 | store => \$method, | ||
| 410 | }, | ||
| 411 | }; | ||
| 412 | |||
| 413 | Params::Check::check($tpl, \%args) or ( | ||
| 414 | $class->_raise_warning('Failed validating input parameters'), | ||
| 415 | return | ||
| 416 | ); | ||
| 417 | |||
| 418 | my $profile = $class->_get_profile($method) or return; | ||
| 419 | |||
| 420 | ### Data::FormValidator or Params::Check template | ||
| 421 | my ($ok, $vclass); | ||
| 422 | if ( | ||
| 423 | UNIVERSAL::isa($profile->{required},'ARRAY') || | ||
| 424 | UNIVERSAL::isa($profile->{optional},'ARRAY') | ||
| 425 | ) { | ||
| 426 | ### Data::FormValidator | ||
| 427 | return $Mod_DV->check($params, %{ $profile }) | ||
| 428 | #return Data::FormValidator->check($params, $profile); | ||
| 429 | } else { | ||
| 430 | ### Params::Check | ||
| 431 | return $Mod_PC->check($params, %{ $profile }); | ||
| 432 | } | ||
| 433 | } | ||
| 434 | |||
| 435 | sub _raise_warning { | ||
| 436 | my ($self, $warning) = @_; | ||
| 437 | warn($warning); | ||
| 438 | } | ||
| 439 | |||
| 440 | sub _get_caller_class { return [caller(1)]->[0]; } | ||
| 441 | |||
| 442 | sub _get_caller_method { return caller(2) ? [caller(2)]->[3] : ''; } | ||
| 443 | |||
| 444 | 1; | ||
| 445 | |||
| 446 | =head1 AUTHOR | ||
| 447 | |||
| 448 | This module by | ||
| 449 | |||
| 450 | Michiel Ootjers E<lt>michiel@cpan.orgE<gt>. | ||
| 451 | |||
| 452 | and | ||
| 453 | |||
| 454 | Jos Boumans E<lt>kane@cpan.orgE<gt>. | ||
| 455 | |||
| 456 | =head1 ACKNOWLEDGEMENTS | ||
| 457 | |||
| 458 | Thanks to Jos Boumans for C<Params::Check>, and the authors of | ||
| 459 | C<Data::FormValidator> | ||
| 460 | |||
| 461 | =head1 COPYRIGHT | ||
| 462 | |||
| 463 | This module is | ||
| 464 | copyright (c) 2002 Michiel Ootjers E<lt>michiel@cpan.orgE<gt>. | ||
| 465 | All rights reserved. | ||
| 466 | |||
| 467 | This library is free software; | ||
| 468 | you may redistribute and/or modify it under the same | ||
| 469 | terms as Perl itself. | ||
| 470 | |||
| 471 | =cut |