File Coverage

blib/lib/Wikibase/Datatype/Struct/MediainfoStatement.pm
Criterion Covered Total %
statement 36 46 78.2
branch 7 14 50.0
condition n/a
subroutine 10 11 90.9
pod 2 2 100.0
total 55 73 75.3


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::MediainfoStatement;
2              
3 6     6   231542 use base qw(Exporter);
  6         33  
  6         585  
4 6     6   44 use strict;
  6         16  
  6         126  
5 6     6   32 use warnings;
  6         14  
  6         175  
6              
7 6     6   972 use Error::Pure qw(err);
  6         23186  
  6         192  
8 6     6   134 use Readonly;
  6         32  
  6         219  
9 6     6   2534 use Wikibase::Datatype::MediainfoStatement;
  6         48779  
  6         263  
10 6     6   2686 use Wikibase::Datatype::Struct::MediainfoSnak;
  6         25  
  6         325  
11 6     6   2820 use Wikibase::Datatype::Struct::Reference;
  6         20  
  6         338  
12 6     6   55 use Wikibase::Datatype::Struct::Utils qw(obj_array_ref2struct struct2snaks_array_ref);
  6         24  
  6         2303  
13              
14             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
15              
16             our $VERSION = 0.11;
17              
18             sub obj2struct {
19 4     4 1 45 my ($obj, $base_uri) = @_;
20              
21 4 50       14 if (! defined $obj) {
22 0         0 err "Object doesn't exist.";
23             }
24 4 50       16 if (! $obj->isa('Wikibase::Datatype::MediainfoStatement')) {
25 0         0 err "Object isn't 'Wikibase::Datatype::MediainfoStatement'.";
26             }
27 4 50       9 if (! defined $base_uri) {
28 0         0 err 'Base URI is required.';
29             }
30              
31             my $struct_hr = {
32             defined $obj->id ? ('id' => $obj->id) : (),
33             'mainsnak' => Wikibase::Datatype::Struct::MediainfoSnak::obj2struct($obj->snak, $base_uri),
34 4         12 @{$obj->property_snaks} ? (
35 1         19 %{obj_array_ref2struct($obj->property_snaks, 'qualifiers', $base_uri,
36             'Wikibase::Datatype::MediainfoSnak', 'Wikibase::Datatype::Struct::MediainfoSnak')},
37             ) : (),
38             'rank' => $obj->rank,
39 4         73 @{$obj->references} ? (
40             'references' => [
41 0         0 map { Wikibase::Datatype::Struct::Reference::obj2struct($_, $base_uri); }
42 4 50       13 @{$obj->references},
  0 100       0  
    50          
43             ],
44             ) : (),
45             'type' => 'statement',
46             };
47              
48 4         52 return $struct_hr;
49             }
50              
51             sub struct2obj {
52 0     0 1   my $struct_hr = shift;
53              
54             my $obj = Wikibase::Datatype::MediainfoStatement->new(
55             exists $struct_hr->{'id'} ? ('id' => $struct_hr->{'id'}) : (),
56             'property_snaks' => struct2snaks_array_ref($struct_hr, 'qualifiers',
57             'Wikibase::Datatype::Struct::MediainfoSnak'),
58             'snak' => Wikibase::Datatype::Struct::MediainfoSnak::struct2obj($struct_hr->{'mainsnak'}),
59             'references' => [
60 0           map { Wikibase::Datatype::Struct::Reference::struct2obj($_) }
61 0           @{$struct_hr->{'references'}}
62             ],
63 0 0         'rank' => $struct_hr->{'rank'},
64             );
65              
66 0           return $obj;
67             }
68              
69             1;
70              
71             __END__
72              
73             =pod
74              
75             =encoding utf8
76              
77             =head1 NAME
78              
79             Wikibase::Datatype::Struct::MediainfoStatement - Wikibase mediainfo statement structure serialization.
80              
81             =head1 SYNOPSIS
82              
83             use Wikibase::Datatype::Struct::MediainfoStatement qw(obj2struct struct2obj);
84              
85             my $struct_hr = obj2struct($obj, $base_uri);
86             my $obj = struct2obj($struct_hr);
87              
88             =head1 DESCRIPTION
89              
90             This conversion is between objects defined in Wikibase::Datatype and structures
91             serialized via JSON to MediaWiki.
92              
93             =head1 SUBROUTINES
94              
95             =head2 C<obj2struct>
96              
97             my $struct_hr = obj2struct($obj, $base_uri);
98              
99             Convert Wikibase::Datatype::MediainfoStatement instance to structure.
100             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
101              
102             Returns reference to hash with structure.
103              
104             =head2 C<struct2obj>
105              
106             my $obj = struct2obj($struct_hr);
107              
108             Convert structure of mediainfo statement to object.
109              
110             Returns Wikibase::Datatype::MediainfoStatement instance.
111              
112             =head1 ERRORS
113              
114             obj2struct():
115             Base URI is required.
116             Object doesn't exist.
117             Object isn't 'Wikibase::Datatype::MediainfoStatement'.
118              
119             =head1 EXAMPLE1
120              
121             =for comment filename=obj2struct_mediainfo_statement.pl
122              
123             use strict;
124             use warnings;
125              
126             use Data::Printer;
127             use Wikibase::Datatype::MediainfoSnak;
128             use Wikibase::Datatype::MediainfoStatement;
129             use Wikibase::Datatype::Struct::MediainfoStatement qw(obj2struct);
130             use Wikibase::Datatype::Value::Item;
131              
132             # Object.
133             my $obj = Wikibase::Datatype::MediainfoStatement->new(
134             'id' => 'M123$00C04D2A-49AF-40C2-9930-C551916887E8',
135              
136             # instance of (P31) human (Q5)
137             'snak' => Wikibase::Datatype::MediainfoSnak->new(
138             'datavalue' => Wikibase::Datatype::Value::Item->new(
139             'value' => 'Q5',
140             ),
141             'property' => 'P31',
142             ),
143             'property_snaks' => [
144             # of (P642) alien (Q474741)
145             Wikibase::Datatype::MediainfoSnak->new(
146             'datavalue' => Wikibase::Datatype::Value::Item->new(
147             'value' => 'Q474741',
148             ),
149             'property' => 'P642',
150             ),
151             ],
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             # id "M123$00C04D2A-49AF-40C2-9930-C551916887E8",
163             # mainsnak {
164             # datavalue {
165             # type "wikibase-entityid",
166             # value {
167             # entity-type "item",
168             # id "Q5",
169             # numeric-id 5
170             # }
171             # },
172             # property "P31",
173             # snaktype "value"
174             # },
175             # qualifiers {
176             # P642 [
177             # [0] {
178             # datavalue {
179             # type "wikibase-entityid",
180             # value {
181             # entity-type "item",
182             # id "Q474741",
183             # numeric-id 474741
184             # }
185             # },
186             # property "P642",
187             # snaktype "value"
188             # }
189             # ]
190             # },
191             # qualifiers-order [
192             # [0] "P642"
193             # ],
194             # rank "normal",
195             # type "statement"
196             # }
197              
198             =head1 EXAMPLE2
199              
200             =for comment filename=struct2obj_mediainfo_statement.pl
201              
202             use strict;
203             use warnings;
204              
205             use Wikibase::Datatype::Struct::MediainfoStatement qw(struct2obj);
206              
207             # Item structure.
208             my $struct_hr = {
209             'id' => 'M123$00C04D2A-49AF-40C2-9930-C551916887E8',
210             'mainsnak' => {
211             'datavalue' => {
212             'type' => 'wikibase-entityid',
213             'value' => {
214             'entity-type' => 'item',
215             'id' => 'Q5',
216             'numeric-id' => 5,
217             },
218             },
219             'property' => 'P31',
220             'snaktype' => 'value',
221             },
222             'qualifiers' => {
223             'P642' => [{
224             'datavalue' => {
225             'type' => 'wikibase-entityid',
226             'value' => {
227             'entity-type' => 'item',
228             'id' => 'Q474741',
229             'numeric-id' => 474741,
230             },
231             },
232             'property' => 'P642',
233             'snaktype' => 'value',
234             }],
235             },
236             'qualifiers-order' => [
237             'P642',
238             ],
239             'rank' => 'normal',
240             'type' => 'statement',
241             };
242              
243             # Get object.
244             my $obj = struct2obj($struct_hr);
245              
246             # Print out.
247             print 'Id: '.$obj->id."\n";
248             print 'Statements: '.$obj->snak->property.' -> '.$obj->snak->datavalue->value."\n";
249             print "Qualifiers:\n";
250             foreach my $property_snak (@{$obj->property_snaks}) {
251             print "\t".$property_snak->property.' -> '.
252             $property_snak->datavalue->value."\n";
253             }
254             print 'Rank: '.$obj->rank."\n";
255              
256             # Output:
257             # Id: M123$00C04D2A-49AF-40C2-9930-C551916887E8
258             # Statements: P31 -> Q5
259             # Qualifiers:
260             # P642 -> Q474741
261             # Rank: normal
262              
263             =head1 DEPENDENCIES
264              
265             L<Error::Pure>,
266             L<Exporter>,
267             L<Readonly>,
268             L<Wikibase::Datatype::MediainfoStatement>,
269             L<Wikibase::Datatype::Struct::Reference>,
270             L<Wikibase::Datatype::Struct::Snak>,
271             L<Wikibase::Datatype::Struct::Utils>.
272              
273             =head1 SEE ALSO
274              
275             =over
276              
277             =item L<Wikibase::Datatype::Struct>
278              
279             Wikibase structure serialization.
280              
281             =item L<Wikibase::Datatype::Statement>
282              
283             Wikibase statement datatype.
284              
285             =item L<Wikibase::Datatype::MediainfoStatement>
286              
287             Wikibase mediainfo statement datatype.
288              
289             =back
290              
291             =head1 REPOSITORY
292              
293             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
294              
295             =head1 AUTHOR
296              
297             Michal Josef Špaček L<mailto:skim@cpan.org>
298              
299             L<http://skim.cz>
300              
301             =head1 LICENSE AND COPYRIGHT
302              
303             © 2020-2023 Michal Josef Špaček
304              
305             BSD 2-Clause License
306              
307             =head1 VERSION
308              
309             0.11
310              
311             =cut