File Coverage

blib/lib/Wikibase/Datatype/Struct/Value.pm
Criterion Covered Total %
statement 70 72 97.2
branch 34 36 94.4
condition n/a
subroutine 15 15 100.0
pod 2 2 100.0
total 121 125 96.8


line stmt bran cond sub pod time code
1              
2             use base qw(Exporter);
3 46     46   462241 use strict;
  46         110  
  46         3770  
4 46     46   237 use warnings;
  46         89  
  46         683  
5 46     46   214  
  46         82  
  46         955  
6             use Error::Pure qw(err);
7 46     46   1247 use Readonly;
  46         19105  
  46         1636  
8 46     46   322 use Wikibase::Datatype::Struct::Value::Globecoordinate;
  46         76  
  46         1267  
9 46     46   15119 use Wikibase::Datatype::Struct::Value::Item;
  46         109  
  46         1723  
10 46     46   14817 use Wikibase::Datatype::Struct::Value::Monolingual;
  46         277  
  46         1653  
11 46     46   14616 use Wikibase::Datatype::Struct::Value::Property;
  46         111  
  46         1519  
12 46     46   14396 use Wikibase::Datatype::Struct::Value::Quantity;
  46         164  
  46         1507  
13 46     46   14511 use Wikibase::Datatype::Struct::Value::String;
  46         125  
  46         1508  
14 46     46   14427 use Wikibase::Datatype::Struct::Value::Time;
  46         144  
  46         1468  
15 46     46   14401 use Wikibase::Datatype::Value;
  46         98  
  46         1481  
16 46     46   244  
  46         134  
  46         16979  
17             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
18              
19             our $VERSION = 0.09;
20              
21             my ($obj, $base_uri) = @_;
22              
23 52     52 1 23120 if (! defined $obj) {
24             err "Object doesn't exist.";
25 52 100       185 }
26 1         4 if (! $obj->isa('Wikibase::Datatype::Value')) {
27             err "Object isn't 'Wikibase::Datatype::Value'.";
28 51 100       166 }
29 1         4  
30             my $struct_hr;
31             my $type = $obj->type;
32 50         67 if ($type eq 'globecoordinate') {
33 50         154 $struct_hr = Wikibase::Datatype::Struct::Value::Globecoordinate::obj2struct($obj, $base_uri);
34 50 100       300 } elsif ($type eq 'item') {
    100          
    100          
    100          
    100          
    100          
    100          
35 1         4 $struct_hr = Wikibase::Datatype::Struct::Value::Item::obj2struct($obj);
36             } elsif ($type eq 'monolingualtext') {
37 19         51 $struct_hr = Wikibase::Datatype::Struct::Value::Monolingual::obj2struct($obj);
38             } elsif ($type eq 'property') {
39 1         5 $struct_hr = Wikibase::Datatype::Struct::Value::Property::obj2struct($obj);
40             } elsif ($type eq 'quantity') {
41 1         4 $struct_hr = Wikibase::Datatype::Struct::Value::Quantity::obj2struct($obj, $base_uri);
42             } elsif ($type eq 'string') {
43 1         5 $struct_hr = Wikibase::Datatype::Struct::Value::String::obj2struct($obj);
44             } elsif ($type eq 'time') {
45 21         55 $struct_hr = Wikibase::Datatype::Struct::Value::Time::obj2struct($obj, $base_uri);
46             } else {
47 5         17 err "Type '$type' is unsupported.";
48             }
49 1         5  
50             return $struct_hr;
51             }
52 49         132  
53             my $struct_hr = shift;
54              
55             if (! exists $struct_hr->{'type'}) {
56 43     43 1 13590 err "Type doesn't exist.";
57             }
58 43 50       107  
59 0         0 my $obj;
60             if ($struct_hr->{'type'} eq 'globecoordinate') {
61             $obj = Wikibase::Datatype::Struct::Value::Globecoordinate::struct2obj($struct_hr);
62 43         62 } elsif ($struct_hr->{'type'} eq 'monolingualtext') {
63 43 100       240 $obj = Wikibase::Datatype::Struct::Value::Monolingual::struct2obj($struct_hr);
    100          
    100          
    100          
    100          
    100          
64 1         5 } elsif ($struct_hr->{'type'} eq 'quantity') {
65             $obj = Wikibase::Datatype::Struct::Value::Quantity::struct2obj($struct_hr);
66 1         6 } elsif ($struct_hr->{'type'} eq 'string') {
67             $obj = Wikibase::Datatype::Struct::Value::String::struct2obj($struct_hr);
68 1         4 } elsif ($struct_hr->{'type'} eq 'time') {
69             $obj = Wikibase::Datatype::Struct::Value::Time::struct2obj($struct_hr);
70 19         61 } elsif ($struct_hr->{'type'} eq 'wikibase-entityid') {
71             if ($struct_hr->{'value'}->{'entity-type'} eq 'item') {
72 5         23 $obj = Wikibase::Datatype::Struct::Value::Item::struct2obj($struct_hr);
73             } elsif ($struct_hr->{'value'}->{'entity-type'} eq 'property') {
74 15 100       46 $obj = Wikibase::Datatype::Struct::Value::Property::struct2obj($struct_hr);
    50          
75 14         48 } else {
76             err "Entity type '$struct_hr->{'value'}->{'entity-type'}' is unsupported.";
77 1         4 }
78             } else {
79 0         0 err "Type '$struct_hr->{'type'}' is unsupported.";
80             }
81              
82 1         5 return $obj;
83             }
84              
85 42         131 1;
86              
87              
88             =pod
89              
90             =encoding utf8
91              
92             =head1 NAME
93              
94             Wikibase::Datatype::Struct::Value - Wikibase value structure serialization.
95              
96             =head1 SYNOPSIS
97              
98             use Wikibase::Datatype::Struct::Value qw(obj2struct struct2obj);
99              
100             my $struct_hr = obj2struct($obj, $base_uri);
101             my $obj = struct2obj($struct_hr);
102              
103             =head1 DESCRIPTION
104              
105             This conversion is between objects defined in Wikibase::Datatype and structures
106             serialized via JSON to MediaWiki.
107              
108             =head1 SUBROUTINES
109              
110             =head2 C<obj2struct>
111              
112             my $struct_hr = obj2struct($obj, $base_uri);
113              
114             Convert Wikibase::Datatype::Value instance to structure.
115             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
116              
117             Returns reference to hash with structure.
118              
119             =head2 C<struct2obj>
120              
121             my $obj = struct2obj($struct_hr);
122              
123             Convert structure of value to object.
124              
125             Returns Wikibase::Datatype::Value instance.
126              
127             =head1 ERRORS
128              
129             obj2struct():
130             Object doesn't exist.
131             Object isn't 'Wikibase::Datatype::Value'.
132             Type '%s' is unsupported.
133              
134             struct2obj():
135             Entity type '%s' is unsupported.
136             Type doesn't exist.
137             Type '%s' is unsupported.
138              
139             =head1 EXAMPLE1
140              
141             use strict;
142             use warnings;
143              
144             use Data::Printer;
145             use Wikibase::Datatype::Value::Time;
146             use Wikibase::Datatype::Struct::Value qw(obj2struct);
147              
148             # Object.
149             my $obj = Wikibase::Datatype::Value::Time->new(
150             'precision' => 10,
151             'value' => '+2020-09-01T00:00:00Z',
152             );
153              
154             # Get structure.
155             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
156              
157             # Dump to output.
158             p $struct_hr;
159              
160             # Output:
161             # \ {
162             # type "time",
163             # value {
164             # after 0,
165             # before 0,
166             # calendarmodel "http://test.wikidata.org/entity/Q1985727",
167             # precision 10,
168             # time "+2020-09-01T00:00:00Z",
169             # timezone 0
170             # }
171             # }
172              
173             =head1 EXAMPLE2
174              
175             use strict;
176             use warnings;
177              
178             use Wikibase::Datatype::Struct::Value qw(struct2obj);
179              
180             # Time structure.
181             my $struct_hr = {
182             'type' => 'time',
183             'value' => {
184             'after' => 0,
185             'before' => 0,
186             'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
187             'precision' => 10,
188             'time' => '+2020-09-01T00:00:00Z',
189             'timezone' => 0,
190             },
191             };
192              
193             # Get object.
194             my $obj = struct2obj($struct_hr);
195              
196             # Get calendar model.
197             my $calendarmodel = $obj->calendarmodel;
198              
199             # Get precision.
200             my $precision = $obj->precision;
201              
202             # Get type.
203             my $type = $obj->type;
204              
205             # Get value.
206             my $value = $obj->value;
207              
208             # Print out.
209             print "Calendar model: $calendarmodel\n";
210             print "Precision: $precision\n";
211             print "Type: $type\n";
212             print "Value: $value\n";
213              
214             # Output:
215             # Calendar model: Q1985727
216             # Precision: 10
217             # Type: time
218             # Value: +2020-09-01T00:00:00Z
219              
220             =head1 DEPENDENCIES
221              
222             L<Error::Pure>,
223             L<Exporter>,
224             L<Readonly>,
225             L<Wikibase::Datatype::Value>,
226             L<Wikibase::Datatype::Struct::Value::Globecoordinate>,
227             L<Wikibase::Datatype::Struct::Value::Item>,
228             L<Wikibase::Datatype::Struct::Value::Monolingual>,
229             L<Wikibase::Datatype::Struct::Value::Property>,
230             L<Wikibase::Datatype::Struct::Value::Quantity>,
231             L<Wikibase::Datatype::Struct::Value::String>,
232             L<Wikibase::Datatype::Struct::Value::Time>.
233              
234             =head1 SEE ALSO
235              
236             =over
237              
238             =item L<Wikibase::Datatype::Struct>
239              
240             Wikibase structure serialization.
241              
242             =item L<Wikibase::Datatype::Value::Globecoordinate>
243              
244             Wikibase globe coordinate value datatype.
245              
246             =item L<Wikibase::Datatype::Value::Item>
247              
248             Wikibase item value datatype.
249              
250             =item L<Wikibase::Datatype::Value::Monolingual>
251              
252             Wikibase monolingual value datatype.
253              
254             =item L<Wikibase::Datatype::Value::Property>
255              
256             Wikibase property value datatype.
257              
258             =item L<Wikibase::Datatype::Value::Quantity>
259              
260             Wikibase quantity value datatype.
261              
262             =item L<Wikibase::Datatype::Value::String>
263              
264             Wikibase string value datatype.
265              
266             =item L<Wikibase::Datatype::Value::Time>
267              
268             Wikibase time value datatype.
269              
270             =back
271              
272             =head1 REPOSITORY
273              
274             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
275              
276             =head1 AUTHOR
277              
278             Michal Josef Špaček L<mailto:skim@cpan.org>
279              
280             L<http://skim.cz>
281              
282             =head1 LICENSE AND COPYRIGHT
283              
284             © 2020-2022 Michal Josef Špaček
285              
286             BSD 2-Clause License
287              
288             =head1 VERSION
289              
290             0.09
291              
292             =cut