File Coverage

blib/lib/Wikibase/Datatype/Struct/Value/Item.pm
Criterion Covered Total %
statement 32 32 100.0
branch 6 6 100.0
condition 18 18 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 66 66 100.0


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