File Coverage

blib/lib/Wikibase/Datatype/Struct/Form.pm
Criterion Covered Total %
statement 68 68 100.0
branch 6 6 100.0
condition 2 2 100.0
subroutine 11 11 100.0
pod 2 2 100.0
total 89 89 100.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Form;
2              
3 8     8   490253 use base qw(Exporter);
  8         64  
  8         1202  
4 8     8   55 use strict;
  8         21  
  8         182  
5 8     8   44 use warnings;
  8         25  
  8         218  
6              
7 8     8   1510 use Error::Pure qw(err);
  8         35534  
  8         309  
8 8     8   204 use Readonly;
  8         20  
  8         285  
9 8     8   2116 use Wikibase::Datatype::Form;
  8         17600  
  8         217  
10 8     8   3508 use Wikibase::Datatype::Struct::Language;
  8         34  
  8         423  
11 8     8   4053 use Wikibase::Datatype::Struct::Statement;
  8         25  
  8         403  
12 8     8   73 use Wikibase::Datatype::Value::Item;
  8         19  
  8         3835  
13              
14             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
15              
16             our $VERSION = 0.11;
17              
18             sub obj2struct {
19 5     5 1 12471 my ($obj, $base_uri) = @_;
20              
21 5 100       18 if (! defined $obj) {
22 1         5 err "Object doesn't exist.";
23             }
24 4 100       26 if (! $obj->isa('Wikibase::Datatype::Form')) {
25 1         5 err "Object isn't 'Wikibase::Datatype::Form'.";
26             }
27 3 100       11 if (! defined $base_uri) {
28 1         4 err 'Base URI is required.';
29             }
30              
31 2         14 my $struct_hr = {
32             'id' => $obj->id,
33             };
34              
35             # Grammatical features.
36 2         23 foreach my $gf (@{$obj->grammatical_features}) {
  2         11  
37 4         49 push @{$struct_hr->{'grammaticalFeatures'}}, $gf->value;
  4         27  
38             }
39              
40             # Representations.
41 2         23 foreach my $rep (@{$obj->representations}) {
  2         13  
42 3         45 $struct_hr->{'representations'}->{$rep->language}
43             = Wikibase::Datatype::Struct::Language::obj2struct($rep);
44             }
45              
46             # Statements.
47 2         18 foreach my $statement (@{$obj->statements}) {
  2         17  
48 3   100     32 $struct_hr->{'claims'}->{$statement->snak->property} //= [];
49 3         53 push @{$struct_hr->{'claims'}->{$statement->snak->property}},
  3         11  
50             Wikibase::Datatype::Struct::Statement::obj2struct($statement, $base_uri);
51             }
52              
53 2         7 return $struct_hr;
54             }
55              
56             sub struct2obj {
57 2     2 1 155 my $struct_hr = shift;
58              
59             # Grammatical features.
60 2         6 my $gm_ar = [];
61 2         5 foreach my $gm (@{$struct_hr->{'grammaticalFeatures'}}) {
  2         8  
62 4         264 push @{$gm_ar}, Wikibase::Datatype::Value::Item->new(
  4         28  
63             'value' => $gm,
64             );
65             }
66              
67             # Representations.
68 2         151 my $representations_ar;
69 2         4 foreach my $lang (keys %{$struct_hr->{'representations'}}) {
  2         15  
70 3         19 push @{$representations_ar}, Wikibase::Datatype::Struct::Language::struct2obj(
71 3         7 $struct_hr->{'representations'}->{$lang});
72             }
73              
74             # Statements.
75 2         7 my $statements_ar = [];
76 2         7 foreach my $property (keys %{$struct_hr->{'claims'}}) {
  2         11  
77 2         8 foreach my $claim_hr (@{$struct_hr->{'claims'}->{$property}}) {
  2         8  
78 2         5 push @{$statements_ar}, Wikibase::Datatype::Struct::Statement::struct2obj(
  2         11  
79             $claim_hr,
80             );
81             }
82             }
83              
84             my $obj = Wikibase::Datatype::Form->new(
85             'grammatical_features' => $gm_ar,
86 2         18 'id' => $struct_hr->{'id'},
87             'representations' => $representations_ar,
88             'statements' => $statements_ar,
89             );
90              
91 2         231 return $obj;
92             }
93              
94             1;
95              
96             __END__
97              
98             =pod
99              
100             =encoding utf8
101              
102             =head1 NAME
103              
104             Wikibase::Datatype::Struct::Form - Wikibase form structure serialization.
105              
106             =head1 SYNOPSIS
107              
108             use Wikibase::Datatype::Struct::Form qw(obj2struct struct2obj);
109              
110             my $struct_hr = obj2struct($obj, $base_uri);
111             my $obj = struct2obj($struct_hr);
112              
113             =head1 DESCRIPTION
114              
115             This conversion is between objects defined in Wikibase::Datatype and structures
116             serialized via JSON to MediaWiki.
117              
118             =head1 SUBROUTINES
119              
120             =head2 C<obj2struct>
121              
122             my $struct_hr = obj2struct($obj, $base_uri);
123              
124             Convert Wikibase::Datatype::Form instance to structure.
125             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
126              
127             Returns reference to hash with structure.
128              
129             =head2 C<struct2obj>
130              
131             my $obj = struct2obj($struct_hr);
132              
133             Convert structure of form to object.
134              
135             Returns Wikibase::Datatype::Form instance.
136              
137             =head1 ERRORS
138              
139             obj2struct():
140             Base URI is required.
141             Object doesn't exist.
142             Object isn't 'Wikibase::Datatype::Form'.
143              
144             =head1 EXAMPLE1
145              
146             =for comment filename=obj2struct_form.pl
147              
148             use strict;
149             use warnings;
150              
151             use Data::Printer;
152             use Wikibase::Datatype::Form;
153             use Wikibase::Datatype::Statement;
154             use Wikibase::Datatype::Struct::Form qw(obj2struct);
155             use Wikibase::Datatype::Value::Monolingual;
156              
157             # Statement.
158             my $statement = Wikibase::Datatype::Statement->new(
159             # instance of (P31) human (Q5)
160             'snak' => Wikibase::Datatype::Snak->new(
161             'datatype' => 'wikibase-item',
162             'datavalue' => Wikibase::Datatype::Value::Item->new(
163             'value' => 'Q5',
164             ),
165             'property' => 'P31',
166             ),
167             );
168              
169             # Object.
170             my $obj = Wikibase::Datatype::Form->new(
171             'grammatical_features' => [
172             Wikibase::Datatype::Value::Item->new(
173             'value' => 'Q163012',
174             ),
175             Wikibase::Datatype::Value::Item->new(
176             'value' => 'Q163014',
177             ),
178             ],
179             'id' => 'ID',
180             'representations' => [
181             Wikibase::Datatype::Value::Monolingual->new(
182             'language' => 'en',
183             'value' => 'Representation en',
184             ),
185             Wikibase::Datatype::Value::Monolingual->new(
186             'language' => 'cs',
187             'value' => 'Representation cs',
188             ),
189             ],
190             'statements' => [
191             $statement,
192             ],
193             );
194              
195             # Get structure.
196             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
197              
198             # Dump to output.
199             p $struct_hr;
200              
201             # Output:
202             # \ {
203             # claims {
204             # P31 [
205             # [0] {
206             # mainsnak {
207             # datatype "wikibase-item",
208             # datavalue {
209             # type "wikibase-entityid",
210             # value {
211             # entity-type "item",
212             # id "Q5",
213             # numeric-id 5
214             # }
215             # },
216             # property "P31",
217             # snaktype "value"
218             # },
219             # rank "normal",
220             # type "statement"
221             # }
222             # ]
223             # },
224             # grammaticalFeatures [
225             # [0] "Q163012",
226             # [1] "Q163014"
227             # ],
228             # id "ID",
229             # represenations {
230             # cs {
231             # language "cs",
232             # value "Representation cs"
233             # },
234             # en {
235             # language "en",
236             # value "Representation en"
237             # }
238             # }
239             # }
240              
241             =head1 EXAMPLE2
242              
243             =for comment filename=struct2obj_form.pl
244              
245             use strict;
246             use warnings;
247              
248             use Data::Printer;
249             use Wikibase::Datatype::Struct::Form qw(struct2obj);
250              
251             # Item structure.
252             my $struct_hr = {
253             'grammaticalFeatures' => [
254             'Q163012',
255             'Q163014',
256             ],
257             'id' => 'ID',
258             'representations' => {
259             'cs' => {
260             'language' => 'cs',
261             'value' => 'Representation cs',
262             },
263             'en' => {
264             'language' => 'en',
265             'value' => 'Representation en',
266             },
267             },
268             'claims' => {
269             'P31' => [{
270             'mainsnak' => {
271             'datatype' => 'wikibase-item',
272             'datavalue' => {
273             'type' => 'wikibase-entityid',
274             'value' => {
275             'entity-type' => 'item',
276             'id' => 'Q5',
277             'numeric-id' => 5,
278             },
279             },
280             'property' => 'P31',
281             'snaktype' => 'value',
282             },
283             'rank' => 'normal',
284             'type' => 'statement',
285             }],
286             },
287             };
288              
289             # Get object.
290             my $obj = struct2obj($struct_hr);
291              
292             # Dump object.
293             p $obj;
294              
295             # Output:
296             # Wikibase::Datatype::Form {
297             # Parents Mo::Object
298             # public methods (6) : BUILD, can (UNIVERSAL), DOES (UNIVERSAL), check_array_object (Mo::utils), isa (UNIVERSAL), VERSION (UNIVERSAL)
299             # private methods (1) : __ANON__ (Mo::is)
300             # internals: {
301             # grammatical_features [
302             # [0] Wikibase::Datatype::Value::Item,
303             # [1] Wikibase::Datatype::Value::Item
304             # ],
305             # id "ID",
306             # represenations undef,
307             # statements [
308             # [0] Wikibase::Datatype::Statement
309             # ]
310             # }
311             # }
312              
313             =head1 DEPENDENCIES
314              
315             L<Error::Pure>,
316             L<Exporter>,
317             L<Readonly>,
318             L<Wikibase::Datatype::Form>,
319             L<Wikibase::Datatype::Struct::Language>,
320             L<Wikibase::Datatype::Struct::Statement>,
321             L<Wikibase::Datatype::Value::Item>.
322              
323             =head1 SEE ALSO
324              
325             =over
326              
327             =item L<Wikibase::Datatype::Struct>
328              
329             Wikibase structure serialization.
330              
331             =item L<Wikibase::Datatype::Form>
332              
333             Wikibase form datatype.
334              
335             =back
336              
337             =head1 REPOSITORY
338              
339             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
340              
341             =head1 AUTHOR
342              
343             Michal Josef Špaček L<mailto:skim@cpan.org>
344              
345             L<http://skim.cz>
346              
347             =head1 LICENSE AND COPYRIGHT
348              
349             © 2020-2023 Michal Josef Špaček
350              
351             BSD 2-Clause License
352              
353             =head1 VERSION
354              
355             0.11
356              
357             =cut