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 50     50   471089 use base qw(Exporter);
  50         162  
  50         4791  
4 50     50   480 use strict;
  50         147  
  50         1093  
5 50     50   304 use warnings;
  50         158  
  50         1732  
6              
7 50     50   1680 use Error::Pure qw(err);
  50         34941  
  50         2238  
8 50     50   502 use Readonly;
  50         149  
  50         2227  
9 50     50   19683 use Wikibase::Datatype::Value::String;
  50         27643  
  50         10995  
10              
11             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
12              
13             our $VERSION = 0.11;
14              
15             sub obj2struct {
16 24     24 1 4181 my $obj = shift;
17              
18 24 100       70 if (! defined $obj) {
19 1         3 err "Object doesn't exist.";
20             }
21 23 100       129 if (! $obj->isa('Wikibase::Datatype::Value::String')) {
22 1         6 err "Object isn't 'Wikibase::Datatype::Value::String'.";
23             }
24              
25 22         120 my $struct_hr = {
26             'type' => 'string',
27             'value' => $obj->value,
28             };
29              
30 22         246 return $struct_hr;
31             }
32              
33             sub struct2obj {
34 20     20 1 173 my $struct_hr = shift;
35              
36 20 50 33     133 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 20         184 'value' => $struct_hr->{'value'},
44             );
45              
46 20         1352 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 value 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             =for comment filename=obj2struct_value_string.pl
103              
104             use strict;
105             use warnings;
106              
107             use Data::Printer;
108             use Wikibase::Datatype::Value::String;
109             use Wikibase::Datatype::Struct::Value::String qw(obj2struct);
110              
111             # Object.
112             my $obj = Wikibase::Datatype::Value::String->new(
113             'value' => 'foo',
114             );
115              
116             # Get structure.
117             my $struct_hr = obj2struct($obj);
118              
119             # Dump to output.
120             p $struct_hr;
121              
122             # Output:
123             # \ {
124             # type "string",
125             # value "foo"
126             # }
127              
128             =head1 EXAMPLE2
129              
130             =for comment filename=struct2obj_value_string.pl
131              
132             use strict;
133             use warnings;
134              
135             use Wikibase::Datatype::Struct::Value::String qw(struct2obj);
136              
137             # String structure.
138             my $struct_hr = {
139             'type' => 'string',
140             'value' => 'foo',
141             };
142              
143             # Get object.
144             my $obj = struct2obj($struct_hr);
145              
146             # Get type.
147             my $type = $obj->type;
148              
149             # Get value.
150             my $value = $obj->value;
151              
152             # Print out.
153             print "Type: $type\n";
154             print "Value: $value\n";
155              
156             # Output:
157             # Type: string
158             # Value: foo
159              
160             =head1 DEPENDENCIES
161              
162             L<Error::Pure>,
163             L<Exporter>,
164             L<Readonly>,
165             L<Wikibase::Datatype::Value::String>.
166              
167             =head1 SEE ALSO
168              
169             =over
170              
171             =item L<Wikibase::Datatype::Struct>
172              
173             Wikibase structure serialization.
174              
175             =item L<Wikibase::Datatype::Value::String>
176              
177             Wikibase string value datatype.
178              
179             =back
180              
181             =head1 REPOSITORY
182              
183             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
184              
185             =head1 AUTHOR
186              
187             Michal Josef Špaček L<mailto:skim@cpan.org>
188              
189             L<http://skim.cz>
190              
191             =head1 LICENSE AND COPYRIGHT
192              
193             © 2020-2023 Michal Josef Špaček
194              
195             BSD 2-Clause License
196              
197             =head1 VERSION
198              
199             0.11
200              
201             =cut