File Coverage

blib/lib/Mo/utils/Array.pm
Criterion Covered Total %
statement 74 74 100.0
branch 26 26 100.0
condition n/a
subroutine 14 14 100.0
pod 5 5 100.0
total 119 119 100.0


line stmt bran cond sub pod time code
1             package Mo::utils::Array;
2              
3 7     7   230118 use base qw(Exporter);
  7         12  
  7         839  
4 7     7   51 use strict;
  7         24  
  7         146  
5 7     7   26 use warnings;
  7         18  
  7         358  
6              
7 7     7   4776 use Error::Pure qw(err);
  7         33909  
  7         148  
8 7     7   534 use List::Util 1.33 qw(none);
  7         151  
  7         906  
9 7     7   7241 use Mo::utils::common qw(check_object);
  7         5304  
  7         146  
10 7     7   414 use Readonly;
  7         14  
  7         301  
11 7     7   48 use Scalar::Util qw(blessed);
  7         14  
  7         7311  
12              
13             Readonly::Array our @EXPORT_OK => qw(check_array check_array_items check_array_object
14             check_array_required check_array_strings);
15              
16             our $VERSION = 0.04;
17              
18             sub check_array {
19 24     24 1 266683 my ($self, $key) = @_;
20              
21 24 100       71 if (! exists $self->{$key}) {
22 1         2 return;
23             }
24              
25 23 100       81 if (ref $self->{$key} ne 'ARRAY') {
26 9         20 my $ref = ref $self->{$key};
27             err "Parameter '".$key."' must be a array.",
28 9 100       66 'Value', $self->{$key},
29             'Reference', ($ref eq '' ? 'SCALAR' : $ref),
30             ;
31             }
32              
33 14         30 return;
34             }
35              
36             sub check_array_items {
37 5     5 1 363450 my ($self, $key, $max_items) = @_;
38              
39 5 100       18 if (! exists $self->{$key}) {
40 1         4 return;
41             }
42              
43 4         15 check_array($self, $key);
44              
45 3 100       4 if (@{$self->{$key}} > $max_items) {
  3         9  
46             err "Parameter '".$key."' has more items than expected.",
47             'Maximum items', $max_items,
48 1         3 'Number of items', (scalar @{$self->{$key}}),
  1         6  
49             ;
50             }
51              
52 2         6 return;
53             }
54              
55             sub check_array_object {
56 6     6 1 360165 my ($self, $key, $class) = @_;
57              
58 6 100       27 if (! exists $self->{$key}) {
59 1         4 return;
60             }
61              
62 5         21 check_array($self, $key);
63              
64 4         9 foreach my $obj (@{$self->{$key}}) {
  4         11  
65 4         19 check_object($obj, $class,
66             "Parameter '%s' with array must contain '%s' objects.",
67             [$key, $class],
68             );
69             }
70              
71 1         13 return;
72             }
73              
74             sub check_array_required {
75 5     5 1 337010 my ($self, $key) = @_;
76              
77 5 100       16 if (! exists $self->{$key}) {
78 1         5 err "Parameter '$key' is required.";
79             }
80              
81 4         11 check_array($self, $key);
82              
83 2 100       2 if (! @{$self->{$key}}) {
  2         5  
84 1         5 err "Parameter '".$key."' with array must have at least one item.";
85             }
86              
87 1         2 return;
88             }
89              
90             sub check_array_strings {
91 9     9 1 249911 my ($self, $key, $strings_ar) = @_;
92              
93 9 100       36 if (! exists $self->{$key}) {
94 1         5 return;
95             }
96              
97 8 100       20 if (! defined $strings_ar) {
98 1         5 err "Parameter '$key' must have strings definition.";
99             }
100 7 100       22 if (ref $strings_ar ne 'ARRAY') {
101 1         19 err "Parameter '$key' must have right string definition.";
102             }
103              
104 6         21 check_array($self, $key);
105              
106 4         7 foreach my $value (@{$self->{$key}}) {
  4         12  
107 5 100       33 if (ref $value ne '') {
108 1         7 err "Parameter '$key' must contain a list of strings.",
109             'Value', $value,
110             'Reference', (ref $value),
111             ;
112             }
113 4 100   6   13 if (none { $value eq $_ } @{$strings_ar}) {
  6         47  
  4         17  
114             err "Parameter '$key' must be one of the defined strings.",
115             'Value', $value,
116 1         10 'Possible strings', "'".(join "', '", @{$strings_ar})."'",
  1         3  
117             ;
118             }
119             }
120              
121 2         12 return;
122             }
123              
124             1;
125              
126             __END__
127              
128             =pod
129              
130             =encoding utf8
131              
132             =head1 NAME
133              
134             Mo::utils::Array - Mo array utilities.
135              
136             =head1 SYNOPSIS
137              
138             use Mo::utils::Array qw(check_array check_array_items check_array_object check_array_required check_array_strings);
139              
140             check_array($self, $key);
141             check_array_items($self, $key, $max_items);
142             check_array_object($self, $key, $class);
143             check_array_required($self, $key);
144             check_array_strings($self, $key, $strings_ar);
145              
146             =head1 DESCRIPTION
147              
148             Mo array utilities for checking of data objects.
149              
150             =head1 SUBROUTINES
151              
152             =head2 C<check_array>
153              
154             check_array($self, $key);
155              
156             I<Since version 0.01.>
157              
158             Check parameter defined by C<$key> which is reference to array.
159              
160             Put error if check isn't ok.
161              
162             Returns undef.
163              
164             =head2 C<check_array_items>
165              
166             check_array_items($self, $key, $max_items);
167              
168             I<Since version 0.03.>
169              
170             Check parameter defined by C<$key> which is reference to array for number of
171             items inside.
172              
173             Put error if check isn't ok.
174              
175             Returns undef.
176              
177             =head2 C<check_array_object>
178              
179             check_array_object($self, $key, $class);
180              
181             I<Since version 0.01.>
182              
183             Check parameter defined by C<$key> which is reference to array with instances
184             of some object type (C<$class>).
185              
186             Put error if check isn't ok.
187              
188             Returns undef.
189              
190             =head2 C<check_array_required>
191              
192             check_array_required($self, $key);
193              
194             I<Since version 0.01.>
195              
196             Check parameter defined by C<$key> which is reference to array for at least one
197             value inside.
198              
199             Put error if check isn't ok.
200              
201             Returns undef.
202              
203             =head2 C<check_array_strings>
204              
205             check_array_strings($self, $key, $strings_ar);
206              
207             I<Since version 0.02.>
208              
209             Check parameter defined by C<$key> which is reference to array with strings
210             defined by C<$strings_ar> which is reference to array.
211              
212             Put error if check isn't ok.
213              
214             Returns undef.
215              
216             =head1 ERRORS
217              
218             check_array():
219             Parameter '%s' must be a array.
220             Value: %s
221             Reference: %s
222              
223             check_array_items():
224             Parameter '%s' must be a array.
225             Value: %s
226             Reference: %s
227             Parameter '%s' has more items than expected.
228             Maximum items: %s
229             Number of items: %s
230              
231             check_array_object():
232             Parameter '%s' must be a array.
233             Value: %s
234             Reference: %s
235             Parameter '%s' with array must contain '%s' objects.
236             Value: %s
237             Reference: %s
238              
239             check_array_required():
240             Parameter '%s' is required.
241             Parameter '%s' must be a array.
242             Value: %s
243             Reference: %s
244             Parameter '%s' with array must have at least one item.
245              
246             check_array_strings():
247             Parameter '%s' must be a array.
248             Value: %s
249             Reference: %s
250             Parameter '%s' must be one of the defined strings.
251             Value: %s
252             Possible strings: %s
253             Parameter '%s' must contain a list of strings.
254             Value: %s
255             Reference: %s
256             Parameter '%s' must have right string definition.
257             Parameter '%s' must have strings definition.
258              
259             =head1 EXAMPLE1
260              
261             =for comment filename=check_array_ok.pl
262              
263             use strict;
264             use warnings;
265              
266             use Mo::utils::Array qw(check_array);
267              
268             my $self = {
269             'key' => ['foo'],
270             };
271             check_array($self, 'key');
272              
273             # Print out.
274             print "ok\n";
275              
276             # Output:
277             # ok
278              
279             =head1 EXAMPLE2
280              
281             =for comment filename=check_array_fail.pl
282              
283             use strict;
284             use warnings;
285              
286             use Error::Pure;
287             use Mo::utils::Array qw(check_array);
288              
289             $Error::Pure::TYPE = 'Error';
290              
291             my $self = {
292             'key' => 'foo',
293             };
294             check_array($self, 'key');
295              
296             # Print out.
297             print "ok\n";
298              
299             # Output like:
300             # #Error [..Array.pm:?] Parameter 'key' must be a array.
301              
302             =head1 EXAMPLE3
303              
304             =for comment filename=check_array_items_ok.pl
305              
306             use strict;
307             use warnings;
308              
309             use Mo::utils::Array qw(check_array_items);
310              
311             my $self = {
312             'key' => ['foo'],
313             };
314             check_array_items($self, 'key', 3);
315              
316             # Print out.
317             print "ok\n";
318              
319             # Output:
320             # ok
321              
322             =head1 EXAMPLE4
323              
324             =for comment filename=check_array_items_fail.pl
325              
326             use strict;
327             use warnings;
328              
329             use Error::Pure;
330             use Mo::utils::Array qw(check_array_items);
331              
332             $Error::Pure::TYPE = 'Error';
333              
334             my $self = {
335             'key' => ['foo', 'bar', 'baz'],
336             };
337             check_array_items($self, 'key', 2);
338              
339             # Print out.
340             print "ok\n";
341              
342             # Output like:
343             # #Error [..Array.pm:?] Parameter 'key' has more items than expected.
344              
345             =head1 EXAMPLE5
346              
347             =for comment filename=check_array_object_ok.pl
348              
349             use strict;
350             use warnings;
351              
352             use Mo::utils::Array qw(check_array_object);
353             use Test::MockObject;
354              
355             my $self = {
356             'key' => [
357             Test::MockObject->new,
358             ],
359             };
360             check_array_object($self, 'key', 'Test::MockObject');
361              
362             # Print out.
363             print "ok\n";
364              
365             # Output:
366             # ok
367              
368             =head1 EXAMPLE6
369              
370             =for comment filename=check_array_object_fail.pl
371              
372             use strict;
373             use warnings;
374              
375             use Error::Pure;
376             use Mo::utils::Array qw(check_array_object);
377              
378             $Error::Pure::TYPE = 'Error';
379              
380             my $self = {
381             'key' => [
382             'foo',
383             ],
384             };
385             check_array_object($self, 'key', 'Test::MockObject');
386              
387             # Print out.
388             print "ok\n";
389              
390             # Output like:
391             # #Error [..Array.pm:?] Parameter 'key' with array must contain 'Test::MockObject' objects.
392              
393             =head1 EXAMPLE7
394              
395             =for comment filename=check_array_required_ok.pl
396              
397             use strict;
398             use warnings;
399              
400             use Mo::utils::Array qw(check_array_required);
401              
402             my $self = {
403             'key' => ['value'],
404             };
405             check_array_required($self, 'key');
406              
407             # Print out.
408             print "ok\n";
409              
410             # Output:
411             # ok
412              
413             =head1 EXAMPLE8
414              
415             =for comment filename=check_array_required_fail.pl
416              
417             use strict;
418             use warnings;
419              
420             use Error::Pure;
421             use Mo::utils::Array qw(check_array_required);
422              
423             $Error::Pure::TYPE = 'Error';
424              
425             my $self = {
426             'key' => [],
427             };
428             check_array_required($self, 'key');
429              
430             # Print out.
431             print "ok\n";
432              
433             # Output like:
434             # #Error [..Array.pm:?] Parameter 'key' with array must have at least one item.
435              
436             =head1 EXAMPLE9
437              
438             =for comment filename=check_array_strings_ok.pl
439              
440             use strict;
441             use warnings;
442              
443             use Mo::utils::Array qw(check_array_strings);
444              
445             my $self = {
446             'key' => ['value'],
447             };
448             check_array_strings($self, 'key', ['value']);
449              
450             # Print out.
451             print "ok\n";
452              
453             # Output:
454             # ok
455              
456             =head1 EXAMPLE10
457              
458             =for comment filename=check_array_strings_fail.pl
459              
460             use strict;
461             use warnings;
462              
463             use Error::Pure;
464             use Mo::utils::Array qw(check_array_strings);
465              
466             $Error::Pure::TYPE = 'Error';
467              
468             my $self = {
469             'key' => ['bad'],
470             };
471             check_array_strings($self, 'key', ['value']);
472              
473             # Print out.
474             print "ok\n";
475              
476             # Output like:
477             # #Error [..Array.pm:?] Parameter 'key' must be one of the defined strings.
478              
479             =head1 DEPENDENCIES
480              
481             L<Exporter>,
482             L<Error::Pure>,
483             L<List::Util>,
484             L<Mo::utils::commons>,
485             L<Readonly>,
486             L<Scalar::Util>.
487              
488             =head1 SEE ALSO
489              
490             =over
491              
492             =item L<Mo>
493              
494             Micro Objects. Mo is less.
495              
496             =item L<Mo::utils>
497              
498             Mo utilities.
499              
500             =item L<Mo::utils::Hash>
501              
502             Mo hash utilities.
503              
504             =item L<Mo::utils::Language>
505              
506             Mo language utilities.
507              
508             =item L<Mo::utils::CSS>
509              
510             Mo CSS utilities.
511              
512             =item L<Wikibase::Datatype::Utils>
513              
514             Wikibase datatype utilities.
515              
516             =back
517              
518             =head1 REPOSITORY
519              
520             L<https://github.com/michal-josef-spacek/Mo-utils-Array>
521              
522             =head1 AUTHOR
523              
524             Michal Josef Špaček L<mailto:skim@cpan.org>
525              
526             L<http://skim.cz>
527              
528             =head1 LICENSE AND COPYRIGHT
529              
530             © 2025-2026 Michal Josef Špaček
531              
532             BSD 2-Clause License
533              
534             =head1 VERSION
535              
536             0.04
537              
538             =cut