File Coverage

blib/lib/Wikibase/Datatype/Struct/Value/Monolingual.pm
Criterion Covered Total %
statement 29 30 96.6
branch 5 6 83.3
condition 1 3 33.3
subroutine 8 8 100.0
pod 2 2 100.0
total 45 49 91.8


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