| 1 | package QBit::Validator::Type::variable; | ||
| 2 | $QBit::Validator::Type::variable::VERSION = '0.012'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Validator::Type); | ||
| 6 | |||
| 7 | use QBit::Validator; | ||
| 8 | |||
| 9 | use Exception::Validator; | ||
| 10 | use Exception::Validator::FailedField; | ||
| 11 | |||
| 12 | #order is important | ||
| 13 | sub get_options_name { | ||
| 14 | qw(type conditions); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub type {()} | ||
| 18 | |||
| 19 | sub conditions { | ||
| 20 | my ($qv, $conditions) = @_; | ||
| 21 | |||
| 22 | throw Exception::Validator gettext('Option "%s" must be a not empty ARRAY', 'conditions') | ||
| 23 | if ref($conditions) ne 'ARRAY' or !@$conditions; | ||
| 24 | |||
| 25 | my $count = 1; | ||
| 26 | my $all = @$conditions; | ||
| 27 | |||
| 28 | my @result = (); | ||
| 29 | foreach my $condition (@$conditions) { | ||
| 30 | my $is_last_condition = $all == $count++; | ||
| 31 | |||
| 32 | if (exists($condition->{'if'}) && exists($condition->{'else'}) && !$is_last_condition) { | ||
| 33 | throw Exception::Validator gettext('Option "else" must only be in the last condition'); | ||
| 34 | } | ||
| 35 | |||
| 36 | push(@result, _get_check_by_condition($qv, $condition, $is_last_condition)); | ||
| 37 | } | ||
| 38 | |||
| 39 | return @result; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _get_check_by_condition { | ||
| 43 | my ($qv, $condition, $is_last_condition) = @_; | ||
| 44 | |||
| 45 | if (exists($condition->{'if'})) { | ||
| 46 | my $if = $condition->{'if'}; | ||
| 47 | |||
| 48 | my $check = _get_check($qv, $if); | ||
| 49 | |||
| 50 | my ($then, $else); | ||
| 51 | if ($condition->{'then'}) { | ||
| 52 | $then = QBit::Validator->new( | ||
| 53 | template => $condition->{'then'}, | ||
| 54 | parent => $qv->parent // $qv, | ||
| 55 | path => $qv->path | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | if ($condition->{'else'}) { | ||
| 60 | $else = QBit::Validator->new( | ||
| 61 | template => $condition->{'else'}, | ||
| 62 | parent => $qv->parent // $qv, | ||
| 63 | path => $qv->path | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | |||
| 67 | return sub { | ||
| 68 | my $validator = $check->(@_); | ||
| 69 | |||
| 70 | if ($validator->has_errors) { | ||
| 71 | if (defined($else)) { | ||
| 72 | if ($else->_validate($_[1])) { | ||
| 73 | return FALSE; | ||
| 74 | } else { | ||
| 75 | throw FF $else->get_errors; | ||
| 76 | } | ||
| 77 | } else { | ||
| 78 | throw FF $validator->get_errors if $is_last_condition; | ||
| 79 | } | ||
| 80 | } else { | ||
| 81 | if (defined($then)) { | ||
| 82 | if ($then->_validate($_[1])) { | ||
| 83 | return FALSE; | ||
| 84 | } else { | ||
| 85 | throw FF $then->get_errors; | ||
| 86 | } | ||
| 87 | } else { | ||
| 88 | # exit with OK | ||
| 89 | return FALSE; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | return TRUE; | ||
| 94 | } | ||
| 95 | } else { | ||
| 96 | #['/key' => {...}] | ||
| 97 | #['' => {...}] | ||
| 98 | #{min => 1} | ||
| 99 | |||
| 100 | my $check = _get_check($qv, $condition); | ||
| 101 | |||
| 102 | if ($is_last_condition) { | ||
| 103 | return sub { | ||
| 104 | my $validator = $check->(@_); | ||
| 105 | |||
| 106 | throw FF $validator->get_errors if $validator->has_errors; | ||
| 107 | |||
| 108 | # exit with OK | ||
| 109 | return FALSE; | ||
| 110 | } | ||
| 111 | } else { | ||
| 112 | return sub { | ||
| 113 | my $validator = $check->(@_); | ||
| 114 | |||
| 115 | return $validator->has_errors; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _get_check { | ||
| 122 | my ($qv, $condition) = @_; | ||
| 123 | |||
| 124 | my $ref = ref($condition); | ||
| 125 | |||
| 126 | my $parent = $qv->parent // $qv; | ||
| 127 | |||
| 128 | my $validator = QBit::Validator->new( | ||
| 129 | template => $ref eq 'ARRAY' ? $condition->[1] : $condition, | ||
| 130 | parent => $parent, | ||
| 131 | path => $qv->path, | ||
| 132 | ); | ||
| 133 | |||
| 134 | if ($ref eq 'ARRAY' && $condition->[0] ne '') { | ||
| 135 | #check field | ||
| 136 | |||
| 137 | my $data = $qv->data; | ||
| 138 | my $path_manager = $parent->path_manager(); | ||
| 139 | |||
| 140 | return sub { | ||
| 141 | my $condition_path = $path_manager->get_absolute_path($condition->[0], $qv->path); | ||
| 142 | |||
| 143 | my $data_to_check = | ||
| 144 | $path_manager->get_data_by_path($path_manager->get_current_node_path($condition_path), $data); | ||
| 145 | |||
| 146 | $validator->_validate($data_to_check); | ||
| 147 | |||
| 148 | return $validator; | ||
| 149 | } | ||
| 150 | } else { | ||
| 151 | #check current node | ||
| 152 | |||
| 153 | return sub { | ||
| 154 | $validator->_validate($_[1]); | ||
| 155 | |||
| 156 | return $validator; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub init { | ||
| 162 | my ($self) = @_; | ||
| 163 | |||
| 164 | $self->SUPER::init(); | ||
| 165 | |||
| 166 | foreach ($self->get_options_name) { | ||
| 167 | $self->{$_} = \&$_; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | TRUE; |