File Coverage

blib/lib/Wikibase/Datatype/Struct/Value/Globecoordinate.pm
Criterion Covered Total %
statement 38 38 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 64 64 100.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Value::Globecoordinate;
2              
3 50     50   1035666 use base qw(Exporter);
  50         176  
  50         4744  
4 50     50   408 use strict;
  50         125  
  50         999  
5 50     50   317 use warnings;
  50         160  
  50         1491  
6              
7 50     50   1663 use Error::Pure qw(err);
  50         23808  
  50         2204  
8 50     50   499 use Readonly;
  50         141  
  50         2030  
9 50     50   31286 use URI;
  50         240526  
  50         1602  
10 50     50   23322 use Wikibase::Datatype::Value::Globecoordinate;
  50         3431890  
  50         18348  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.11;
15              
16             sub obj2struct {
17 6     6 1 6114 my ($obj, $base_uri) = @_;
18              
19 6 100       22 if (! defined $obj) {
20 1         15 err "Object doesn't exist.";
21             }
22 5 100       29 if (! $obj->isa('Wikibase::Datatype::Value::Globecoordinate')) {
23 1         6 err "Object isn't 'Wikibase::Datatype::Value::Globecoordinate'.";
24             }
25 4 100       13 if (! defined $base_uri) {
26 1         4 err 'Base URI is required.';
27             }
28              
29 3 100       15 my $struct_hr = {
30             'value' => {
31             'altitude' => $obj->altitude ? $obj->altitude : 'null',
32             'globe' => $base_uri.$obj->globe,
33             'latitude' => $obj->latitude,
34             'longitude' => $obj->longitude,
35             'precision' => $obj->precision,
36             },
37             'type' => $obj->type,
38             };
39              
40 3         131 return $struct_hr;
41             }
42              
43             sub struct2obj {
44 6     6 1 13763 my $struct_hr = shift;
45              
46 6 100 100     43 if (! exists $struct_hr->{'type'}
47             || $struct_hr->{'type'} ne 'globecoordinate') {
48              
49 2         21 err "Structure isn't for 'globecoordinate' datatype.";
50             }
51              
52 4         26 my $u = URI->new($struct_hr->{'value'}->{'globe'});
53 4         14899 my @path_segments = $u->path_segments;
54 4         334 my $globe = $path_segments[-1];
55             my $obj = Wikibase::Datatype::Value::Globecoordinate->new(
56             defined $struct_hr->{'value'}->{'altitude'} ? (
57             'altitude' => $struct_hr->{'value'}->{'altitude'},
58             ) : (),
59             'globe' => $globe,
60             'precision' => $struct_hr->{'value'}->{'precision'},
61             'value' => [
62             $struct_hr->{'value'}->{'latitude'},
63 4 100       48 $struct_hr->{'value'}->{'longitude'},
64             ],
65             );
66              
67 4         663 return $obj;
68             }
69              
70             1;
71              
72             __END__
73              
74             =pod
75              
76             =encoding utf8
77              
78             =head1 NAME
79              
80             Wikibase::Datatype::Struct::Value::Globecoordinate - Wikibase globe coordinate value structure serialization.
81              
82             =head1 SYNOPSIS
83              
84             use Wikibase::Datatype::Struct::Value::Globecoordinate qw(obj2struct struct2obj);
85              
86             my $struct_hr = obj2struct($obj, $base_uri);
87             my $obj = struct2obj($struct_hr);
88              
89             =head1 DESCRIPTION
90              
91             This conversion is between objects defined in Wikibase::Datatype and structures
92             serialized via JSON to MediaWiki.
93              
94             =head1 SUBROUTINES
95              
96             =head2 C<obj2struct>
97              
98             my $struct_hr = obj2struct($obj, $base_uri);
99              
100             Convert Wikibase::Datatype::Value::Globecoordinate instance to structure.
101             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
102              
103             Returns reference to hash with structure.
104              
105             =head2 C<struct2obj>
106              
107             my $obj = struct2obj($struct_hr);
108              
109             Convert structure of globe coordinate to object.
110              
111             Returns Wikibase::Datatype::Value::Globecoordinate instance.
112              
113             =head1 ERRORS
114              
115             obj2struct():
116             Base URI is required.
117             Object doesn't exist.
118             Object isn't 'Wikibase::Datatype::Value::Globecoordinate'.
119              
120             struct2obj():
121             Structure isn't for 'globecoordinate' datatype.
122              
123             =head1 EXAMPLE1
124              
125             =for comment filename=obj2struct_value_globe_coordinate.pl
126              
127             use strict;
128             use warnings;
129              
130             use Data::Printer;
131             use Wikibase::Datatype::Value::Globecoordinate;
132             use Wikibase::Datatype::Struct::Value::Globecoordinate qw(obj2struct);
133              
134             # Object.
135             my $obj = Wikibase::Datatype::Value::Globecoordinate->new(
136             'value' => [49.6398383, 18.1484031],
137             );
138              
139             # Get structure.
140             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
141              
142             # Dump to output.
143             p $struct_hr;
144              
145             # Output:
146             # \ {
147             # type "globecoordinate",
148             # value {
149             # altitude "null",
150             # globe "http://test.wikidata.org/entity/Q2",
151             # latitude 49.6398383,
152             # longitude 18.1484031,
153             # precision 1e-07
154             # }
155             # }
156              
157             =head1 EXAMPLE2
158              
159             =for comment filename=struct2obj_value_globe_coordinate.pl
160              
161             use strict;
162             use warnings;
163              
164             use Wikibase::Datatype::Struct::Value::Globecoordinate qw(struct2obj);
165              
166             # Globe coordinate structure.
167             my $struct_hr = {
168             'type' => 'globecoordinate',
169             'value' => {
170             'altitude' => 'null',
171             'globe' => 'http://test.wikidata.org/entity/Q2',
172             'latitude' => 49.6398383,
173             'longitude' => 18.1484031,
174             'precision' => 1e-07,
175             },
176             };
177              
178             # Get object.
179             my $obj = struct2obj($struct_hr);
180              
181             # Get globe.
182             my $globe = $obj->globe;
183              
184             # Get longitude.
185             my $longitude = $obj->longitude;
186              
187             # Get latitude.
188             my $latitude = $obj->latitude;
189              
190             # Get precision.
191             my $precision = $obj->precision;
192              
193             # Get type.
194             my $type = $obj->type;
195              
196             # Get value.
197             my $value_ar = $obj->value;
198              
199             # Print out.
200             print "Globe: $globe\n";
201             print "Latitude: $latitude\n";
202             print "Longitude: $longitude\n";
203             print "Precision: $precision\n";
204             print "Type: $type\n";
205             print 'Value: '.(join ', ', @{$value_ar})."\n";
206              
207             # Output:
208             # Globe: Q2
209             # Latitude: 49.6398383
210             # Longitude: 18.1484031
211             # Precision: 1e-07
212             # Type: globecoordinate
213             # Value: 49.6398383, 18.1484031
214              
215             =head1 DEPENDENCIES
216              
217             L<Error::Pure>,
218             L<Exporter>,
219             L<Readonly>,
220             L<URI>,
221             L<Wikibase::Datatype::Value::Globecoordinate>.
222              
223             =head1 SEE ALSO
224              
225             =over
226              
227             =item L<Wikibase::Datatype::Struct>
228              
229             Wikibase structure serialization.
230              
231             =item L<Wikibase::Datatype::Value::Globecoordinate>
232              
233             Wikibase globe coordinate value datatype.
234              
235             =back
236              
237             =head1 REPOSITORY
238              
239             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
240              
241             =head1 AUTHOR
242              
243             Michal Josef Špaček L<mailto:skim@cpan.org>
244              
245             L<http://skim.cz>
246              
247             =head1 LICENSE AND COPYRIGHT
248              
249             © 2020-2023 Michal Josef Špaček
250              
251             BSD 2-Clause License
252              
253             =head1 VERSION
254              
255             0.11
256              
257             =cut