| 1 | package QBit::Validator::Type::array; | ||
| 2 | $QBit::Validator::Type::array::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 size_min size size_max all contents); | ||
| 13 | } | ||
| 14 | |||
| 15 | sub pre_process_template { | ||
| 16 | my ($self, $template) = @_; | ||
| 17 | |||
| 18 | throw Exception::Validator gettext('Options "all" and "contents" can not be used together') | ||
| 19 | if exists($template->{'all'}) && exists($template->{'contents'}); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub type { | ||
| 23 | return sub { | ||
| 24 | throw gettext('Data must be ARRAY') unless ref($_[1]) eq 'ARRAY'; | ||
| 25 | |||
| 26 | return TRUE; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | sub size_min { | ||
| 31 | my ($qv, $size_min) = @_; | ||
| 32 | |||
| 33 | throw Exception::Validator gettext('Option "%s" must be positive number', 'size_min') | ||
| 34 | if !defined($size_min) || $size_min !~ /\A[0-9]+\z/; | ||
| 35 | |||
| 36 | return sub { | ||
| 37 | throw FF gettext('Data size "%s" less then "%s"', scalar(@{$_[1]}), $size_min) if @{$_[1]} < $size_min; | ||
| 38 | |||
| 39 | return TRUE; | ||
| 40 | }; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub size { | ||
| 44 | my ($qv, $size) = @_; | ||
| 45 | |||
| 46 | throw Exception::Validator gettext('Option "%s" must be positive number', 'size') | ||
| 47 | if !defined($size) || $size !~ /\A[0-9]+\z/; | ||
| 48 | |||
| 49 | return sub { | ||
| 50 | throw FF gettext('Data size "%s" not equal "%s"', scalar(@{$_[1]}), $size) unless @{$_[1]} == $size; | ||
| 51 | |||
| 52 | return TRUE; | ||
| 53 | }; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub size_max { | ||
| 57 | my ($qv, $size_max) = @_; | ||
| 58 | |||
| 59 | throw Exception::Validator gettext('Option "%s" must be positive number', 'size_max') | ||
| 60 | if !defined($size_max) || $size_max !~ /\A[0-9]+\z/; | ||
| 61 | |||
| 62 | return sub { | ||
| 63 | throw FF gettext('Data size "%s" more than "%s"', scalar(@{$_[1]}), $size_max) if @{$_[1]} > $size_max; | ||
| 64 | |||
| 65 | return TRUE; | ||
| 66 | }; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub all { | ||
| 70 | my ($qv, $template) = @_; | ||
| 71 | |||
| 72 | my $parent = $qv->parent // $qv; | ||
| 73 | my $path_manager = $parent->path_manager(); | ||
| 74 | |||
| 75 | my $validator = QBit::Validator->new( | ||
| 76 | template => $template, | ||
| 77 | parent => $parent, | ||
| 78 | path => $path_manager->get_absolute_path($path_manager->get_path_part('array', '%d'), $qv->path), | ||
| 79 | ); | ||
| 80 | |||
| 81 | return sub { | ||
| 82 | my $errors = []; | ||
| 83 | |||
| 84 | my $num = 0; | ||
| 85 | $path_manager->set_dynamic_part(\$num); | ||
| 86 | foreach (@{$_[1]}) { | ||
| 87 | unless ($validator->_validate($_)) { | ||
| 88 | $errors->[$num] = $validator->get_errors; | ||
| 89 | } | ||
| 90 | |||
| 91 | $num++; | ||
| 92 | } | ||
| 93 | $path_manager->reset_dynamic_part(); | ||
| 94 | |||
| 95 | throw FF $errors if @$errors; | ||
| 96 | |||
| 97 | return TRUE; | ||
| 98 | }; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub contents { | ||
| 102 | my ($qv, $templates) = @_; | ||
| 103 | |||
| 104 | throw Exception::Validator gettext('Option "%s" must be ARRAY', 'contents') | ||
| 105 | if !defined($templates) || ref($templates) ne 'ARRAY'; | ||
| 106 | |||
| 107 | my $parent = $qv->parent // $qv; | ||
| 108 | my $path_manager = $parent->path_manager; | ||
| 109 | |||
| 110 | my $path = $qv->path; | ||
| 111 | |||
| 112 | my @validators = (); | ||
| 113 | my $i = 0; | ||
| 114 | foreach my $template (@$templates) { | ||
| 115 | my $validator = QBit::Validator->new( | ||
| 116 | template => $template, | ||
| 117 | parent => $parent, | ||
| 118 | path => $path_manager->get_absolute_path($path_manager->get_path_part('array', $i), $path), | ||
| 119 | ); | ||
| 120 | |||
| 121 | push(@validators, $validator); | ||
| 122 | |||
| 123 | $i++; | ||
| 124 | } | ||
| 125 | |||
| 126 | return sub { | ||
| 127 | throw FF gettext('Data size "%s" no equal "%s"', scalar(@{$_[1]}), scalar(@validators)) | ||
| 128 | unless @{$_[1]} == @validators; | ||
| 129 | |||
| 130 | my $errors = []; | ||
| 131 | my $num = 0; | ||
| 132 | foreach (@{$_[1]}) { | ||
| 133 | unless ($validators[$num]->_validate($_)) { | ||
| 134 | $errors->[$num] = $validators[$num]->get_errors; | ||
| 135 | } | ||
| 136 | |||
| 137 | $num++; | ||
| 138 | } | ||
| 139 | |||
| 140 | throw FF $errors if @$errors; | ||
| 141 | |||
| 142 | return TRUE; | ||
| 143 | }; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub init { | ||
| 147 | my ($self) = @_; | ||
| 148 | |||
| 149 | $self->SUPER::init(); | ||
| 150 | |||
| 151 | foreach ($self->get_options_name) { | ||
| 152 | $self->{$_} = \&$_; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | TRUE; |