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 50     50   1032873 use base qw(Exporter);
  50         173  
  50         4865  
4 50     50   368 use strict;
  50         110  
  50         1040  
5 50     50   300 use warnings;
  50         158  
  50         1570  
6              
7 50     50   1669 use Error::Pure qw(err);
  50         34941  
  50         2166  
8 50     50   533 use Readonly;
  50         128  
  50         2132  
9 50     50   2729 use URI;
  50         19343  
  50         1503  
10 50     50   22868 use Wikibase::Datatype::Value::Quantity;
  50         1729728  
  50         30638  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.11;
15              
16             sub obj2struct {
17 7     7 1 7971 my ($obj, $base_uri) = @_;
18              
19 7 100       25 if (! defined $obj) {
20 1         4 err "Object doesn't exist.";
21             }
22 6 100       41 if (! $obj->isa('Wikibase::Datatype::Value::Quantity')) {
23 1         8 err "Object isn't 'Wikibase::Datatype::Value::Quantity'.";
24             }
25 5 100       17 if (! defined $base_uri) {
26 1         4 err 'Base URI is required.';
27             }
28              
29 4         17 my $amount = $obj->value;
30 4         44 $amount = _add_plus($amount);
31 4         8 my $unit;
32 4 100       17 if (defined $obj->unit) {
33 3         28 $unit = $base_uri.$obj->unit;
34             } else {
35 1         10 $unit = '1';
36             }
37 4 100       36 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         70 return $struct_hr;
52             }
53              
54             sub struct2obj {
55 4     4 1 6514 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         13 my $amount = $struct_hr->{'value'}->{'amount'};
64 4         12 $amount = _remove_plus($amount);
65 4         25 my $unit = $struct_hr->{'value'}->{'unit'};
66 4 100       16 if ($unit eq 1) {
67 1         4 $unit = undef;
68             } else {
69 3         52 my $u = URI->new($unit);
70 3         10218 my @path_segments = $u->path_segments;
71 3         301 $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       45 'upper_bound' => _remove_plus($struct_hr->{'value'}->{'upperBound'}),
    100          
80             ) : (),
81             'value' => $amount,
82             );
83              
84 4         591 return $obj;
85             }
86              
87             sub _add_plus {
88 6     6   36 my $value = shift;
89              
90 6 50       29 if ($value =~ m/^\d+$/) {
91 6         26 $value = '+'.$value;
92             }
93              
94 6         19 return $value;
95             }
96              
97             sub _remove_plus {
98 6     6   11 my $value = shift;
99              
100 6 50       37 if ($value =~ m/^\+(\d+)$/ms) {
101 6         18 $value = $1;
102             }
103              
104 6         21 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 value 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             =for comment filename=obj2struct_value_quantity.pl
163              
164             use strict;
165             use warnings;
166              
167             use Data::Printer;
168             use Wikibase::Datatype::Value::Quantity;
169             use Wikibase::Datatype::Struct::Value::Quantity qw(obj2struct);
170              
171             # Object.
172             my $obj = Wikibase::Datatype::Value::Quantity->new(
173             'unit' => 'Q190900',
174             'value' => 10,
175             );
176              
177             # Get structure.
178             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
179              
180             # Dump to output.
181             p $struct_hr;
182              
183             # Output:
184             # \ {
185             # type "quantity",
186             # value {
187             # amount "+10",
188             # unit "http://test.wikidata.org/entity/Q190900"
189             # }
190             # }
191              
192             =head1 EXAMPLE2
193              
194             =for comment filename=struct2obj_value_quantity.pl
195              
196             use strict;
197             use warnings;
198              
199             use Wikibase::Datatype::Struct::Value::Quantity qw(struct2obj);
200              
201             # Quantity structure.
202             my $struct_hr = {
203             'type' => 'quantity',
204             'value' => {
205             'amount' => '+10',
206             'unit' => 'http://test.wikidata.org/entity/Q190900',
207             },
208             };
209              
210             # Get object.
211             my $obj = struct2obj($struct_hr);
212              
213             # Get type.
214             my $type = $obj->type;
215              
216             # Get unit.
217             my $unit = $obj->unit;
218              
219             # Get value.
220             my $value = $obj->value;
221              
222             # Print out.
223             print "Type: $type\n";
224             if (defined $unit) {
225             print "Unit: $unit\n";
226             }
227             print "Value: $value\n";
228              
229             # Output:
230             # Type: quantity
231             # Unit: Q190900
232             # Value: 10
233              
234             =head1 DEPENDENCIES
235              
236             L<Error::Pure>,
237             L<Exporter>,
238             L<Readonly>,
239             L<URI>,
240             L<Wikibase::Datatype::Value::Property>.
241              
242             =head1 SEE ALSO
243              
244             =over
245              
246             =item L<Wikibase::Datatype::Struct>
247              
248             Wikibase structure serialization.
249              
250             =item L<Wikibase::Datatype::Value::Quantity>
251              
252             Wikibase quantity value datatype.
253              
254             =back
255              
256             =head1 REPOSITORY
257              
258             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
259              
260             =head1 AUTHOR
261              
262             Michal Josef Špaček L<mailto:skim@cpan.org>
263              
264             L<http://skim.cz>
265              
266             =head1 LICENSE AND COPYRIGHT
267              
268             © 2020-2023 Michal Josef Špaček
269              
270             BSD 2-Clause License
271              
272             =head1 VERSION
273              
274             0.11
275              
276             =cut