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 46     46   360040 use base qw(Exporter);
  46         151  
  46         4578  
4 46     46   321 use strict;
  46         104  
  46         951  
5 46     46   254 use warnings;
  46         150  
  46         1589  
6              
7 46     46   1865 use Error::Pure qw(err);
  46         24882  
  46         1885  
8 46     46   447 use Readonly;
  46         123  
  46         1977  
9 46     46   28005 use URI;
  46         225972  
  46         1644  
10 46     46   24974 use Wikibase::Datatype::Value::Globecoordinate;
  46         68467  
  46         17635  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.08;
15              
16             sub obj2struct {
17 6     6 1 22339 my ($obj, $base_uri) = @_;
18              
19 6 100       22 if (! defined $obj) {
20 1         5 err "Object doesn't exist.";
21             }
22 5 100       29 if (! $obj->isa('Wikibase::Datatype::Value::Globecoordinate')) {
23 1         5 err "Object isn't 'Wikibase::Datatype::Value::Globecoordinate'.";
24             }
25 4 100       12 if (! defined $base_uri) {
26 1         4 err 'Base URI is required.';
27             }
28              
29 3 100       13 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         124 return $struct_hr;
41             }
42              
43             sub struct2obj {
44 6     6 1 30491 my $struct_hr = shift;
45              
46 6 100 100     59 if (! exists $struct_hr->{'type'}
47             || $struct_hr->{'type'} ne 'globecoordinate') {
48              
49 2         13 err "Structure isn't for 'globecoordinate' datatype.";
50             }
51              
52 4         41 my $u = URI->new($struct_hr->{'value'}->{'globe'});
53 4         17313 my @path_segments = $u->path_segments;
54 4         352 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       66 $struct_hr->{'value'}->{'longitude'},
64             ],
65             );
66              
67 4         754 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 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 item 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             use strict;
126             use warnings;
127              
128             use Data::Printer;
129             use Wikibase::Datatype::Value::Globecoordinate;
130             use Wikibase::Datatype::Struct::Value::Globecoordinate qw(obj2struct);
131              
132             # Object.
133             my $obj = Wikibase::Datatype::Value::Globecoordinate->new(
134             'value' => [49.6398383, 18.1484031],
135             );
136              
137             # Get structure.
138             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
139              
140             # Dump to output.
141             p $struct_hr;
142              
143             # Output:
144             # \ {
145             # type "globecoordinate",
146             # value {
147             # altitude "null",
148             # globe "http://test.wikidata.org/entity/Q2",
149             # latitude 49.6398383,
150             # longitude 18.1484031,
151             # precision 1e-07
152             # }
153             # }
154              
155             =head1 EXAMPLE2
156              
157             use strict;
158             use warnings;
159              
160             use Wikibase::Datatype::Struct::Value::Globecoordinate qw(struct2obj);
161              
162             # Globe coordinate structure.
163             my $struct_hr = {
164             'type' => 'globecoordinate',
165             'value' => {
166             'altitude' => 'null',
167             'globe' => 'http://test.wikidata.org/entity/Q2',
168             'latitude' => 49.6398383,
169             'longitude' => 18.1484031,
170             'precision' => 1e-07,
171             },
172             };
173              
174             # Get object.
175             my $obj = struct2obj($struct_hr);
176              
177             # Get globe.
178             my $globe = $obj->globe;
179              
180             # Get longitude.
181             my $longitude = $obj->longitude;
182              
183             # Get latitude.
184             my $latitude = $obj->latitude;
185              
186             # Get precision.
187             my $precision = $obj->precision;
188              
189             # Get type.
190             my $type = $obj->type;
191              
192             # Get value.
193             my $value_ar = $obj->value;
194              
195             # Print out.
196             print "Globe: $globe\n";
197             print "Latitude: $latitude\n";
198             print "Longitude: $longitude\n";
199             print "Precision: $precision\n";
200             print "Type: $type\n";
201             print 'Value: '.(join ', ', @{$value_ar})."\n";
202              
203             # Output:
204             # Globe: Q2
205             # Latitude: 49.6398383
206             # Longitude: 18.1484031
207             # Precision: 1e-07
208             # Type: globecoordinate
209             # Value: 49.6398383, 18.1484031
210              
211             =head1 DEPENDENCIES
212              
213             L<Error::Pure>,
214             L<Exporter>,
215             L<Readonly>,
216             L<URI>,
217             L<Wikibase::Datatype::Value::Globecoordinate>.
218              
219             =head1 SEE ALSO
220              
221             =over
222              
223             =item L<Wikibase::Datatype::Struct>
224              
225             Wikibase structure serialization.
226              
227             =item L<Wikibase::Datatype::Value::Globecoordinate>
228              
229             Wikibase globe coordinate value datatype.
230              
231             =back
232              
233             =head1 REPOSITORY
234              
235             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
236              
237             =head1 AUTHOR
238              
239             Michal Josef Špaček L<mailto:skim@cpan.org>
240              
241             L<http://skim.cz>
242              
243             =head1 LICENSE AND COPYRIGHT
244              
245             © Michal Josef Špaček 2020-2021
246              
247             BSD 2-Clause License
248              
249             =head1 VERSION
250              
251             0.08
252              
253             =cut