| 1 | package MARC::Convert::Wikidata::Object::Utils; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 23 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 23 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 23 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 23 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 23 | Readonly::Array our @EXPORT_OK => qw(check_date check_date_order); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 23 | our $VERSION = 0.01; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub check_date { | ||||||||||||||||||||||||||||
| 16 | 73 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
check_date: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 73 | ▶ | if (! exists $self->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 16 | return; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 57 | ▶ | if (! defined $self->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | return; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Check year format. | ||||||||||||||||||||||||||||
| 27 | 57 | ▶ | if ($self->{$key} !~ m/^\-?(\d{1,4})\-?\d{0,2}\-?\d{0,2}$/ms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | err "Parameter '$key' is in bad format.", | ||||||||||||||||||||||||||||
| 29 | 2 | 'Value', $self->{$key}, | |||||||||||||||||||||||||||
| 30 | ; | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 55 | my $year = $1; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Check year greater than actual. | ||||||||||||||||||||||||||||
| 35 | 55 | ▶ | if ($year > DateTime->now->year) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 1 | err "Parameter '$key' has year greater than actual year."; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 54 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub check_date_order { | ||||||||||||||||||||||||||||
| 43 | 32 | ▶ | my ($self, $key1, $key2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
check_date_order: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 32 | ▶ | if (! exists $self->{$key1} || ! exists $self->{$key2}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $self->{$key1} and exists $self->{$key2}
MC/DC100%2/2
exists $self->{$key1} and exists $self->{$key2}
exists $self->{$key1}
exists $self->{$key2}
Truth table100%3/3
A and B
A= exists $self->{$key1}B= exists $self->{$key2}
| |||||||||||||||||||||||||||||
| 46 | 10 | return; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 22 | ▶ | if (! defined $self->{$key1} || ! defined $self->{$key2}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{$key1} and defined $self->{$key2}
MC/DC0%0/2
defined $self->{$key1} and defined $self->{$key2}
defined $self->{$key1}
defined $self->{$key2}
Truth table33%1/3
A and B
A= defined $self->{$key1}B= defined $self->{$key2}
| |||||||||||||||||||||||||||||
| 50 | 0 | return; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 22 | my $dt1 = _construct_dt($self->{$key1}); | |||||||||||||||||||||||||||
| 54 | 22 | my $dt2 = _construct_dt($self->{$key2}); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 22 | my $cmp = DateTime->compare($dt1, $dt2); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # dt1 >= dt2 | ||||||||||||||||||||||||||||
| 59 | 22 | ▶ | if ($cmp != -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 3 | err "Parameter '$key1' has date greater or same as parameter '$key2' date."; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 19 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub _construct_dt { | ||||||||||||||||||||||||||||
| 67 | 44 | ▶ | my $date = shift; | ||||||||||||||||||||||||||
|
Subroutine
_construct_dt: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 44 | my ($year, $month, $day) = $date =~ m/^(\-?\d{1,4})\-?(\d{0,2})\-?(\d{0,2})$/ms; | |||||||||||||||||||||||||||
| 70 | 44 | ▶ | my $dt = DateTime->new( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 71 | 'year' => $year, | ||||||||||||||||||||||||||||
| 72 | $month ? ('month' => $month) : (), | ||||||||||||||||||||||||||||
| 73 | $day ? ('day' => $day) : (), | ||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 44 | return $dt; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 23 | 1; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =pod | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =encoding utf8 | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 NAME | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | MARC::Convert::Wikidata::Object::Utils - MARC::Convert::Wikidata::Object utilities. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | use MARC::Convert::Wikidata::Object::Utils qw(check_date); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | check_date($self, $key); | ||||||||||||||||||||||||||||
| 96 | check_date_order($self, $key1, $key2); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Utilities for checking of data values. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 C<check_date> | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | check_date($self, $key); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Check parameter defined by C<$key> which is date and that date isn't greater | ||||||||||||||||||||||||||||
| 109 | than actual year. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Possible dates: | ||||||||||||||||||||||||||||
| 112 | - YYYY-MM-DD | ||||||||||||||||||||||||||||
| 113 | - YYYY-M-D | ||||||||||||||||||||||||||||
| 114 | - YYYY-MM | ||||||||||||||||||||||||||||
| 115 | - YYYY-M | ||||||||||||||||||||||||||||
| 116 | - YYYY | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Put error if check isn't ok. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Returns undef. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 C<check_date_order> | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | check_date_order($self, $key1, $key2); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Check if date with C<$key1> is lesser than date with C<$key2>. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Put error if check isn't ok. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Returns undef. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 ERRORS | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | check_date(): | ||||||||||||||||||||||||||||
| 135 | Parameter '%s' for date is in bad format. | ||||||||||||||||||||||||||||
| 136 | Parameter '%s' has year greater than actual year. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | check_date_order(): | ||||||||||||||||||||||||||||
| 139 | Parameter '%s' has date greater or same as parameter '%s' date. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 EXAMPLE1 | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =for comment filename=check_date_ok.pl | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | use strict; | ||||||||||||||||||||||||||||
| 146 | use warnings; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | use MARC::Convert::Wikidata::Object::Utils qw(check_date); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | my $self = { | ||||||||||||||||||||||||||||
| 151 | 'key' => '2022-01-15', | ||||||||||||||||||||||||||||
| 152 | }; | ||||||||||||||||||||||||||||
| 153 | check_date($self, 'key'); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Print out. | ||||||||||||||||||||||||||||
| 156 | print "ok\n"; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Output: | ||||||||||||||||||||||||||||
| 159 | # ok | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 EXAMPLE2 | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =for comment filename=check_date_error.pl | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | use strict; | ||||||||||||||||||||||||||||
| 166 | use warnings; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | use Error::Pure; | ||||||||||||||||||||||||||||
| 169 | use MARC::Convert::Wikidata::Object::Utils qw(check_date); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | $Error::Pure::TYPE = 'Error'; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | my $self = { | ||||||||||||||||||||||||||||
| 174 | 'key' => 'foo', | ||||||||||||||||||||||||||||
| 175 | }; | ||||||||||||||||||||||||||||
| 176 | check_date($self, 'key'); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Print out. | ||||||||||||||||||||||||||||
| 179 | print "ok\n"; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # Output like: | ||||||||||||||||||||||||||||
| 182 | # #Error [..Utils.pm:?] Parameter 'key' is in bad format. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | L<DateTime>, | ||||||||||||||||||||||||||||
| 187 | L<Exporter>, | ||||||||||||||||||||||||||||
| 188 | L<Error::Pure>, | ||||||||||||||||||||||||||||
| 189 | L<Readonly>. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =over | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item L<Mo::utils> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Mo utilities. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =back | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | L<https://github.com/michal-josef-spacek/MARC-Convert-Wikidata-Object> | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Michal Josef Špaček L<mailto:skim@cpan.org> | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | L<http://skim.cz> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | © Michal Josef Špaček 2021-2023 | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | BSD 2-Clause License | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 VERSION | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0.01 | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||||||||||