| 1 | package QBit::Validator::Type::scalar; | ||
| 2 | $QBit::Validator::Type::scalar::VERSION = '0.012'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Validator::Type); | ||
| 6 | |||
| 7 | use Exception::Validator; | ||
| 8 | use Exception::Validator::FailedField; | ||
| 9 | |||
| 10 | #order is important | ||
| 11 | sub get_options_name { | ||
| 12 | qw(type eq regexp min max len_min len len_max in); | ||
| 13 | } | ||
| 14 | |||
| 15 | sub type { | ||
| 16 | my ($qv, $types) = @_; | ||
| 17 | |||
| 18 | return sub { | ||
| 19 | throw FF gettext('Data must be SCALAR') if ref($_[1]); | ||
| 20 | |||
| 21 | return TRUE; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | sub eq { | ||
| 26 | my ($qv, $val) = @_; | ||
| 27 | |||
| 28 | if (defined($val)) { | ||
| 29 | throw Exception::Validator gettext('Option "%s" must be numeric', 'eq') | ||
| 30 | unless looks_like_number($val); | ||
| 31 | |||
| 32 | return sub { | ||
| 33 | throw FF gettext('Data must be defined') unless defined($_[1]); | ||
| 34 | |||
| 35 | throw FF gettext('Data must be numeric, but got "%s"', $_[1]) | ||
| 36 | unless looks_like_number($_[1]); | ||
| 37 | |||
| 38 | throw FF gettext('Got value "%s" not equal "%s"', $_[1], $val) unless $_[1] == $val; | ||
| 39 | |||
| 40 | return TRUE; | ||
| 41 | }; | ||
| 42 | } else { | ||
| 43 | return sub { | ||
| 44 | throw FF gettext('Data must be null') if defined($_[1]); | ||
| 45 | |||
| 46 | return TRUE; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | sub regexp { | ||
| 52 | my ($qv, $regexp) = @_; | ||
| 53 | |||
| 54 | throw Exception::Validator gettext('Option "%s" must be type "Regexp"', 'regexp') | ||
| 55 | if !defined($regexp) || ref($regexp) ne 'Regexp'; | ||
| 56 | |||
| 57 | return sub { | ||
| 58 | throw FF gettext('Got value "%s" do not fit the regular expression', $_[1]) if $_[1] !~ $regexp; | ||
| 59 | |||
| 60 | return TRUE; | ||
| 61 | }; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub min { | ||
| 65 | my ($qv, $min) = @_; | ||
| 66 | |||
| 67 | throw Exception::Validator gettext('Option "%s" must be numeric', 'min') | ||
| 68 | unless looks_like_number($min); | ||
| 69 | |||
| 70 | return sub { | ||
| 71 | throw FF gettext('The data must be numeric, but got "%s"', $_[1]) unless looks_like_number($_[1]); | ||
| 72 | |||
| 73 | throw FF gettext('Got value "%s" less then "%s"', $_[1], $min) if $_[1] < $min; | ||
| 74 | |||
| 75 | return TRUE; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | sub max { | ||
| 80 | my ($qv, $max) = @_; | ||
| 81 | |||
| 82 | throw Exception::Validator gettext('Option "%s" must be numeric', 'max') | ||
| 83 | unless looks_like_number($max); | ||
| 84 | |||
| 85 | return sub { | ||
| 86 | throw FF gettext('The data must be numeric, but got "%s"', $_[1]) unless looks_like_number($_[1]); | ||
| 87 | |||
| 88 | throw FF gettext('Got value "%s" more than "%s"', $_[1], $max) if $_[1] > $max; | ||
| 89 | |||
| 90 | return TRUE; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | sub len_min { | ||
| 95 | my ($qv, $len_min) = @_; | ||
| 96 | |||
| 97 | throw Exception::Validator gettext('Option "%s" must be positive number', 'len_min') | ||
| 98 | if !defined($len_min) || $len_min !~ /\A[0-9]+\z/; | ||
| 99 | |||
| 100 | return sub { | ||
| 101 | throw FF gettext('Length "%s" less then "%s"', $_[1], $len_min) if length($_[1]) < $len_min; | ||
| 102 | |||
| 103 | return TRUE; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | sub len { | ||
| 108 | my ($qv, $len) = @_; | ||
| 109 | |||
| 110 | throw Exception::Validator gettext('Option "%s" must be positive number', 'len_max') | ||
| 111 | if !defined($len) || $len !~ /\A[0-9]+\z/; | ||
| 112 | |||
| 113 | return sub { | ||
| 114 | throw FF gettext('Length "%s" not equal "%s"', $_[1], $len) unless length($_[1]) == $len; | ||
| 115 | |||
| 116 | return TRUE; | ||
| 117 | }; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub len_max { | ||
| 121 | my ($qv, $len_max) = @_; | ||
| 122 | |||
| 123 | throw Exception::Validator gettext('Option "%s" must be positive number', 'len_max') | ||
| 124 | if !defined($len_max) || $len_max !~ /\A[0-9]+\z/; | ||
| 125 | |||
| 126 | return sub { | ||
| 127 | throw FF gettext('Length "%s" more than "%s"', $_[1], $len_max) if length($_[1]) > $len_max; | ||
| 128 | |||
| 129 | return TRUE; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | sub in { | ||
| 134 | my ($qv, $in) = @_; | ||
| 135 | |||
| 136 | throw Exception::Validator gettext('Key "%s" must be defined', 'in') unless defined($in); | ||
| 137 | |||
| 138 | if (ref($in) eq '') { | ||
| 139 | return sub { | ||
| 140 | throw FF gettext('Got value "%s" not in array: %s', $_[1], $in) unless $_[1] eq $in; | ||
| 141 | |||
| 142 | return TRUE; | ||
| 143 | }; | ||
| 144 | } elsif (ref($in) eq 'ARRAY') { | ||
| 145 | my %dictionary = (); | ||
| 146 | my $dictionary_str = join(', ', map {$dictionary{$_} = TRUE; $_} @$in); | ||
| 147 | |||
| 148 | sub { | ||
| 149 | throw FF gettext('Got value "%s" not in array: %s', $_[1], $dictionary_str) unless $dictionary{$_[1]}; | ||
| 150 | |||
| 151 | return TRUE; | ||
| 152 | } | ||
| 153 | } else { | ||
| 154 | throw Exception::Validator gettext('Key "%s" must be SCALAR or ARRAY', 'in') unless defined($in); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | sub init { | ||
| 159 | my ($self) = @_; | ||
| 160 | |||
| 161 | $self->SUPER::init(); | ||
| 162 | |||
| 163 | foreach ($self->get_options_name) { | ||
| 164 | $self->{$_} = \&$_; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | TRUE; |