File Coverage

blib/lib/Wikibase/Datatype/Utils.pm
Criterion Covered Total %
statement 69 69 100.0
branch 26 26 100.0
condition n/a
subroutine 16 16 100.0
pod 6 6 100.0
total 117 117 100.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Utils;
2              
3 247     247   983408 use base qw(Exporter);
  247         891  
  247         26119  
4 247     247   1762 use strict;
  247         701  
  247         5358  
5 247     247   1355 use warnings;
  247         594  
  247         7015  
6              
7 247     247   244637 use DateTime;
  247         134334777  
  247         13931  
8 247     247   38200 use Error::Pure qw(err);
  247         707914  
  247         13666  
9 247     247   6401 use List::Util qw(none);
  247         741  
  247         15463  
10 247     247   154690 use Wikibase::Datatype::Languages qw(all_language_codes);
  247         1235  
  247         8165  
11 247     247   20261 use Readonly;
  247         1189  
  247         208498  
12              
13             Readonly::Array our @EXPORT_OK => qw(check_datetime check_entity check_language
14             check_lexeme check_property check_sense);
15              
16             our $VERSION = 0.30;
17              
18             sub check_datetime {
19 52     52 1 15433 my ($self, $key) = @_;
20              
21 52 100       510 if ($self->{$key} !~ m/^([\+\-]\d+)\-(\d{2})\-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/ms) {
22             err "Parameter '$key' has bad date time.",
23 1         6 'Value', $self->{$key},
24             ;
25             }
26 51         451 my ($year, $month, $day, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6);
27 51 100       247 if ($month > 12) {
28             err "Parameter '$key' has bad date time month value.",
29 1         9 'value' => $self->{$key},
30             ;
31             }
32 50 100       216 if ($month > 0) {
33 46         444 my $dt = DateTime->new(
34             'day' => 1,
35             'month' => $month,
36             'year' => int($year),
37             )->add(months => 1)->subtract(days => 1);;
38 46 100       143427 if ($day > $dt->day) {
39             err "Parameter '$key' has bad date time day value.",
40 1         14 'value' => $self->{$key},
41             ;
42             }
43             } else {
44 4 100       14 if ($day != 0) {
45             err "Parameter '$key' has bad date time day value.",
46 1         7 'value' => $self->{$key},
47             ;
48             }
49             }
50 48 100       706 if ($hour != 0) {
51             err "Parameter '$key' has bad date time hour value.",
52 2         21 'value' => $self->{$key},
53             ;
54             }
55 46 100       202 if ($min != 0) {
56             err "Parameter '$key' has bad date time minute value.",
57 1         9 'value' => $self->{$key},
58             ;
59             }
60 45 100       192 if ($sec != 0) {
61             err "Parameter '$key' has bad date time second value.",
62 1         11 'value' => $self->{$key},
63             ;
64             }
65              
66 44         169 return;
67             }
68              
69             sub check_entity {
70 242     242 1 4577 my ($self, $key) = @_;
71              
72 242         842 _check_item_with_char($self, $key, 'Q');
73              
74 238         574 return;
75             }
76              
77             sub check_language {
78 118     118 1 5045 my ($self, $key) = @_;
79              
80 118 100   35289   705 if (none { $_ eq $self->{$key} } all_language_codes()) {
  35289         228595  
81 4         31 err "Language code '".$self->{$key}."' isn't ISO 639-1 code.";
82             }
83              
84 114         5323 return;
85             }
86              
87             sub check_lexeme {
88 2     2 1 3126 my ($self, $key) = @_;
89              
90 2         8 _check_item_with_char($self, $key, 'L');
91              
92 1         3 return;
93             }
94              
95             sub check_property {
96 219     219 1 4128 my ($self, $key) = @_;
97              
98 219         723 _check_item_with_char($self, $key, 'P');
99              
100 215         480 return;
101             }
102              
103             sub check_sense {
104 7     7 1 3717 my ($self, $key) = @_;
105              
106 7 100       32 if (! defined $self->{$key}) {
107 1         4 return;
108             }
109              
110 6 100       56 if ($self->{$key} !~ m/^L\d+\-S\d+$/ms) {
111 2         21 err "Parameter '$key' must begin with 'L' and number, dash, S and number after it.";
112             }
113              
114 4         14 return;
115             }
116              
117             sub _check_item_with_char {
118 463     463   1202 my ($self, $key, $char) = @_;
119              
120 463 100       1459 if (! defined $self->{$key}) {
121 27         59 return;
122             }
123              
124 436 100       6688 if ($self->{$key} !~ m/^$char\d+$/ms) {
125 9         65 err "Parameter '$key' must begin with '$char' and number after it.";
126             }
127              
128 427         1251 return;
129             }
130              
131             1;
132              
133             __END__
134              
135             =pod
136              
137             =encoding utf8
138              
139             =head1 NAME
140              
141             Wikibase::Datatype::Utils - Wikibase datatype utilities.
142              
143             =head1 SYNOPSIS
144              
145             use Wikibase::Datatype::Utils qw(check_datetime check_entity check_language check_lexeme check_property check_sense);
146              
147             check_datetime($self, $key);
148             check_entity($self, $key);
149             check_language($self, $key);
150             check_lexeme($self, $key);
151             check_property($self, $key);
152             check_sense($self, $key);
153              
154             =head1 DESCRIPTION
155              
156             Datatype utilities for checking of data objects.
157              
158             =head1 SUBROUTINES
159              
160             =head2 C<check_datetime>
161              
162             check_datetime($self, $key);
163              
164             Check parameter defined by C<$key> if it's datetime for Wikibase.
165             Format of value is variation of ISO 8601 with some changes (like 00 as valid month).
166              
167             Returns undef.
168              
169             =head2 C<check_entity>
170              
171             check_entity($self, $key);
172              
173             Check parameter defined by C<$key> if it's entity (/^Q\d+/).
174              
175             Returns undef.
176              
177             =head2 C<check_language>
178              
179             check_language($self, $key);
180              
181             Check parameter defined by C<$key> if it's ISO 639-1 language code and if language exists.
182              
183             Returns undef.
184              
185             =head2 C<check_lexeme>
186              
187             check_lexeme($self, $key);
188              
189             Check parameter defined by C<$key> if it's entity (/^L\d+/).
190              
191             Returns undef.
192              
193             =head2 C<check_property>
194              
195             check_property($self, $key);
196              
197             Check parameter defined by C<$key> if it's property (/^P\d+/).
198              
199             Returns undef.
200              
201             =head2 C<check_sense>
202              
203             check_sense($self, $key);
204              
205             Check parameter defined by C<$key> if it's property (/^L\d+\-S\d+$/).
206              
207             Returns undef.
208              
209             =head1 ERRORS
210              
211             check_datetime():
212             Parameter '%s' has bad date time.
213             Value: %s
214             Parameter '%s' has bad date time day value.
215             Value: %s
216             Parameter '%s' has bad date time hour value.
217             Value: %s
218             Parameter '%s' has bad date time minute value.
219             Value: %s
220             Parameter '%s' has bad date time month value.
221             Value: %s
222             Parameter '%s' has bad date time second value.
223             Value: %s
224              
225             check_entity():
226             Parameter '%s' must begin with 'Q' and number after it.";
227              
228             check_language():
229             Language code '%s' isn't ISO 639-1 code.
230             Language with ISO 639-1 code '%s' doesn't exist.
231              
232             check_lexeme():
233             Parameter '%s' must begin with 'L' and number after it.";
234              
235             check_property():
236             Parameter '%s' must begin with 'P' and number after it.";
237              
238             check_sense():
239             Parameter '%s' must begin with 'L' and number, dash, S and number after it.
240              
241             =head1 EXAMPLE1
242              
243             =for comment filename=check_datetime_success.pl
244              
245             use strict;
246             use warnings;
247              
248             use Wikibase::Datatype::Utils qw(check_datetime);
249              
250             my $self = {
251             'key' => '+0134-11-00T00:00:00Z',
252             'precision' => 10
253             };
254             check_datetime($self, 'key');
255              
256             # Print out.
257             print "ok\n";
258              
259             # Output:
260             # ok
261              
262             =head1 EXAMPLE2
263              
264             =for comment filename=check_datetime_fail.pl
265              
266             use strict;
267             use warnings;
268              
269             use Wikibase::Datatype::Utils qw(check_datetime);
270              
271             $Error::Pure::TYPE = 'Error';
272              
273             my $self = {
274             'key' => '+0134-34-00T00:01:00Z',
275             };
276             check_datetime($self, 'key');
277              
278             # Print out.
279             print "ok\n";
280              
281             # Output:
282             # #Error [/../Wikibase/Datatype/Utils.pm:?] Parameter 'key' has bad date time month value.
283              
284             =head1 EXAMPLE3
285              
286             =for comment filename=check_entity_success.pl
287              
288             use strict;
289             use warnings;
290              
291             use Wikibase::Datatype::Utils qw(check_entity);
292              
293             my $self = {
294             'key' => 'Q123',
295             };
296             check_entity($self, 'key');
297              
298             # Print out.
299             print "ok\n";
300              
301             # Output:
302             # ok
303              
304             =head1 EXAMPLE4
305              
306             =for comment filename=check_entity_fail.pl
307              
308             use strict;
309             use warnings;
310              
311             use Error::Pure;
312             use Wikibase::Datatype::Utils qw(check_entity);
313              
314             $Error::Pure::TYPE = 'Error';
315              
316             my $self = {
317             'key' => 'bad_entity',
318             };
319             check_entity($self, 'key');
320              
321             # Print out.
322             print "ok\n";
323              
324             # Output like:
325             # #Error [/../Wikibase/Datatype/Utils.pm:?] Parameter 'key' must begin with 'Q' and number after it.
326              
327             =head1 EXAMPLE5
328              
329             =for comment filename=check_lexeme_success.pl
330              
331             use strict;
332             use warnings;
333              
334             use Wikibase::Datatype::Utils qw(check_lexeme);
335              
336             my $self = {
337             'key' => 'L123',
338             };
339             check_lexeme($self, 'key');
340              
341             # Print out.
342             print "ok\n";
343              
344             # Output:
345             # ok
346              
347             =head1 EXAMPLE6
348              
349             =for comment filename=check_lexeme_fail.pl
350              
351             use strict;
352             use warnings;
353              
354             use Error::Pure;
355             use Wikibase::Datatype::Utils qw(check_lexeme);
356              
357             $Error::Pure::TYPE = 'Error';
358              
359             my $self = {
360             'key' => 'bad_entity',
361             };
362             check_lexeme($self, 'key');
363              
364             # Print out.
365             print "ok\n";
366              
367             # Output like:
368             # #Error [/../Wikibase/Datatype/Utils.pm:?] Parameter 'key' must begin with 'L' and number after it.
369              
370             =head1 EXAMPLE7
371              
372             =for comment filename=check_property_success.pl
373              
374             use strict;
375             use warnings;
376              
377             use Wikibase::Datatype::Utils qw(check_property);
378              
379             my $self = {
380             'key' => 'P123',
381             };
382             check_property($self, 'key');
383              
384             # Print out.
385             print "ok\n";
386              
387             # Output:
388             # ok
389              
390             =head1 EXAMPLE8
391              
392             =for comment filename=check_property_fail.pl
393              
394             use strict;
395             use warnings;
396              
397             use Error::Pure;
398             use Wikibase::Datatype::Utils qw(check_property);
399              
400             $Error::Pure::TYPE = 'Error';
401              
402             my $self = {
403             'key' => 'bad_property',
404             };
405             check_property($self, 'key');
406              
407             # Print out.
408             print "ok\n";
409              
410             # Output like:
411             # #Error [/../Wikibase/Datatype/Utils.pm:?] Parameter 'key' must begin with 'P' and number after it.
412              
413             =head1 EXAMPLE9
414              
415             =for comment filename=check_sense_success.pl
416              
417             use strict;
418             use warnings;
419              
420             use Wikibase::Datatype::Utils qw(check_sense);
421              
422             my $self = {
423             'key' => 'L34727-S1',
424             };
425             check_sense($self, 'key');
426              
427             # Print out.
428             print "ok\n";
429              
430             # Output:
431             # ok
432              
433             =head1 EXAMPLE10
434              
435             =for comment filename=check_sense_fail.pl
436              
437             use strict;
438             use warnings;
439              
440             use Error::Pure;
441             use Wikibase::Datatype::Utils qw(check_sense);
442              
443             $Error::Pure::TYPE = 'Error';
444              
445             my $self = {
446             'key' => 'bad_sense',
447             };
448             check_sense($self, 'key');
449              
450             # Print out.
451             print "ok\n";
452              
453             # Output like:
454             # #Error [/../Wikibase/Datatype/Utils.pm:?] Parameter 'key' must begin with 'L' and number, dash, S and number after it.
455              
456             =head1 DEPENDENCIES
457              
458             L<DateTime>,
459             L<Exporter>,
460             L<Error::Pure>,
461             L<List::Util>,
462             L<Readonly>.
463              
464             =head1 SEE ALSO
465              
466             =over
467              
468             =item L<Wikibase::Datatype>
469              
470             Wikibase datatypes.
471              
472             =back
473              
474             =head1 REPOSITORY
475              
476             L<https://github.com/michal-josef-spacek/Wikibase-Datatype>
477              
478             =head1 AUTHOR
479              
480             Michal Josef Špaček L<mailto:skim@cpan.org>
481              
482             L<http://skim.cz>
483              
484             =head1 LICENSE AND COPYRIGHT
485              
486             © 2020-2023 Michal Josef Špaček
487              
488             BSD 2-Clause License
489              
490             =head1 VERSION
491              
492             0.30
493              
494             =cut