File Coverage

blib/lib/Wikibase/Datatype/Struct/Language.pm
Criterion Covered Total %
statement 28 28 100.0
branch 4 4 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 42 42 100.0


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