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