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