File Coverage

blib/lib/Wikibase/Datatype/Struct/Value/Quantity.pm
Criterion Covered Total %
statement 56 57 98.2
branch 21 24 87.5
condition 1 3 33.3
subroutine 11 11 100.0
pod 2 2 100.0
total 91 97 93.8


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Value::Quantity;
2              
3 46     46   342950 use base qw(Exporter);
  46         149  
  46         4743  
4 46     46   301 use strict;
  46         95  
  46         983  
5 46     46   222 use warnings;
  46         104  
  46         1545  
6              
7 46     46   1830 use Error::Pure qw(err);
  46         36085  
  46         1952  
8 46     46   459 use Readonly;
  46         127  
  46         2031  
9 46     46   2806 use URI;
  46         19421  
  46         1324  
10 46     46   23616 use Wikibase::Datatype::Value::Quantity;
  46         58265  
  46         28172  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.08;
15              
16             sub obj2struct {
17 7     7 1 22259 my ($obj, $base_uri) = @_;
18              
19 7 100       24 if (! defined $obj) {
20 1         4 err "Object doesn't exist.";
21             }
22 6 100       30 if (! $obj->isa('Wikibase::Datatype::Value::Quantity')) {
23 1         5 err "Object isn't 'Wikibase::Datatype::Value::Quantity'.";
24             }
25 5 100       13 if (! defined $base_uri) {
26 1         4 err 'Base URI is required.';
27             }
28              
29 4         16 my $amount = $obj->value;
30 4         35 $amount = _add_plus($amount);
31 4         7 my $unit;
32 4 100       21 if (defined $obj->unit) {
33 3         24 $unit = $base_uri.$obj->unit;
34             } else {
35 1         8 $unit = 1;
36             }
37 4 100       37 my $struct_hr = {
    100          
38             'value' => {
39             'amount' => $amount,
40             defined $obj->lower_bound ? (
41             'lowerBound' => _add_plus($obj->lower_bound),
42             ) : (),
43             'unit' => $unit,
44             defined $obj->upper_bound ? (
45             'upperBound' => _add_plus($obj->upper_bound),
46             ) : (),
47             },
48             'type' => 'quantity',
49             };
50              
51 4         57 return $struct_hr;
52             }
53              
54             sub struct2obj {
55 4     4 1 4419 my $struct_hr = shift;
56              
57 4 50 33     34 if (! exists $struct_hr->{'type'}
58             || $struct_hr->{'type'} ne 'quantity') {
59              
60 0         0 err "Structure isn't for 'quantity' datatype.";
61             }
62              
63 4         11 my $amount = $struct_hr->{'value'}->{'amount'};
64 4         12 $amount = _remove_plus($amount);
65 4         12 my $unit = $struct_hr->{'value'}->{'unit'};
66 4 100       15 if ($unit eq 1) {
67 1         3 $unit = undef;
68             } else {
69 3         26 my $u = URI->new($unit);
70 3         10066 my @path_segments = $u->path_segments;
71 3         281 $unit = $path_segments[-1];
72             }
73             my $obj = Wikibase::Datatype::Value::Quantity->new(
74             $struct_hr->{'value'}->{'lowerBound'} ? (
75             'lower_bound' => _remove_plus($struct_hr->{'value'}->{'lowerBound'}),
76             ) : (),
77             'unit' => $unit,
78             $struct_hr->{'value'}->{'upperBound'} ? (
79 4 100       46 'upper_bound' => _remove_plus($struct_hr->{'value'}->{'upperBound'}),
    100          
80             ) : (),
81             'value' => $amount,
82             );
83              
84 4         504 return $obj;
85             }
86              
87             sub _add_plus {
88 6     6   31 my $value = shift;
89              
90 6 50       38 if ($value =~ m/^\d+$/) {
91 6         18 $value = '+'.$value;
92             }
93              
94 6         18 return $value;
95             }
96              
97             sub _remove_plus {
98 6     6   11 my $value = shift;
99              
100 6 50       34 if ($value =~ m/^\+(\d+)$/ms) {
101 6         20 $value = $1;
102             }
103              
104 6         19 return $value;
105             }
106              
107             1;
108              
109             __END__
110              
111             =pod
112              
113             =encoding utf8
114              
115             =head1 NAME
116              
117             Wikibase::Datatype::Struct::Value::Quantity - Wikibase quantity structure serialization.
118              
119             =head1 SYNOPSIS
120              
121             use Wikibase::Datatype::Struct::Value::Quantity qw(obj2struct struct2obj);
122              
123             my $struct_hr = obj2struct($obj, $base_uri);
124             my $obj = struct2obj($struct_hr);
125              
126             =head1 DESCRIPTION
127              
128             This conversion is between objects defined in Wikibase::Datatype and structures
129             serialized via JSON to MediaWiki.
130              
131             =head1 SUBROUTINES
132              
133             =head2 C<obj2struct>
134              
135             my $struct_hr = obj2struct($obj, $base_uri);
136              
137             Convert Wikibase::Datatype::Value::Quantity instance to structure.
138             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
139              
140             Returns reference to hash with structure.
141              
142             =head2 C<struct2obj>
143              
144             my $obj = struct2obj($struct_hr);
145              
146             Convert structure of quantity to object.
147              
148             Returns Wikibase::Datatype::Value::Quantity instance.
149              
150             =head1 ERRORS
151              
152             obj2struct():
153             Base URI is required.
154             Object doesn't exist.
155             Object isn't 'Wikibase::Datatype::Value::Quantity'.
156              
157             struct2obj():
158             Structure isn't for 'quantity' datatype.
159              
160             =head1 EXAMPLE1
161              
162             use strict;
163             use warnings;
164              
165             use Data::Printer;
166             use Wikibase::Datatype::Value::Quantity;
167             use Wikibase::Datatype::Struct::Value::Quantity qw(obj2struct);
168              
169             # Object.
170             my $obj = Wikibase::Datatype::Value::Quantity->new(
171             'unit' => 'Q190900',
172             'value' => 10,
173             );
174              
175             # Get structure.
176             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
177              
178             # Dump to output.
179             p $struct_hr;
180              
181             # Output:
182             # \ {
183             # type "quantity",
184             # value {
185             # amount "+10",
186             # unit "http://test.wikidata.org/entity/Q190900"
187             # }
188             # }
189              
190             =head1 EXAMPLE2
191              
192             use strict;
193             use warnings;
194              
195             use Wikibase::Datatype::Struct::Value::Quantity qw(struct2obj);
196              
197             # Quantity structure.
198             my $struct_hr = {
199             'type' => 'quantity',
200             'value' => {
201             'amount' => '+10',
202             'unit' => 'http://test.wikidata.org/entity/Q190900',
203             },
204             };
205              
206             # Get object.
207             my $obj = struct2obj($struct_hr);
208              
209             # Get type.
210             my $type = $obj->type;
211              
212             # Get unit.
213             my $unit = $obj->unit;
214              
215             # Get value.
216             my $value = $obj->value;
217              
218             # Print out.
219             print "Type: $type\n";
220             if (defined $unit) {
221             print "Unit: $unit\n";
222             }
223             print "Value: $value\n";
224              
225             # Output:
226             # Type: quantity
227             # Unit: Q190900
228             # Value: 10
229              
230             =head1 DEPENDENCIES
231              
232             L<Error::Pure>,
233             L<Exporter>,
234             L<Readonly>,
235             L<URI>,
236             L<Wikibase::Datatype::Value::Property>.
237              
238             =head1 SEE ALSO
239              
240             =over
241              
242             =item L<Wikibase::Datatype::Struct>
243              
244             Wikibase structure serialization.
245              
246             =item L<Wikibase::Datatype::Value::Quantity>
247              
248             Wikibase quantity value datatype.
249              
250             =back
251              
252             =head1 REPOSITORY
253              
254             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
255              
256             =head1 AUTHOR
257              
258             Michal Josef Špaček L<mailto:skim@cpan.org>
259              
260             L<http://skim.cz>
261              
262             =head1 LICENSE AND COPYRIGHT
263              
264             © Michal Josef Špaček 2020-2021
265              
266             BSD 2-Clause License
267              
268             =head1 VERSION
269              
270             0.08
271              
272             =cut