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   377700 use base qw(Exporter);
  11         64  
  11         1244  
4 11     11   150 use strict;
  11         26  
  11         250  
5 11     11   60 use warnings;
  11         22  
  11         366  
6              
7 11     11   1644 use Error::Pure qw(err);
  11         25689  
  11         431  
8 11     11   235 use Readonly;
  11         22  
  11         494  
9 11     11   4911 use Wikibase::Datatype::MediainfoSnak;
  11         82010  
  11         339  
10 11     11   5020 use Wikibase::Datatype::Struct::Value;
  11         38  
  11         3180  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.08;
15              
16             sub obj2struct {
17 11     11 1 7819 my ($obj, $base_uri) = @_;
18              
19 11 100       34 if (! defined $obj) {
20 1         6 err "Object doesn't exist.";
21             }
22 10 100       50 if (! $obj->isa('Wikibase::Datatype::MediainfoSnak')) {
23 1         5 err "Object isn't 'Wikibase::Datatype::MediainfoSnak'.";
24             }
25 9 100       74 if (! defined $base_uri) {
26 1         4 err 'Base URI is required.';
27             }
28              
29 8 100       31 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         130 return $struct_hr;
38             }
39              
40             sub struct2obj {
41 6     6 1 5565 my $struct_hr = shift;
42              
43             # Data value isn't required for snaktype 'novalue'.
44 6         11 my $datavalue;
45 6 100       20 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       39 'snaktype' => $struct_hr->{'snaktype'},
55             );
56              
57 6         694 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             use strict;
113             use warnings;
114              
115             use Data::Printer;
116             use Wikibase::Datatype::MediainfoSnak;
117             use Wikibase::Datatype::Struct::MediainfoSnak qw(obj2struct);
118             use Wikibase::Datatype::Value::Item;
119              
120             # Object.
121             # instance of (P31) human (Q5)
122             my $obj = Wikibase::Datatype::MediainfoSnak->new(
123             'datavalue' => Wikibase::Datatype::Value::Item->new(
124             'value' => 'Q5',
125             ),
126             'property' => 'P31',
127             );
128              
129             # Get structure.
130             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
131              
132             # Dump to output.
133             p $struct_hr;
134              
135             # Output:
136             # \ {
137             # datavalue {
138             # type "wikibase-entityid",
139             # value {
140             # entity-type "item",
141             # id "Q5",
142             # numeric-id 5
143             # }
144             # },
145             # property "P31",
146             # snaktype "value"
147             # }
148              
149             =head1 EXAMPLE2
150              
151             use strict;
152             use warnings;
153              
154             use Wikibase::Datatype::Struct::MediainfoSnak qw(struct2obj);
155              
156             # Item structure.
157             my $struct_hr = {
158             'datavalue' => {
159             'type' => 'wikibase-entityid',
160             'value' => {
161             'entity-type' => 'item',
162             'id' => 'Q5',
163             'numeric-id' => 5,
164             },
165             },
166             'property' => 'P31',
167             'snaktype' => 'value',
168             };
169              
170             # Get object.
171             my $obj = struct2obj($struct_hr);
172              
173             # Get value.
174             my $datavalue = $obj->datavalue->value;
175              
176             # Get property.
177             my $property = $obj->property;
178              
179             # Get snak type.
180             my $snaktype = $obj->snaktype;
181              
182             # Print out.
183             print "Property: $property\n";
184             print "Value: $datavalue\n";
185             print "Snak type: $snaktype\n";
186              
187             # Output:
188             # Property: P31
189             # Value: Q5
190             # Snak type: value
191              
192             =head1 DEPENDENCIES
193              
194             L<Error::Pure>,
195             L<Exporter>,
196             L<Readonly>,
197             L<Wikibase::Datatype::MediainfoSnak>,
198             L<Wikibase::Datatype::Struct::Value>.
199              
200             =head1 SEE ALSO
201              
202             =over
203              
204             =item L<Wikibase::Datatype::Struct>
205              
206             Wikibase structure serialization.
207              
208             =item L<Wikibase::Datatype::Struct::Snak>
209              
210             Wikibase snak structure serialization.
211              
212             =item L<Wikibase::Datatype::Snak>
213              
214             Wikibase snak datatype.
215              
216             =back
217              
218             =head1 REPOSITORY
219              
220             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
221              
222             =head1 AUTHOR
223              
224             Michal Josef Špaček L<mailto:skim@cpan.org>
225              
226             L<http://skim.cz>
227              
228             =head1 LICENSE AND COPYRIGHT
229              
230             © Michal Josef Špaček 2020-2021
231              
232             BSD 2-Clause License
233              
234             =head1 VERSION
235              
236             0.08
237              
238             =cut