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