File Coverage

blib/lib/Wikibase/Datatype/Value/Globecoordinate.pm
Criterion Covered Total %
statement 38 38 100.0
branch 12 12 100.0
condition n/a
subroutine 9 9 100.0
pod 3 4 75.0
total 62 63 98.4


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Value::Globecoordinate;
2              
3 10     10   1149755 use strict;
  10         120  
  10         292  
4 10     10   66 use warnings;
  10         52  
  10         284  
5              
6 10     10   4767 use Error::Pure qw(err);
  10         106406  
  10         192  
7 10     10   5229 use Mo qw(build is);
  10         5301  
  10         70  
8 10     10   19119 use Wikibase::Datatype::Utils qw(check_entity);
  10         43  
  10         228  
9              
10             our $VERSION = 0.30;
11              
12             extends 'Wikibase::Datatype::Value';
13              
14             has altitude => (
15             'is' => 'ro',
16             );
17              
18             has globe => (
19             'is' => 'ro',
20             );
21              
22             sub latitude {
23 1     1 1 15 my $self = shift;
24              
25 1         3 return $self->{'value'}->[0];
26             }
27              
28             sub longitude {
29 1     1 1 11 my $self = shift;
30              
31 1         3 return $self->{'value'}->[1];
32             }
33              
34             has precision => (
35             'is' => 'ro',
36             );
37              
38             sub type {
39 1     1 1 17 return 'globecoordinate';
40             }
41              
42             sub BUILD {
43 15     15 0 99 my $self = shift;
44              
45 15 100       79 if (ref $self->{'value'} ne 'ARRAY') {
46 1         4 err "Parameter 'value' must be a array.";
47             }
48 14 100       29 if (@{$self->{'value'}} != 2) {
  14         64  
49 1         5 err "Parameter 'value' array must have two fields ".
50             "(latitude and longitude).";
51             }
52 13         30 my ($lat, $lon) = @{$self->{'value'}};
  13         43  
53 13 100       172 if ($lat !~ m/^\-?\d+\.?\d*$/ms) {
54 1         4 err "Parameter 'value' has bad first parameter (latitude).";
55             }
56 12 100       100 if ($lon !~ m/^\-?\d+\.?\d*$/ms) {
57 1         4 err "Parameter 'value' has bad first parameter (longitude).";
58             }
59              
60 11 100       44 if (! defined $self->{'globe'}) {
61 10         29 $self->{'globe'} = 'Q2',
62             }
63 11         57 check_entity($self, 'globe');
64              
65 11 100       45 if (! defined $self->{'precision'}) {
66 10         30 $self->{'precision'} = '1e-07',
67             }
68              
69 11         30 return;
70             }
71              
72             1;
73              
74             __END__
75              
76             =pod
77              
78             =encoding utf8
79              
80             =head1 NAME
81              
82             Wikibase::Datatype::Value::Globecoordinate - Wikibase globe coordinate value datatype.
83              
84             =head1 SYNOPSIS
85              
86             use Wikibase::Datatype::Value::Globecoordinate;
87              
88             my $obj = Wikibase::Datatype::Value::Globecoordinate->new(%params);
89             my $altitude = $obj->altitude;
90             my $globe = $obj->globe;
91             my $latitude = $obj->latitude;
92             my $longitude = $obj->longitude;
93             my $precision = $obj->precision;
94             my $type = $obj->type;
95             my $value = $obj->value;
96              
97             =head1 DESCRIPTION
98              
99             This datatype is globecoordinate class for representation of coordinate.
100              
101             =head1 METHODS
102              
103             =head2 C<new>
104              
105             my $obj = Wikibase::Datatype::Value::Globecoordinate->new(%params);
106              
107             Constructor.
108              
109             Returns instance of object.
110              
111             =over 8
112              
113             =item * C<altitude>
114              
115             Altitude.
116             Parameter is optional.
117             Default value is undef.
118              
119             =item * C<globe>
120              
121             Globe entity.
122             Parameter is optional.
123             Default value is 'Q2'.
124              
125             =item * C<precision>
126              
127             Coordinate precision.
128             Parameter is optional.
129             Default value is '1e-07'.
130              
131             =item * C<value>
132              
133             Value of instance.
134             Parameter is required.
135              
136             =back
137              
138             =head2 C<altitude>
139              
140             my $altitude = $obj->altitude;
141              
142             Get altitude.
143              
144             Returns TODO
145              
146             =head2 C<globe>
147              
148             my $globe = $obj->globe;
149              
150             Get globe. Unit is entity (e.g. /^Q\d+$/).
151              
152             Returns string.
153              
154             =head2 C<latitude>
155              
156             my $latitude = $obj->latitude;
157              
158             Get latitude.
159              
160             Returns number.
161              
162             =head2 C<longitude>
163              
164             my $longitude = $obj->longitude;
165              
166             Get longitude.
167              
168             Returns number.
169              
170             =head2 C<precision>
171              
172             my $precision = $obj->precision;
173              
174             Get precision.
175              
176             Returns number.
177              
178             =head2 C<type>
179              
180             my $type = $obj->type;
181              
182             Get type. This is constant 'string'.
183              
184             Returns string.
185              
186             =head2 C<value>
187              
188             my $value = $obj->value;
189              
190             Get value.
191              
192             Returns string.
193              
194             =head1 ERRORS
195              
196             new():
197             From Wikibase::Datatype::Utils::check_entity():
198             Parameter 'globe' must begin with 'Q' and number after it.
199             From Wikibase::Datatype::Value::new():
200             Parameter 'value' is required.
201             Parameter 'value' array must have two fields (latitude and longitude).
202             Parameter 'value' has bad first parameter (latitude).
203             Parameter 'value' has bad first parameter (longitude).
204             Parameter 'value' must be a array.
205              
206             =head1 EXAMPLE
207              
208             =for comment filename=create_and_print_value_globecoordinate.pl
209              
210             use strict;
211             use warnings;
212              
213             use Wikibase::Datatype::Value::Globecoordinate;
214              
215             # Object.
216             my $obj = Wikibase::Datatype::Value::Globecoordinate->new(
217             'value' => [49.6398383, 18.1484031],
218             );
219              
220             # Get globe.
221             my $globe = $obj->globe;
222              
223             # Get longitude.
224             my $longitude = $obj->longitude;
225              
226             # Get latitude.
227             my $latitude = $obj->latitude;
228              
229             # Get precision.
230             my $precision = $obj->precision;
231              
232             # Get type.
233             my $type = $obj->type;
234              
235             # Get value.
236             my $value_ar = $obj->value;
237              
238             # Print out.
239             print "Globe: $globe\n";
240             print "Latitude: $latitude\n";
241             print "Longitude: $longitude\n";
242             print "Precision: $precision\n";
243             print "Type: $type\n";
244             print 'Value: '.(join ', ', @{$value_ar})."\n";
245              
246             # Output:
247             # Globe: Q2
248             # Latitude: 49.6398383
249             # Longitude: 18.1484031
250             # Precision: 1e-07
251             # Type: globecoordinate
252             # Value: 49.6398383, 18.1484031
253              
254             =head1 DEPENDENCIES
255              
256             L<Error::Pure>,
257             L<Mo>,
258             L<Wikibase::Datatype::Utils>,
259             L<Wikibase::Datatype::Value>.
260              
261             =head1 SEE ALSO
262              
263             =over
264              
265             =item L<Wikibase::Datatype::Value>
266              
267             Wikibase datatypes.
268              
269             =item L<Wikibase::Datatype::Print::Value::Globecoordinate>
270              
271             Wikibase globe coordinate value pretty print helpers.
272              
273             =back
274              
275             =head1 REPOSITORY
276              
277             L<https://github.com/michal-josef-spacek/Wikibase-Datatype>
278              
279             =head1 AUTHOR
280              
281             Michal Josef Špaček L<mailto:skim@cpan.org>
282              
283             L<http://skim.cz>
284              
285             =head1 LICENSE AND COPYRIGHT
286              
287             © 2020-2023 Michal Josef Špaček
288              
289             BSD 2-Clause License
290              
291             =head1 VERSION
292              
293             0.30
294              
295             =cut