QBit/Validator.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 269 CC 27 SCAR 35.5
CC
27
Cov
0%
init
90.0
_get_type_and_template
72.0
_get_nodes
20.0
CRAP 34.7
1 package QBit::Validator;
2 $QBit::Validator::VERSION = '0.012';
3 use qbit;
4  
5 use base qw(QBit::Class);
6  
7 use Exception::Validator;
8  
9 __PACKAGE__->mk_ro_accessors(qw(app parent path));
10  
11 __PACKAGE__->mk_accessors(qw(template));
12  
13 my %AVAILABLE_FIELDS = map {$_ => TRUE} qw(data template app throw pre_run path path_manager parent sys_errors_handler);
14  
15 our $PATH_MANAGER = 'QBit::Validator::PathManager';
16 our $SYS_ERRORS_HANDLER = sub { };
17  
18 sub init {
19 my ($self) = @_;
20  
21 foreach (qw(template)) {
22 throw Exception::Validator gettext('Expected "%s"', $_) unless exists($self->{$_});
23 }
24  
25 throw Exception::Validator gettext('Option "parent" must be QBit::Validator')
26 if defined($self->parent) && (!blessed($self->parent) || !$self->parent->isa('QBit::Validator'));
27  
28 my @bad_fields = grep {!$AVAILABLE_FIELDS{$_}} keys(%{$self});
29 throw Exception::Validator gettext('Unknown options: %s', join(', ', @bad_fields))
30 if @bad_fields;
31  
32 if (exists($self->{'pre_run'})) {
33 throw Exception::Validator gettext('Option "pre_run" must be code')
34 if !defined($self->{'pre_run'}) || ref($self->{'pre_run'}) ne 'CODE';
35  
36 $self->{'pre_run'}($self);
37 }
38  
39 local $SYS_ERRORS_HANDLER = $self->{'sys_errors_handler'} if exists($self->{'sys_errors_handler'});
40  
41 $self->_init_template();
42  
43 $self->validate($self->data) if exists($self->{'data'});
44  
45 $self->throw_exception() if $self->has_errors && $self->{'throw'};
46 }
47  
48 sub use_errors_handler {
49 my ($self, $error) = @_;
50  
51 $SYS_ERRORS_HANDLER->($error);
52 }
53  
54 sub _init_template {
55 my ($self) = @_;
56  
57 $self->{'__CHECKS__'} = [];
58  
59 my $template = $self->template;
60  
61 throw Exception::Validator gettext('Key "%s" must be HASH', 'template')
62 if !defined($template) || ref($template) ne 'HASH';
63  
64 my ($type_obj, $final_template) = $self->_get_type_and_template($template);
65  
66 push(@{$self->{'__CHECKS__'}}, $type_obj->get_checks_by_template($self, $final_template, []));
67 }
68  
69 sub data {
70 my ($self, @params) = @_;
71  
72 if (@params) {
73 $self->{'data'} = $params[0];
74 }
75  
76 my $parent = $self->parent // $self;
77  
78 return $parent->{'data'};
79 }
80  
81 sub _get_type_and_template {
82 my ($self, $template) = @_;
83  
84 my $exists_check = exists($template->{'check'});
85 my $check = delete($template->{'check'});
86  
87 throw Exception::Validator gettext('Option "%s" must be defined', 'check')
88 if $exists_check && !defined($check);
89  
90 my $type = delete($template->{'type'}) // 'scalar';
91  
92 my $type_obj = $self->_get_type_by_name($type);
93  
94 if ($type_obj->can('get_template')) {
95 my $base_template = $type_obj->get_template();
96  
97 if (exists($base_template->{'check'}) && ref($base_template->{'check'}) ne 'ARRAY') {
98 $base_template->{'check'} = [$base_template->{'check'}];
99 }
100  
101 $template = {%$base_template, %$template};
102  
103 push(@{$template->{'check'}}, ref($check) eq 'ARRAY' ? @$check : $check) if defined($check);
104  
105 return $self->_get_type_and_template($template);
106 } else {
107 $template->{'type'} = $type;
108  
109 push(@{$template->{'check'}}, ref($check) eq 'ARRAY' ? @$check : $check) if defined($check);
110  
111 return ($type_obj, $template);
112 }
113 }
114  
115 sub validate {
116 my ($self, $data) = @_;
117  
118 $self->data($data);
119  
120 return $self->_validate($data);
121 }
122  
123 sub _validate {
124 my ($self, $data) = @_;
125  
126 delete($self->{'__ERRORS__'});
127  
128 my @checks = @{$self->{'__CHECKS__'}};
129  
130 my $res = TRUE;
131 try {
132 foreach my $check (@checks) {
133 last unless $check->($self, $data);
134 }
135 }
136 catch {
137 my ($exception) = @_;
138  
139 my $error;
140 if ($exception->{'check_error'} || !$self->{'__CUSTOM_ERRORS__'}) {
141 $error = $exception->message;
142 } else {
143 $error = $self->{'__CUSTOM_ERRORS__'};
144 }
145  
146 $self->{'__ERRORS__'} = $error;
147  
148 $res = FALSE;
149 };
150  
151 return $res;
152 }
153  
154 sub get_errors {$_[0]->{'__ERRORS__'}}
155  
156 sub _get_type_by_name {
157 my ($self, $type_name) = @_;
158  
159 my $package_stash = package_stash(ref($self));
160  
161 unless (exists($package_stash->{'__TYPES__'}{$type_name})) {
162 my $type_class = 'QBit::Validator::Type::' . $type_name;
163 require_class($type_class);
164  
165 $package_stash->{'__TYPES__'}{$type_name} = $type_class->new();
166 }
167  
168 return $package_stash->{'__TYPES__'}{$type_name};
169 }
170  
171 sub throw_exception {
172 my ($self) = @_;
173  
174 throw Exception::Validator $self->get_all_errors;
175 }
176  
177 sub get_all_errors {
178 my ($self) = @_;
179  
180 my $error = '';
181  
182 $error .= join("\n", map {$_->{'message'}} $self->get_fields_with_error());
183  
184 return $error;
185 }
186  
187 sub get_error {
188 my $error = $_[0]->path_manager->get_data_by_path($_[1] // $_[0]->path_manager->root, $_[0]->get_errors);
189  
190 return ref($error) eq '' ? $error : undef;
191 }
192  
193 sub get_fields_with_error {
194 my ($self) = @_;
195  
196 my $errors = $self->get_errors;
197  
198 if (defined($errors)) {
199 my $path_manager = $self->path_manager;
200  
201 return _get_nodes($path_manager, $path_manager->root, $errors);
202 } else {
203 return ();
204 }
205 }
206  
207 sub _get_nodes {
208 my ($path_manager, $root_path, $data) = @_;
209  
210 if (ref($data) eq 'HASH') {
211 return map {
212 _get_nodes($path_manager,
213 $path_manager->concatenate($root_path, $path_manager->delimiter, $path_manager->hash_path($_)),
214 $data->{$_})
215 }
216 sort keys(%$data);
217 } elsif (ref($data) eq 'ARRAY') {
218 my $i = 0;
219 my @result = ();
220 while ($i < @$data) {
221 if (defined($data->[$i])) {
222 push(
223 @result,
224 _get_nodes(
225 $path_manager,
226 $path_manager->concatenate($root_path, $path_manager->delimiter, $path_manager->array_path($i)),
227 $data->[$i]
228 )
229 );
230 }
231  
232 $i++;
233 }
234  
235 return @result;
236 } else {
237 return {path => $root_path, message => $data};
238 }
239 }
240  
241 sub has_error {defined($_[0]->get_error($_[1]))}
242  
243 sub has_errors {
244 return defined($_[0]->get_errors);
245 }
246  
247 sub path_manager {
248 my ($self) = @_;
249  
250 unless ($self->{'path_manager'}) {
251 require_class($PATH_MANAGER);
252  
253 $self->{'path_manager'} = $PATH_MANAGER->new();
254 }
255  
256 return $self->{'path_manager'};
257 }
258  
259 TRUE;
260  
261 __END__