File Coverage

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


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Value::Property;
2              
3 46     46   359992 use base qw(Exporter);
  46         145  
  46         4702  
4 46     46   304 use strict;
  46         101  
  46         946  
5 46     46   234 use warnings;
  46         97  
  46         1531  
6              
7 46     46   1839 use Error::Pure qw(err);
  46         25551  
  46         1877  
8 46     46   477 use Readonly;
  46         274  
  46         2192  
9 46     46   22562 use Wikibase::Datatype::Value::Property;
  46         41599  
  46         14734  
10              
11             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
12              
13             our $VERSION = 0.08;
14              
15             sub obj2struct {
16 4     4 1 19638 my $obj = shift;
17              
18 4 100       15 if (! defined $obj) {
19 1         5 err "Object doesn't exist.";
20             }
21 3 100       21 if (! $obj->isa('Wikibase::Datatype::Value::Property')) {
22 1         5 err "Object isn't 'Wikibase::Datatype::Value::Property'.";
23             }
24              
25 2         10 my $numeric_id = $obj->value;
26 2         25 $numeric_id =~ s/^P//ms;
27 2         21 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 2         31 return $struct_hr;
37             }
38              
39             sub struct2obj {
40 6     6 1 22774 my $struct_hr = shift;
41              
42 6 100 66     90 if (! exists $struct_hr->{'type'}
      100        
      100        
      66        
      66        
      66        
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 'property') {
49              
50 4         15 err "Structure isn't for 'property' datatype.";
51             }
52              
53             my $obj = Wikibase::Datatype::Value::Property->new(
54 2         22 'value' => $struct_hr->{'value'}->{'id'},
55             );
56              
57 2         239 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::Property - Wikibase property structure serialization.
71              
72             =head1 SYNOPSIS
73              
74             use Wikibase::Datatype::Struct::Value::Property 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::Property 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 property to object.
99              
100             Returns Wikibase::Datatype::Value::Property instance.
101              
102             =head1 ERRORS
103              
104             obj2struct():
105             Object doesn't exist.
106             Object isn't 'Wikibase::Datatype::Value::Property'.
107              
108             struct2obj():
109             Structure isn't for 'property' datatype.
110              
111             =head1 EXAMPLE1
112              
113             use strict;
114             use warnings;
115              
116             use Data::Printer;
117             use Wikibase::Datatype::Value::Property;
118             use Wikibase::Datatype::Struct::Value::Property qw(obj2struct);
119              
120             # Object.
121             my $obj = Wikibase::Datatype::Value::Property->new(
122             'value' => 'P123',
123             );
124              
125             # Get structure.
126             my $struct_hr = obj2struct($obj);
127              
128             # Dump to output.
129             p $struct_hr;
130              
131             # Output:
132             # \ {
133             # type "wikibase-entityid",
134             # value {
135             # entity-type "property",
136             # id "P123",
137             # numeric-id 123
138             # }
139             # }
140              
141             =head1 EXAMPLE2
142              
143             use strict;
144             use warnings;
145              
146             use Wikibase::Datatype::Struct::Value::Property qw(struct2obj);
147              
148             # Property structure.
149             my $struct_hr = {
150             'type' => 'wikibase-entityid',
151             'value' => {
152             'entity-type' => 'property',
153             'id' => 'P123',
154             'numeric-id' => 123,
155             },
156             };
157              
158             # Get object.
159             my $obj = struct2obj($struct_hr);
160              
161             # Get value.
162             my $value = $obj->value;
163              
164             # Get type.
165             my $type = $obj->type;
166              
167             # Print out.
168             print "Type: $type\n";
169             print "Value: $value\n";
170              
171             # Output:
172             # Type: property
173             # Value: P123
174              
175             =head1 DEPENDENCIES
176              
177             L<Error::Pure>,
178             L<Exporter>,
179             L<Readonly>,
180             L<Wikibase::Datatype::Value::Property>.
181              
182             =head1 SEE ALSO
183              
184             =over
185              
186             =item L<Wikibase::Datatype::Struct>
187              
188             Wikibase structure serialization.
189              
190             =item L<Wikibase::Datatype::Value::Property>
191              
192             Wikibase property value datatype.
193              
194             =back
195              
196             =head1 REPOSITORY
197              
198             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
199              
200             =head1 AUTHOR
201              
202             Michal Josef Špaček L<mailto:skim@cpan.org>
203              
204             L<http://skim.cz>
205              
206             =head1 LICENSE AND COPYRIGHT
207              
208             © Michal Josef Špaček 2020-2021
209              
210             BSD 2-Clause License
211              
212             =head1 VERSION
213              
214             0.08
215              
216             =cut