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