File Coverage

blib/lib/Wikibase/Datatype/Struct/Snak.pm
Criterion Covered Total %
statement 36 36 100.0
branch 10 10 100.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 57 57 100.0


line stmt bran cond sub pod time code
1              
2             use base qw(Exporter);
3 32     32   400363 use strict;
  32         82  
  32         2442  
4 32     32   161 use warnings;
  32         56  
  32         566  
5 32     32   130  
  32         50  
  32         710  
6             use Error::Pure qw(err);
7 32     32   1140 use Readonly;
  32         17357  
  32         967  
8 32     32   254 use Wikibase::Datatype::Snak;
  32         57  
  32         820  
9 32     32   8150 use Wikibase::Datatype::Struct::Value;
  32         1286800  
  32         762  
10 32     32   11048  
  32         89  
  32         6191  
11             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
12              
13             our $VERSION = 0.09;
14              
15             my ($obj, $base_uri) = @_;
16              
17 43     43 1 5868 if (! defined $obj) {
18             err "Object doesn't exist.";
19 43 100       94 }
20 1         4 if (! $obj->isa('Wikibase::Datatype::Snak')) {
21             err "Object isn't 'Wikibase::Datatype::Snak'.";
22 42 100       121 }
23 1         5 if (! defined $base_uri) {
24             err 'Base URI is required.';
25 41 100       72 }
26 1         3  
27             my $struct_hr = {
28             defined $obj->datavalue
29 40 100       110 ? ('datavalue' => Wikibase::Datatype::Struct::Value::obj2struct($obj->datavalue, $base_uri))
30             : (),
31             'datatype' => $obj->datatype,
32             'property' => $obj->property,
33             'snaktype' => $obj->snaktype,
34             };
35              
36             return $struct_hr;
37             }
38 40         679  
39             my $struct_hr = shift;
40              
41             # Data value isn't required for snaktype 'novalue'.
42 35     35 1 4653 my $datavalue;
43             if (exists $struct_hr->{'datavalue'}) {
44             $datavalue = Wikibase::Datatype::Struct::Value::struct2obj($struct_hr->{'datavalue'});
45 35         49 }
46 35 100       90  
47 32         85 my $obj = Wikibase::Datatype::Snak->new(
48             'datavalue' => $datavalue,
49             'datatype' => $struct_hr->{'datatype'},
50             'property' => $struct_hr->{'property'},
51             'snaktype' => $struct_hr->{'snaktype'},
52             );
53              
54 35         153 return $obj;
55             }
56              
57 32         5005 1;
58              
59              
60             =pod
61              
62             =encoding utf8
63              
64             =head1 NAME
65              
66             Wikibase::Datatype::Struct::Snak - Wikibase snak structure serialization.
67              
68             =head1 SYNOPSIS
69              
70             use Wikibase::Datatype::Struct::Snak qw(obj2struct struct2obj);
71              
72             my $struct_hr = obj2struct($obj, $base_uri);
73             my $obj = struct2obj($struct_hr);
74              
75             =head1 DESCRIPTION
76              
77             This conversion is between objects defined in Wikibase::Datatype and structures
78             serialized via JSON to MediaWiki.
79              
80             =head1 SUBROUTINES
81              
82             =head2 C<obj2struct>
83              
84             my $struct_hr = obj2struct($obj, $base_uri);
85              
86             Convert Wikibase::Datatype::Snak instance to structure.
87             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
88              
89             Returns reference to hash with structure.
90              
91             =head2 C<struct2obj>
92              
93             my $obj = struct2obj($struct_hr);
94              
95             Convert structure of snak to object.
96              
97             Returns Wikibase::Datatype::Snak instance.
98              
99             =head1 ERRORS
100              
101             obj2struct():
102             Base URI is required.
103             Object doesn't exist.
104             Object isn't 'Wikibase::Datatype::Snak'.
105              
106             =head1 EXAMPLE1
107              
108             use strict;
109             use warnings;
110              
111             use Data::Printer;
112             use Wikibase::Datatype::Snak;
113             use Wikibase::Datatype::Struct::Snak qw(obj2struct);
114             use Wikibase::Datatype::Value::Item;
115              
116             # Object.
117             # instance of (P31) human (Q5)
118             my $obj = Wikibase::Datatype::Snak->new(
119             'datatype' => 'wikibase-item',
120             'datavalue' => Wikibase::Datatype::Value::Item->new(
121             'value' => 'Q5',
122             ),
123             'property' => 'P31',
124             );
125              
126             # Get structure.
127             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
128              
129             # Dump to output.
130             p $struct_hr;
131              
132             # Output:
133             # \ {
134             # datatype "wikibase-item",
135             # datavalue {
136             # type "wikibase-entityid",
137             # value {
138             # entity-type "item",
139             # id "Q5",
140             # numeric-id 5
141             # }
142             # },
143             # property "P31",
144             # snaktype "value"
145             # }
146              
147             =head1 EXAMPLE2
148              
149             use strict;
150             use warnings;
151              
152             use Wikibase::Datatype::Struct::Snak qw(struct2obj);
153              
154             # Item structure.
155             my $struct_hr = {
156             'datatype' => 'wikibase-item',
157             'datavalue' => {
158             'type' => 'wikibase-entityid',
159             'value' => {
160             'entity-type' => 'item',
161             'id' => 'Q5',
162             'numeric-id' => 5,
163             },
164             },
165             'property' => 'P31',
166             'snaktype' => 'value',
167             };
168              
169             # Get object.
170             my $obj = struct2obj($struct_hr);
171              
172             # Get value.
173             my $datavalue = $obj->datavalue->value;
174              
175             # Get datatype.
176             my $datatype = $obj->datatype;
177              
178             # Get property.
179             my $property = $obj->property;
180              
181             # Print out.
182             print "Property: $property\n";
183             print "Type: $datatype\n";
184             print "Value: $datavalue\n";
185              
186             # Output:
187             # Property: P31
188             # Type: wikibase-item
189             # Value: Q5
190              
191             =head1 DEPENDENCIES
192              
193             L<Error::Pure>,
194             L<Exporter>,
195             L<Readonly>,
196             L<Wikibase::Datatype::Snak>,
197             L<Wikibase::Datatype::Struct::Value>.
198              
199             =head1 SEE ALSO
200              
201             =over
202              
203             =item L<Wikibase::Datatype::Struct>
204              
205             Wikibase structure serialization.
206              
207             =item L<Wikibase::Datatype::Snak>
208              
209             Wikibase snak datatype.
210              
211             =back
212              
213             =head1 REPOSITORY
214              
215             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
216              
217             =head1 AUTHOR
218              
219             Michal Josef Špaček L<mailto:skim@cpan.org>
220              
221             L<http://skim.cz>
222              
223             =head1 LICENSE AND COPYRIGHT
224              
225             © 2020-2022 Michal Josef Špaček
226              
227             BSD 2-Clause License
228              
229             =head1 VERSION
230              
231             0.09
232              
233             =cut