File Coverage

blib/lib/Wikibase/Datatype/Statement.pm
Criterion Covered Total %
statement 33 33 100.0
branch 2 2 100.0
condition 3 3 100.0
subroutine 10 10 100.0
pod 0 1 0.0
total 48 49 97.9


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Statement;
2              
3 87     87   287404 use strict;
  87         216  
  87         10052  
4 87     87   2404 use warnings;
  87         2351  
  87         8504  
5              
6 87     87   36435 use Error::Pure qw(err);
  87         660571  
  87         4640  
7 87     87   4756 use List::Util 1.33 qw(none);
  87         2327  
  87         8034  
8 87     87   24320 use Mo qw(build default is);
  87         32073  
  87         626  
9 87     87   186073 use Mo::utils qw(check_isa check_required);
  87         159336  
  87         4040  
10 87     87   38680 use Mo::utils::Array qw(check_array_object);
  87         143016  
  87         3461  
11 87     87   4947 use Readonly;
  87         211  
  87         33181  
12              
13             Readonly::Array our @RANKS => qw(normal preferred deprecated);
14              
15             our $VERSION = 0.39;
16              
17             has id => (
18             is => 'ro',
19             );
20              
21             has property_snaks => (
22             default => [],
23             is => 'ro',
24             );
25              
26             has rank => (
27             is => 'ro',
28             default => 'normal',
29             );
30              
31             has references => (
32             default => [],
33             is => 'ro',
34             );
35              
36             has snak => (
37             is => 'ro',
38             );
39              
40             sub BUILD {
41 99     99 0 11187 my $self = shift;
42              
43             # Check requirements.
44 99         776 check_required($self, 'snak');
45              
46             # Check rank.
47 98 100 100 5   1494 if (defined $self->{'rank'} && none { $_ eq $self->{'rank'} } @RANKS) {
  5         46  
48 1         11 err "Parameter 'rank' has bad value. Possible values are ".(
49             join ', ', @RANKS).'.';
50             }
51              
52             # Check snak.
53 97         390 check_isa($self, 'snak', 'Wikibase::Datatype::Snak');
54              
55             # Check property snaks.
56 96         2857 check_array_object($self, 'property_snaks', 'Wikibase::Datatype::Snak');
57              
58             # Check references.
59 94         1148 check_array_object($self, 'references', 'Wikibase::Datatype::Reference');
60              
61 92         1361 return;
62             }
63              
64             1;
65              
66             __END__
67              
68             =pod
69              
70             =encoding utf8
71              
72             =head1 NAME
73              
74             Wikibase::Datatype::Statement - Wikibase statement datatype.
75              
76             =head1 SYNOPSIS
77              
78             use Wikibase::Datatype::Statement;
79              
80             my $obj = Wikibase::Datatype::Statement->new(%params);
81             my $id = $obj->id;
82             my $property_snaks_ar = $obj->property_snaks;
83             my $rank = $obj->rank;
84             my $referenes_ar = $obj->references;
85             my $snak = $obj->snak;
86              
87             =head1 DESCRIPTION
88              
89             This datatype is statement class for representing claim.
90              
91             =head1 METHODS
92              
93             =head2 C<new>
94              
95             my $obj = Wikibase::Datatype::Statement->new(%params);
96              
97             Constructor.
98              
99             Returns instance of object.
100              
101             =over 8
102              
103             =item * C<id>
104              
105             Id of statement.
106             Parameter is optional.
107              
108             =item * C<property_snaks>
109              
110             Property snaks.
111             Parameter is reference to hash with Wikibase::Datatype::Snak instances.
112             Parameter is optional.
113             Default value is [].
114              
115             =item * C<rank>
116              
117             Rank value.
118             Parameter is string with these possible values: normal, preferred and deprecated
119             Default value is 'normal'.
120              
121             =item * C<references>
122              
123             List of references.
124             Parameter is reference to hash with Wikibase::Datatype::Reference instances.
125             Parameter is optional.
126             Default value is [].
127              
128             =item * C<snak>
129              
130             Main snak.
131             Parameter is Wikibase::Datatype::Snak instance.
132             Parameter is required.
133              
134             =back
135              
136             =head2 C<id>
137              
138             my $id = $obj->id;
139              
140             Get id of statement.
141              
142             Returns string.
143              
144             =head2 C<property_snaks>
145              
146             my $property_snaks_ar = $obj->property_snaks;
147              
148             Get property snaks.
149              
150             Returns reference to array with Wikibase::Datatype::Snak instances.
151              
152             =head2 C<rank>
153              
154             my $rank = $obj->rank;
155              
156             Get rank value.
157              
158             =head2 C<references>
159              
160             my $referenes_ar = $obj->references;
161              
162             Get references.
163              
164             Returns reference to array with Wikibase::Datatype::Reference instance.
165              
166             =head2 C<snak>
167              
168             my $snak = $obj->snak;
169              
170             Get main snak.
171              
172             Returns Wikibase::Datatype::Snak instance.
173              
174             =head1 ERRORS
175              
176             new():
177             From Mo::utils::check_isa():
178             Parameter 'snak' must be a 'Wikibase::Datatype::Snak' object.
179              
180             From Mo::utils::check_required():
181             Parameter 'snak' is required.
182              
183             From Mo::utils::Array::check_array_object():
184             Parameter 'property_snaks' must be a array.
185             Value: %s
186             Reference: %s
187             Parameter 'property_snaks' with array must contain 'Wikibase::Datatype::Snak' objects.
188             Value: %s
189             Reference: %s
190             Parameter 'references' must be a array.
191             Value: %s
192             Reference: %s
193             Parameter 'references' with array must contain 'Wikibase::Datatype::Reference' objects.
194             Value: %s
195             Reference: %s
196              
197             Parameter 'rank' has bad value. Possible values are normal, preferred, deprecated.
198              
199             =head1 EXAMPLE
200              
201             =for comment filename=create_and_print_statement.pl
202              
203             use strict;
204             use warnings;
205              
206             use Wikibase::Datatype::Reference;
207             use Wikibase::Datatype::Statement;
208             use Wikibase::Datatype::Snak;
209             use Wikibase::Datatype::Value::Item;
210             use Wikibase::Datatype::Value::String;
211             use Wikibase::Datatype::Value::Time;
212              
213             # Object.
214             my $obj = Wikibase::Datatype::Statement->new(
215             'id' => 'Q123$00C04D2A-49AF-40C2-9930-C551916887E8',
216              
217             # instance of (P31) human (Q5)
218             'snak' => Wikibase::Datatype::Snak->new(
219             'datatype' => 'wikibase-item',
220             'datavalue' => Wikibase::Datatype::Value::Item->new(
221             'value' => 'Q5',
222             ),
223             'property' => 'P31',
224             ),
225             'property_snaks' => [
226             # of (P642) alien (Q474741)
227             Wikibase::Datatype::Snak->new(
228             'datatype' => 'wikibase-item',
229             'datavalue' => Wikibase::Datatype::Value::Item->new(
230             'value' => 'Q474741',
231             ),
232             'property' => 'P642',
233             ),
234             ],
235             'references' => [
236             Wikibase::Datatype::Reference->new(
237             'snaks' => [
238             # stated in (P248) Virtual International Authority File (Q53919)
239             Wikibase::Datatype::Snak->new(
240             'datatype' => 'wikibase-item',
241             'datavalue' => Wikibase::Datatype::Value::Item->new(
242             'value' => 'Q53919',
243             ),
244             'property' => 'P248',
245             ),
246              
247             # VIAF ID (P214) 113230702
248             Wikibase::Datatype::Snak->new(
249             'datatype' => 'external-id',
250             'datavalue' => Wikibase::Datatype::Value::String->new(
251             'value' => '113230702',
252             ),
253             'property' => 'P214',
254             ),
255              
256             # retrieved (P813) 7 December 2013
257             Wikibase::Datatype::Snak->new(
258             'datatype' => 'time',
259             'datavalue' => Wikibase::Datatype::Value::Time->new(
260             'value' => '+2013-12-07T00:00:00Z',
261             ),
262             'property' => 'P813',
263             ),
264             ],
265             ),
266             ],
267             );
268              
269             # Print out.
270             print 'Id: '.$obj->id."\n";
271             print 'Claim: '.$obj->snak->property.' -> '.$obj->snak->datavalue->value."\n";
272             print "Qualifiers:\n";
273             foreach my $property_snak (@{$obj->property_snaks}) {
274             print "\t".$property_snak->property.' -> '.
275             $property_snak->datavalue->value."\n";
276             }
277             print "References:\n";
278             foreach my $reference (@{$obj->references}) {
279             print "\tReference:\n";
280             foreach my $reference_snak (@{$reference->snaks}) {
281             print "\t\t".$reference_snak->property.' -> '.
282             $reference_snak->datavalue->value."\n";
283             }
284             }
285             print 'Rank: '.$obj->rank."\n";
286              
287             # Output:
288             # Id: Q123$00C04D2A-49AF-40C2-9930-C551916887E8
289             # Claim: P31 -> Q5
290             # Qualifiers:
291             # P642 -> Q474741
292             # References:
293             # Reference:
294             # P248 -> Q53919
295             # P214 -> 113230702
296             # P813 -> +2013-12-07T00:00:00Z
297             # Rank: normal
298              
299             =head1 DEPENDENCIES
300              
301             L<Error::Pure>,
302             L<List::Util>,
303             L<Mo>,
304             L<Mo::utils>.
305             L<Mo::utils::Array>.
306             L<Readonly>.
307              
308             =head1 SEE ALSO
309              
310             =over
311              
312             =item L<Wikibase::Datatype>
313              
314             Wikibase datatypes.
315              
316             =back
317              
318             =head1 REPOSITORY
319              
320             L<https://github.com/michal-josef-spacek/Wikibase-Datatype>
321              
322             =head1 AUTHOR
323              
324             Michal Josef Špaček L<mailto:skim@cpan.org>
325              
326             L<http://skim.cz>
327              
328             =head1 LICENSE AND COPYRIGHT
329              
330             © 2020-2025 Michal Josef Špaček
331              
332             BSD 2-Clause License
333              
334             =head1 VERSION
335              
336             0.39
337              
338             =cut