| 1 | # | ||
| 2 | # ConstraintsFactory.pm - Module to create constraints for HTML::FormValidator. | ||
| 3 | # | ||
| 4 | # This file is part of FormValidator. | ||
| 5 | # | ||
| 6 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 7 | # | ||
| 8 | # Copyright (C) 2000 iNsu Innovations Inc. | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify | ||
| 11 | # it under the terms as perl itself. | ||
| 12 | # | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | package HTML::FormValidator::ConstraintsFactory; | ||
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | HTML::FormValidator::ConstraintsFactory - Module to create constraints for HTML::FormValidator. | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | use HTML::FormValidator::ConstraintsFactory qw( :set :bool ); | ||
| 26 | |||
| 27 | constraints => { | ||
| 28 | param1 => make_or_constraint( | ||
| 29 | make_num_set_constraint( -1, ( 1 .. 10 ) ), | ||
| 30 | make_set_constraint( 1, ( 20 .. 30 ) ), | ||
| 31 | ), | ||
| 32 | province => make_word_set_constraint( "AB QC ON TN NU" ), | ||
| 33 | bid => make_range_constraint( 1, 1, 10 ), | ||
| 34 | } | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | This module contains several functions which returns closures that can | ||
| 39 | be used for constraints. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS ); | ||
| 44 | |||
| 45 | BEGIN { | ||
| 46 | require Exporter; | ||
| 47 | |||
| 48 | ($VERSION) = '$Revision: 1.3 $' =~ /Revision: ([\d.]+)/; | ||
| 49 | |||
| 50 | @ISA = qw( Exporter ); | ||
| 51 | |||
| 52 | @EXPORT = (); | ||
| 53 | @EXPORT_OK = (); | ||
| 54 | |||
| 55 | %EXPORT_TAGS = | ||
| 56 | ( | ||
| 57 | bool => [ qw( make_not_constraint make_or_constraint | ||
| 58 | make_and_constraint ) ], | ||
| 59 | set => [ qw( make_set_constraint make_num_set_constraint | ||
| 60 | make_word_set_constraint make_cmp_set_constraint ) ], | ||
| 61 | num => [ qw( make_clamp_constraint make_lt_constraint | ||
| 62 | make_le_constraint make_gt_constraint | ||
| 63 | make_ge_constraint ) ], | ||
| 64 | ); | ||
| 65 | |||
| 66 | Exporter::export_ok_tags( 'bool' ); | ||
| 67 | Exporter::export_ok_tags( 'set' ); | ||
| 68 | Exporter::export_ok_tags( 'num' ); | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | =pod | ||
| 73 | |||
| 74 | =head1 BOOLEAN CONSTRAINTS | ||
| 75 | |||
| 76 | Those constraints are available by using the C<:bool> tag. | ||
| 77 | |||
| 78 | =head2 make_not_constraint( $c1 ) | ||
| 79 | |||
| 80 | This will create a constraint that will return the negation of the | ||
| 81 | result of constraint $c1. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub make_not_constraint { | ||
| 86 | my $c1 = $_[0]; | ||
| 87 | # Closure | ||
| 88 | return sub { ! $c1->( @_ ) }; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 make_or_constraint( @constraints ) | ||
| 92 | |||
| 93 | This will create a constraint that will return the result of the first | ||
| 94 | constraint that return an non false result. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub make_or_constraint { | ||
| 99 | my @c = @_; | ||
| 100 | # Closure | ||
| 101 | return sub { | ||
| 102 | my $res; | ||
| 103 | foreach my $c ( @c ) { | ||
| 104 | $res = $c->( @_ ); | ||
| 105 | return $res if $res; | ||
| 106 | } | ||
| 107 | return $res; | ||
| 108 | }; | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 make_and_constraint( @constraints ) | ||
| 112 | |||
| 113 | This will create a constraint that will return the result of the first | ||
| 114 | constraint that return an non false result only if all constraints | ||
| 115 | returns a non-false results. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub make_and_constraint { | ||
| 120 | my @c = @_; | ||
| 121 | |||
| 122 | # Closure | ||
| 123 | return sub { | ||
| 124 | my $res; | ||
| 125 | foreach my $c ( @c ) { | ||
| 126 | $res = $c->( @_ ); | ||
| 127 | return $res if ! $res; | ||
| 128 | |||
| 129 | $res ||= $res; | ||
| 130 | } | ||
| 131 | return $res; | ||
| 132 | }; | ||
| 133 | } | ||
| 134 | |||
| 135 | =pod | ||
| 136 | |||
| 137 | =head1 SET CONSTRAINTS | ||
| 138 | |||
| 139 | Those constraints are available by using the C<:set> tag. | ||
| 140 | |||
| 141 | =head2 make_set_constraint( $res, @elements ) | ||
| 142 | |||
| 143 | This will create a constraint that will return $res if the value | ||
| 144 | is one of the @elements set, or the negation of $res otherwise. | ||
| 145 | |||
| 146 | The C<eq> operator is used for comparison. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub make_set_constraint { | ||
| 151 | my $res = shift; | ||
| 152 | my @values = @_; | ||
| 153 | |||
| 154 | # Closure | ||
| 155 | return sub { | ||
| 156 | my $v = $_[0]; | ||
| 157 | foreach my $t ( @values ) { | ||
| 158 | return $res if $t eq $v; | ||
| 159 | } | ||
| 160 | return ! $res; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 make_num_set_constraint( $res, @elements ) | ||
| 165 | |||
| 166 | This will create a constraint that will return $res if the value | ||
| 167 | is one of the @elements set, or the negation of $res otherwise. | ||
| 168 | |||
| 169 | The C<==> operator is used for comparison. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub make_num_set_constraint { | ||
| 174 | my $res = shift; | ||
| 175 | my @values = @_; | ||
| 176 | |||
| 177 | # Closure | ||
| 178 | return sub { | ||
| 179 | my $v = $_[0]; | ||
| 180 | foreach my $t ( @values ) { | ||
| 181 | return $res if $t == $v; | ||
| 182 | } | ||
| 183 | return ! $res; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | =head2 make_word_set_constraint( $res, $set ) | ||
| 188 | |||
| 189 | This will create a constraint that will return $res if the value is | ||
| 190 | a word in $set, or the negation of $res otherwise. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub make_word_set_constraint { | ||
| 195 | my ($res,$set) = @_; | ||
| 196 | |||
| 197 | # Closure | ||
| 198 | return sub { | ||
| 199 | my $v = $_[0]; | ||
| 200 | if ( $set =~ /\b$v\b/i ) { | ||
| 201 | return $res; | ||
| 202 | } else { | ||
| 203 | return ! $res; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 make_cmp_set_constraint( $res, $cmp, @elements ) | ||
| 209 | |||
| 210 | This will create a constraint that will return $res if the value | ||
| 211 | is one of the @elements set, or the negation of $res otherwise. | ||
| 212 | |||
| 213 | $cmp is a function which takes two argument and should return true or false depending if the two elements are equal. | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub make_match_set_constraint { | ||
| 218 | my $res = shift; | ||
| 219 | my $cmp = shift; | ||
| 220 | my @values = @_; | ||
| 221 | |||
| 222 | # Closure | ||
| 223 | return sub { | ||
| 224 | my $v = $_[0]; | ||
| 225 | foreach my $t ( @values ) { | ||
| 226 | return $res if $cmp->($v, $t ); | ||
| 227 | } | ||
| 228 | return ! $res; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | =pod | ||
| 233 | |||
| 234 | =head1 NUMERICAL LOGICAL CONSTRAINTS | ||
| 235 | |||
| 236 | Those constraints are available by using the C<:num> tag. | ||
| 237 | |||
| 238 | =head2 make_clamp_constraint( $res, $low, $high ) | ||
| 239 | |||
| 240 | This will create a constraint that will return $res if the value | ||
| 241 | is between $low and $high bounds included or its negation otherwise. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub make_clamp_constraint { | ||
| 246 | my ( $res, $low, $high ) = @_; | ||
| 247 | |||
| 248 | return sub { | ||
| 249 | my $v = $_[0]; | ||
| 250 | $v < $low || $v > $high ? ! $res : $res; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | =head2 make_lt_constraint( $res, $bound ) | ||
| 255 | |||
| 256 | This will create a constraint that will return $res if the value | ||
| 257 | is lower than $bound, or the negation of $res otherwise. | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub make_lt_constraint { | ||
| 262 | my ( $res, $bound ) = @_; | ||
| 263 | |||
| 264 | return sub { | ||
| 265 | $_[0] < $bound ? $res : ! $res; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | =head2 make_le_constraint( $res, $bound ) | ||
| 270 | |||
| 271 | This will create a constraint that will return $res if the value | ||
| 272 | is lower or equal than $bound, or the negation of $res otherwise. | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | sub make_le_constraint { | ||
| 277 | my ( $res, $bound ) = @_; | ||
| 278 | |||
| 279 | return sub { | ||
| 280 | $_[0] <= $bound ? $res : ! $res; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | =head2 make_gt_constraint( $res, $bound ) | ||
| 285 | |||
| 286 | This will create a constraint that will return $res if the value | ||
| 287 | is greater than $bound, or the negation of $res otherwise. | ||
| 288 | |||
| 289 | =cut | ||
| 290 | |||
| 291 | sub make_gt_constraint { | ||
| 292 | my ( $res, $bound ) = @_; | ||
| 293 | |||
| 294 | return sub { | ||
| 295 | $_[0] >= $bound ? $res : ! $res; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | =head2 make_ge_constraint( $res, $bound ) | ||
| 300 | |||
| 301 | This will create a constraint that will return $res if the value | ||
| 302 | is greater or equal than $bound, or the negation of $res otherwise. | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | sub make_ge_constraint { | ||
| 307 | my ( $res, $bound ) = @_; | ||
| 308 | |||
| 309 | return sub { | ||
| 310 | $_[0] >= $bound ? $res : ! $res; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | 1; | ||
| 315 | |||
| 316 | __END__ |