File Coverage

blib/lib/Wikibase/Datatype/Struct/Sitelink.pm
Criterion Covered Total %
statement 35 35 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Sitelink;
2              
3 8     8   473112 use base qw(Exporter);
  8         59  
  8         816  
4 8     8   54 use strict;
  8         17  
  8         151  
5 8     8   45 use warnings;
  8         20  
  8         210  
6              
7 8     8   1380 use Error::Pure qw(err);
  8         34215  
  8         254  
8 8     8   204 use Readonly;
  8         18  
  8         277  
9 8     8   3230 use Wikibase::Datatype::Sitelink;
  8         18892  
  8         295  
10 8     8   3633 use Wikibase::Datatype::Value::Item;
  8         2213063  
  8         1993  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.11;
15              
16             sub obj2struct {
17 5     5 1 4213 my $obj = shift;
18              
19 5 100       18 if (! defined $obj) {
20 1         4 err "Object doesn't exist.";
21             }
22 4 100       94 if (! $obj->isa('Wikibase::Datatype::Sitelink')) {
23 1         5 err "Object isn't 'Wikibase::Datatype::Sitelink'.";
24             }
25              
26             my $struct_hr = {
27             'badges' => [
28 3         10 map { $_->value } @{$obj->badges},
  2         34  
  3         12  
29             ],
30             'site' => $obj->site,
31             'title' => $obj->title,
32             };
33              
34 3         63 return $struct_hr;
35             }
36              
37             sub struct2obj {
38 3     3 1 152 my $struct_hr = shift;
39              
40             my $obj = Wikibase::Datatype::Sitelink->new(
41             'badges' => [
42 2         146 map { Wikibase::Datatype::Value::Item->new('value' => $_); }
43 3         17 @{$struct_hr->{'badges'}},
44             ],
45             'site' => $struct_hr->{'site'},
46 3         9 'title' => $struct_hr->{'title'},
47             );
48              
49 3         313 return $obj;
50             }
51              
52             1;
53              
54             __END__
55              
56             =pod
57              
58             =encoding utf8
59              
60             =head1 NAME
61              
62             Wikibase::Datatype::Struct::Sitelink - Wikibase sitelink structure serialization.
63              
64             =head1 SYNOPSIS
65              
66             use Wikibase::Datatype::Struct::Sitelink qw(obj2struct struct2obj);
67              
68             my $struct_hr = obj2struct($obj);
69             my $obj = struct2obj($struct_hr);
70              
71             =head1 DESCRIPTION
72              
73             This conversion is between objects defined in Wikibase::Datatype and structures
74             serialized via JSON to MediaWiki.
75              
76             =head1 SUBROUTINES
77              
78             =head2 C<obj2struct>
79              
80             my $struct_hr = obj2struct($obj);
81              
82             Convert Wikibase::Datatype::Sitelink instance to structure.
83              
84             Returns reference to hash with structure.
85              
86             =head2 C<struct2obj>
87              
88             my $obj = struct2obj($struct_hr);
89              
90             Convert structure of sitelink to object.
91              
92             Returns Wikibase::Datatype::Sitelink instance.
93              
94             =head1 ERRORS
95              
96             obj2struct():
97             Object doesn't exist.
98             Object isn't 'Wikibase::Datatype::Sitelink'.
99              
100             =head1 EXAMPLE1
101              
102             =for comment filename=obj2struct_sitelink.pl
103              
104             use strict;
105             use warnings;
106              
107             use Data::Printer;
108             use Wikibase::Datatype::Sitelink;
109             use Wikibase::Datatype::Struct::Sitelink qw(obj2struct);
110              
111             # Object.
112             my $obj = Wikibase::Datatype::Sitelink->new(
113             'site' => 'enwiki',
114             'title' => 'Main page',
115             );
116              
117             # Get structure.
118             my $struct_hr = obj2struct($obj);
119              
120             # Dump to output.
121             p $struct_hr;
122              
123             # Output:
124             # \ {
125             # badges [],
126             # site "enwiki",
127             # title "Main page"
128             # }
129              
130             =head1 EXAMPLE2
131              
132             =for comment filename=struct2obj_sitelink.pl
133              
134             use strict;
135             use warnings;
136              
137             use Wikibase::Datatype::Struct::Sitelink qw(struct2obj);
138              
139             # Item structure.
140             my $struct_hr = {
141             'badges' => [],
142             'site' => 'enwiki',
143             'title' => 'Main page',
144             };
145              
146             # Get object.
147             my $obj = struct2obj($struct_hr);
148              
149             # Get badges.
150             my $badges_ar = [map { $_->value } @{$obj->badges}];
151              
152             # Get site.
153             my $site = $obj->site;
154              
155             # Get title.
156             my $title = $obj->title;
157              
158             # Print out.
159             print 'Badges: '.(join ', ', @{$badges_ar})."\n";
160             print "Site: $site\n";
161             print "Title: $title\n";
162              
163             # Output:
164             # Badges:
165             # Site: enwiki
166             # Title: Main page
167              
168             =head1 DEPENDENCIES
169              
170             L<Error::Pure>,
171             L<Exporter>,
172             L<Readonly>,
173             L<Wikibase::Datatype::Sitelink>,
174             L<Wikibase::Datatype::Value::Item>.
175              
176             =head1 SEE ALSO
177              
178             =over
179              
180             =item L<Wikibase::Datatype::Struct>
181              
182             Wikibase structure serialization.
183              
184             =item L<Wikibase::Datatype::Sitelink>
185              
186             Wikibase sitelink datatype.
187              
188             =back
189              
190             =head1 REPOSITORY
191              
192             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
193              
194             =head1 AUTHOR
195              
196             Michal Josef Špaček L<mailto:skim@cpan.org>
197              
198             L<http://skim.cz>
199              
200             =head1 LICENSE AND COPYRIGHT
201              
202             © 2020-2023 Michal Josef Špaček
203              
204             BSD 2-Clause License
205              
206             =head1 VERSION
207              
208             0.11
209              
210             =cut