File Coverage

blib/lib/Wikibase/Datatype/Struct/Value/String.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::String;
2              
3 46     46   351727 use base qw(Exporter);
  46         140  
  46         5070  
4 46     46   385 use strict;
  46         137  
  46         1086  
5 46     46   243 use warnings;
  46         106  
  46         1642  
6              
7 46     46   1795 use Error::Pure qw(err);
  46         37195  
  46         1990  
8 46     46   490 use Readonly;
  46         107  
  46         2195  
9 46     46   20368 use Wikibase::Datatype::Value::String;
  46         26727  
  46         10413  
10              
11             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
12              
13             our $VERSION = 0.08;
14              
15             sub obj2struct {
16 21     21 1 19217 my $obj = shift;
17              
18 21 100       67 if (! defined $obj) {
19 1         4 err "Object doesn't exist.";
20             }
21 20 100       128 if (! $obj->isa('Wikibase::Datatype::Value::String')) {
22 1         5 err "Object isn't 'Wikibase::Datatype::Value::String'.";
23             }
24              
25 19         105 my $struct_hr = {
26             'type' => 'string',
27             'value' => $obj->value,
28             };
29              
30 19         187 return $struct_hr;
31             }
32              
33             sub struct2obj {
34 17     17 1 123 my $struct_hr = shift;
35              
36 17 50 33     108 if (! exists $struct_hr->{'type'}
37             || $struct_hr->{'type'} ne 'string') {
38              
39 0         0 err "Structure isn't for 'string' datatype.";
40             }
41              
42             my $obj = Wikibase::Datatype::Value::String->new(
43 17         146 'value' => $struct_hr->{'value'},
44             );
45              
46 17         1133 return $obj;
47             }
48              
49             1;
50              
51             __END__
52              
53             =pod
54              
55             =encoding utf8
56              
57             =head1 NAME
58              
59             Wikibase::Datatype::Struct::Value::String - Wikibase string structure serialization.
60              
61             =head1 SYNOPSIS
62              
63             use Wikibase::Datatype::Struct::Value::String 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::String 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 string to object.
88              
89             Returns Wikibase::Datatype::Value::String instance.
90              
91             =head1 ERRORS
92              
93             obj2struct():
94             Object doesn't exist.
95             Object isn't 'Wikibase::Datatype::Value::String'.
96              
97             struct2obj():
98             Structure isn't for 'string' datatype.
99              
100             =head1 EXAMPLE1
101              
102             use strict;
103             use warnings;
104              
105             use Data::Printer;
106             use Wikibase::Datatype::Value::String;
107             use Wikibase::Datatype::Struct::Value::String qw(obj2struct);
108              
109             # Object.
110             my $obj = Wikibase::Datatype::Value::String->new(
111             'value' => 'foo',
112             );
113              
114             # Get structure.
115             my $struct_hr = obj2struct($obj);
116              
117             # Dump to output.
118             p $struct_hr;
119              
120             # Output:
121             # \ {
122             # type "string",
123             # value "foo"
124             # }
125              
126             =head1 EXAMPLE2
127              
128             use strict;
129             use warnings;
130              
131             use Wikibase::Datatype::Struct::Value::String qw(struct2obj);
132              
133             # String structure.
134             my $struct_hr = {
135             'type' => 'string',
136             'value' => 'foo',
137             };
138              
139             # Get object.
140             my $obj = struct2obj($struct_hr);
141              
142             # Get type.
143             my $type = $obj->type;
144              
145             # Get value.
146             my $value = $obj->value;
147              
148             # Print out.
149             print "Type: $type\n";
150             print "Value: $value\n";
151              
152             # Output:
153             # Type: string
154             # Value: foo
155              
156             =head1 DEPENDENCIES
157              
158             L<Error::Pure>,
159             L<Exporter>,
160             L<Readonly>,
161             L<Wikibase::Datatype::Value::String>.
162              
163             =head1 SEE ALSO
164              
165             =over
166              
167             =item L<Wikibase::Datatype::Struct>
168              
169             Wikibase structure serialization.
170              
171             =item L<Wikibase::Datatype::Value::String>
172              
173             Wikibase string value datatype.
174              
175             =back
176              
177             =head1 REPOSITORY
178              
179             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
180              
181             =head1 AUTHOR
182              
183             Michal Josef Špaček L<mailto:skim@cpan.org>
184              
185             L<http://skim.cz>
186              
187             =head1 LICENSE AND COPYRIGHT
188              
189             © Michal Josef Špaček 2020-2021
190              
191             BSD 2-Clause License
192              
193             =head1 VERSION
194              
195             0.08
196              
197             =cut