| 1 | package Gtk2::Ex::FormFactory::Rules; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | use File::Basename; | ||
| 7 | |||
| 8 | my %RULES = ( | ||
| 9 | "empty" => sub { $_[0] eq '' }, | ||
| 10 | "not-empty" => sub { $_[0] ne '' }, | ||
| 11 | |||
| 12 | "alphanumeric" => sub { $_[0] =~ /^\w+$/ }, | ||
| 13 | "identifier" => sub { $_[0] =~ /^[a-z_]\w*$/i }, | ||
| 14 | "no-whitespace" => sub { $_[0] !~ /\s/ }, | ||
| 15 | |||
| 16 | "zero" => sub { $_[0] =~ /^0(\.0*)?$/ }, | ||
| 17 | "not-zero" => sub { $_[0] !~ /^0(\.0*)?$/ }, | ||
| 18 | |||
| 19 | "integer" => sub { $_[0] =~ /^[+-]?\d+$/ }, | ||
| 20 | "positive-integer" => sub { $_[0] =~ /^\+?\d+$/ && $_[0] > 0; }, | ||
| 21 | "positive-zero-integer" => sub { $_[0] =~ /^\+?\d+$/ }, | ||
| 22 | "negative-integer" => sub { $_[0] =~ /^-\d+$/ }, | ||
| 23 | "negative-zero-integer" => sub { $_[0] =~ /^(-\d+|0+)$/ }, | ||
| 24 | |||
| 25 | "float" => sub { $_[0] =~ /^[+-]?\d+(\.\d+)?$/ }, | ||
| 26 | "positive-float" => sub { $_[0] =~ /^\+?\d+(\.\d+)?$/ && $_[0] > 0 }, | ||
| 27 | "positive-zero-float" => sub { $_[0] =~ /^\+?\d+(\.\d+)?$/ }, | ||
| 28 | "negative-float" => sub { $_[0] =~ /^-\d+(\.\d+)?$/ }, | ||
| 29 | "negative-zero-float" => sub { $_[0] =~ /^(-\d+(\.\d+)?|0+)$/ }, | ||
| 30 | |||
| 31 | "odd" => sub { $_[0] % 2 }, | ||
| 32 | "even" => sub { !($_[0] % 2) }, | ||
| 33 | |||
| 34 | "file-executable" => sub { (!-d $_[0] && -x $_[0]) }, | ||
| 35 | "file-writable" => sub { (!-d $_[0] && -w $_[0]) }, | ||
| 36 | "file-readable" => sub { (!-d $_[0] && -r $_[0]) }, | ||
| 37 | |||
| 38 | "dir-writable" => sub { (-d $_[0] && -w $_[0]) }, | ||
| 39 | "dir-readable" => sub { (-d $_[0] && -r $_[0]) }, | ||
| 40 | |||
| 41 | "parent-dir-writable" => sub { -w dirname($_[0]) }, | ||
| 42 | "parent-dir-readable" => sub { -r dirname($_[0]) }, | ||
| 43 | |||
| 44 | "executable-command" => \&rule_executable_command, | ||
| 45 | |||
| 46 | ); | ||
| 47 | |||
| 48 | my %RULES_MESSAGES = ( | ||
| 49 | "empty" => "{field} is not empty.", | ||
| 50 | "not-empty" => "{field} is empty.", | ||
| 51 | |||
| 52 | "alphanumeric" => "{field} is not alphanumeric.", | ||
| 53 | "identifier" => "{field} is no identifier.", | ||
| 54 | "no-whitespace" => "{field} contains whitespace.", | ||
| 55 | |||
| 56 | "zero" => "{field} is not zero", | ||
| 57 | "not-zero" => "{field} is zero", | ||
| 58 | |||
| 59 | "integer" => "{field} is no integer.", | ||
| 60 | "positive-integer" => "{field} is no positive integer.", | ||
| 61 | "positive-zero-integer" => "{field} is no positive/zero integer.", | ||
| 62 | "negative-integer" => "{field} is no negative integer.", | ||
| 63 | "negative-zero-integer" => "{field} is no negative/zero integer.", | ||
| 64 | |||
| 65 | "float" => "{field} is no float.", | ||
| 66 | "positive-float" => "{field} is no positive float.", | ||
| 67 | "positive-zero-float" => "{field} is no positive/zero float.", | ||
| 68 | "negative-float" => "{field} is no negative float.", | ||
| 69 | "negative-zero-float" => "{field} is no negative/zero float.", | ||
| 70 | |||
| 71 | "odd" => "{field} is not odd.", | ||
| 72 | "even" => "{field} is not even.", | ||
| 73 | |||
| 74 | "file-executable" => "{field} is no file and/or not executable.", | ||
| 75 | "file-writable" => "{field} is no file and/or not writable.", | ||
| 76 | "file-readable" => "{field} is no file and/or not readable.", | ||
| 77 | |||
| 78 | "dir-writable" => "{field} is no directory and/or not writable.", | ||
| 79 | "dir-readable" => "{field} is no directory and/or not readable.", | ||
| 80 | |||
| 81 | "parent-dir-writable" => "{field} has no writable parent directory.", | ||
| 82 | "parent-dir-readable" => "{field} has no readable parent directory.", | ||
| 83 | |||
| 84 | "executable-command" => "_rule_result", | ||
| 85 | ); | ||
| 86 | |||
| 87 | my $MESSAGE_FORMAT = "Data entered is invalid.\n\n[MESSAGES]\nOld value restored."; | ||
| 88 | |||
| 89 | sub get_rules { shift->{rules} } | ||
| 90 | sub get_rules_messages { shift->{rules_messages} } | ||
| 91 | sub get_message_format { shift->{message_format} } | ||
| 92 | |||
| 93 | sub set_rules { shift->{rules} = $_[1] } | ||
| 94 | sub set_rules_messages { shift->{rules_messages} = $_[1] } | ||
| 95 | sub set_message_format { shift->{message_format} = $_[1] } | ||
| 96 | |||
| 97 | sub new { | ||
| 98 | my $class = shift; | ||
| 99 | my %par = @_; | ||
| 100 | my ($rules, $rules_messages, $message_format) = | ||
| 101 | @par{'rules','rules_messages','message_format'}; | ||
| 102 | |||
| 103 | $rules ||= {}; | ||
| 104 | $rules_messages ||= {}; | ||
| 105 | $message_format ||= $MESSAGE_FORMAT; | ||
| 106 | |||
| 107 | my $self = bless { | ||
| 108 | rules => $rules, | ||
| 109 | rules_messages => $rules_messages, | ||
| 110 | message_format => $message_format, | ||
| 111 | }, $class; | ||
| 112 | |||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub check { | ||
| 117 | my $self = shift; | ||
| 118 | my ($rules, $field, $value) = @_; | ||
| 119 | |||
| 120 | $field ||= "Value"; | ||
| 121 | |||
| 122 | $rules = [ $rules ] unless ref $rules eq 'ARRAY'; | ||
| 123 | |||
| 124 | my $messages; | ||
| 125 | foreach my $rule ( @{$rules} ) { | ||
| 126 | if ( $rule eq 'or-empty' ) { | ||
| 127 | return "" if $value eq ''; | ||
| 128 | next; | ||
| 129 | } | ||
| 130 | if ( ref $rule eq 'CODE' ) { | ||
| 131 | my $msg = &$rule($value); | ||
| 132 | $messages .= "$msg\n" if $msg; | ||
| 133 | next; | ||
| 134 | } | ||
| 135 | |||
| 136 | my $coderef = $self->get_rules->{$rule} || $RULES{$rule}; | ||
| 137 | |||
| 138 | if ( $coderef ) { | ||
| 139 | my $rc = &$coderef($value); | ||
| 140 | my $message = | ||
| 141 | $self->get_rules_messages->{$rule} || | ||
| 142 | $RULES_MESSAGES{$rule}; | ||
| 143 | warn "Message of rule '$rule' not defined" | ||
| 144 | if $message eq ''; | ||
| 145 | $message ||= "{field} has an unknown error"; | ||
| 146 | if ( $message eq '_rule_result' ) { | ||
| 147 | if ( $rc ne '' ) { | ||
| 148 | $messages .= "$rc\n"; | ||
| 149 | } | ||
| 150 | } elsif ( !$rc ) { | ||
| 151 | $messages .= "$message\n"; | ||
| 152 | } | ||
| 153 | } else { | ||
| 154 | warn "Unknown rule '$rule'. Verification skipped."; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | if ( $messages ) { | ||
| 159 | my $format = $self->get_message_format; | ||
| 160 | $messages =~ s/\{field\}/$field/g; | ||
| 161 | $format =~ s/\[MESSAGES\]/$messages/; | ||
| 162 | $messages = $format; | ||
| 163 | } | ||
| 164 | |||
| 165 | return $messages; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub rule_executable_command { | ||
| 169 | my ($command) = @_; | ||
| 170 | |||
| 171 | my ($file) = split (/ /, $command); | ||
| 172 | |||
| 173 | if ( not -f $file ) { | ||
| 174 | foreach my $p ( split (/:/, $ENV{PATH}) ) { | ||
| 175 | $file = "$p/$file",last if -x "$p/$file"; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | if ( -x $file ) { | ||
| 180 | return ""; | ||
| 181 | } else { | ||
| 182 | return "{field} not found" if not -e $file; | ||
| 183 | return "{field} not executable"; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | 1; | ||
| 188 | |||
| 189 | __END__ |