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