File Coverage

blib/lib/Wikibase/Datatype/Struct/Value/Sense.pm
Criterion Covered Total %
statement 30 30 100.0
branch 6 6 100.0
condition 14 18 77.7
subroutine 8 8 100.0
pod 2 2 100.0
total 60 64 93.7


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