File Coverage

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


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