File Coverage

blib/lib/Wikibase/Datatype/Struct/Sense.pm
Criterion Covered Total %
statement 56 56 100.0
branch 6 6 100.0
condition 2 2 100.0
subroutine 10 10 100.0
pod 2 2 100.0
total 76 76 100.0


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