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 50     50   1050022 use base qw(Exporter);
  50         189  
  50         4737  
4 50     50   370 use strict;
  50         129  
  50         1045  
5 50     50   251 use warnings;
  50         133  
  50         1650  
6              
7 50     50   1755 use Error::Pure qw(err);
  50         24738  
  50         2111  
8 50     50   460 use Readonly;
  50         165  
  50         2610  
9 50     50   22518 use Wikibase::Datatype::Value::Property;
  50         1709567  
  50         15289  
10              
11             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
12              
13             our $VERSION = 0.11;
14              
15             sub obj2struct {
16 4     4 1 4902 my $obj = shift;
17              
18 4 100       19 if (! defined $obj) {
19 1         8 err "Object doesn't exist.";
20             }
21 3 100       26 if (! $obj->isa('Wikibase::Datatype::Value::Property')) {
22 1         6 err "Object isn't 'Wikibase::Datatype::Value::Property'.";
23             }
24              
25 2         18 my $numeric_id = $obj->value;
26 2         30 $numeric_id =~ s/^P//ms;
27 2         12 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         30 return $struct_hr;
37             }
38              
39             sub struct2obj {
40 6     6 1 7245 my $struct_hr = shift;
41              
42 6 100 66     98 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         29 'value' => $struct_hr->{'value'}->{'id'},
55             );
56              
57 2         278 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 value 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             =for comment filename=obj2struct_value_property.pl
114              
115             use strict;
116             use warnings;
117              
118             use Data::Printer;
119             use Wikibase::Datatype::Value::Property;
120             use Wikibase::Datatype::Struct::Value::Property qw(obj2struct);
121              
122             # Object.
123             my $obj = Wikibase::Datatype::Value::Property->new(
124             'value' => 'P123',
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 "property",
138             # id "P123",
139             # numeric-id 123
140             # }
141             # }
142              
143             =head1 EXAMPLE2
144              
145             =for comment filename=struct2obj_value_property.pl
146              
147             use strict;
148             use warnings;
149              
150             use Wikibase::Datatype::Struct::Value::Property qw(struct2obj);
151              
152             # Property structure.
153             my $struct_hr = {
154             'type' => 'wikibase-entityid',
155             'value' => {
156             'entity-type' => 'property',
157             'id' => 'P123',
158             'numeric-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: property
177             # Value: P123
178              
179             =head1 DEPENDENCIES
180              
181             L<Error::Pure>,
182             L<Exporter>,
183             L<Readonly>,
184             L<Wikibase::Datatype::Value::Property>.
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::Property>
195              
196             Wikibase property 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