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 28     28   465994 use base qw(Exporter);
  28         102  
  28         2761  
4 28     28   189 use strict;
  28         63  
  28         545  
5 28     28   151 use warnings;
  28         59  
  28         834  
6              
7 28     28   1966 use Error::Pure qw(err);
  28         34924  
  28         1008  
8 28     28   365 use Readonly;
  28         78  
  28         1126  
9 28     28   10392 use Wikibase::Datatype::Value::Monolingual;
  28         9454754  
  28         5990  
10              
11             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
12              
13             our $VERSION = 0.11;
14              
15             sub obj2struct {
16 25     25 1 6241 my $obj = shift;
17              
18 25 100       84 if (! defined $obj) {
19 1         4 err "Object doesn't exist.";
20             }
21 24 100       107 if (! $obj->isa('Wikibase::Datatype::Value::Monolingual')) {
22 1         5 err "Object isn't 'Wikibase::Datatype::Value::Monolingual'.";
23             }
24              
25 23         68 my $struct_hr = {
26             'language' => $obj->language,
27             'value' => $obj->value,
28             };
29              
30 23         335 return $struct_hr;
31             }
32              
33             sub struct2obj {
34 23     23 1 168 my $struct_hr = shift;
35              
36             my $obj = Wikibase::Datatype::Value::Monolingual->new(
37             'language' => $struct_hr->{'language'},
38 23         152 'value' => $struct_hr->{'value'},
39             );
40              
41 23         46731 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             =for comment filename=obj2struct_language.pl
95              
96             use strict;
97             use warnings;
98              
99             use Data::Printer;
100             use Wikibase::Datatype::Value::Monolingual;
101             use Wikibase::Datatype::Struct::Value::Monolingual qw(obj2struct);
102              
103             # Object.
104             my $obj = Wikibase::Datatype::Value::Monolingual->new(
105             'language' => 'en',
106             'value' => 'English text',
107             );
108              
109             # Get structure.
110             my $struct_hr = obj2struct($obj);
111              
112             # Dump to output.
113             p $struct_hr;
114              
115             # Output:
116             # \ {
117             # language "en",
118             # value "English text"
119             # }
120              
121             =head1 EXAMPLE2
122              
123             =for comment filename=struct2obj_language.pl
124              
125             use strict;
126             use warnings;
127              
128             use Wikibase::Datatype::Struct::Language qw(struct2obj);
129              
130             # Monolingualtext structure.
131             my $struct_hr = {
132             'language' => 'en',
133             'value' => 'English text',
134             };
135              
136             # Get object.
137             my $obj = struct2obj($struct_hr);
138              
139             # Get language.
140             my $language = $obj->language;
141              
142             # Get type.
143             my $type = $obj->type;
144              
145             # Get value.
146             my $value = $obj->value;
147              
148             # Print out.
149             print "Language: $language\n";
150             print "Type: $type\n";
151             print "Value: $value\n";
152              
153             # Output:
154             # Language: en
155             # Type: monolingualtext
156             # Value: English text
157              
158             =head1 DEPENDENCIES
159              
160             L<Error::Pure>,
161             L<Exporter>,
162             L<Readonly>,
163             L<Wikibase::Datatype::Value::Monolingual>.
164              
165             =head1 SEE ALSO
166              
167             =over
168              
169             =item L<Wikibase::Datatype::Struct>
170              
171             Wikibase structure serialization.
172              
173             =item L<Wikibase::Datatype::Value::Monolingual>
174              
175             Wikibase monolingual value datatype.
176              
177             =item L<Wikibase::Datatype::Struct::Value::Monolingual>
178              
179             Wikibase monolingual structure serialization.
180              
181             =back
182              
183             =head1 REPOSITORY
184              
185             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
186              
187             =head1 AUTHOR
188              
189             Michal Josef Špaček L<mailto:skim@cpan.org>
190              
191             L<http://skim.cz>
192              
193             =head1 LICENSE AND COPYRIGHT
194              
195             © 2020-2023 Michal Josef Špaček
196              
197             BSD 2-Clause License
198              
199             =head1 VERSION
200              
201             0.11
202              
203             =cut