File Coverage

blib/lib/Wikibase/Datatype/Snak.pm
Criterion Covered Total %
statement 41 41 100.0
branch 10 10 100.0
condition n/a
subroutine 11 11 100.0
pod 0 1 0.0
total 62 63 98.4


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Snak;
2              
3 150     150   2074402 use strict;
  150         380  
  150         6157  
4 150     150   834 use warnings;
  150         434  
  150         9092  
5              
6 150     150   35688 use Error::Pure qw(err);
  150         1043071  
  150         7840  
7 150     150   6774 use List::Util 1.33 qw(none);
  150         3860  
  150         24820  
8 150     150   36521 use Mo qw(build is);
  150         54324  
  150         1102  
9 150     150   209260 use Mo::utils qw(check_isa check_required);
  150         232811  
  150         7514  
10 150     150   12880 use Readonly;
  150         602  
  150         8841  
11 150     150   78291 use Wikibase::Datatype::Utils qw(check_property);
  150         804  
  150         18986  
12              
13             # Pairs data type and datatype.
14             Readonly::Hash our %DATA_TYPES => (
15             'commonsMedia' => 'Wikibase::Datatype::Value::String',
16             'external-id' => 'Wikibase::Datatype::Value::String',
17             'geo-shape' => 'Wikibase::Datatype::Value::String',
18             'globe-coordinate' => 'Wikibase::Datatype::Value::Globecoordinate',
19             'math' => 'Wikibase::Datatype::Value::String',
20             'monolingualtext' => 'Wikibase::Datatype::Value::Monolingual',
21             'musical-notation' => 'Wikibase::Datatype::Value::String',
22             'quantity' => 'Wikibase::Datatype::Value::Quantity',
23             'string' => 'Wikibase::Datatype::Value::String',
24             'tabular-data' => 'Wikibase::Datatype::Value::String',
25             'time' => 'Wikibase::Datatype::Value::Time',
26             'url' => 'Wikibase::Datatype::Value::String',
27             'wikibase-item' => 'Wikibase::Datatype::Value::Item',
28             'wikibase-lexeme' => 'Wikibase::Datatype::Value::Lexeme',
29             'wikibase-property' => 'Wikibase::Datatype::Value::Property',
30             'wikibase-sense' => 'Wikibase::Datatype::Value::Sense',
31             );
32             Readonly::Array our @SNAK_TYPES => qw(
33             novalue
34             somevalue
35             value
36             );
37              
38             our $VERSION = 0.39;
39              
40             has datatype => (
41             is => 'ro',
42             );
43              
44             has datavalue => (
45             is => 'ro',
46             );
47              
48             has property => (
49             is => 'ro',
50             );
51              
52             has snaktype => (
53             is => 'ro',
54             );
55              
56             sub BUILD {
57 225     225 0 17516 my $self = shift;
58              
59             # Check snak type.
60 225 100       1530 if (defined $self->{'snaktype'}) {
61 5 100   9   40 if (none { $self->{'snaktype'} eq $_ } @SNAK_TYPES) {
  9         92  
62 1         12 err "Parameter 'snaktype' = '$self->{'snaktype'}' isn't supported.";
63             }
64             } else {
65 220         767 $self->{'snaktype'} = 'value';
66             }
67              
68             # Requirements.
69 224 100       943 if ($self->{'snaktype'} eq 'value') {
70 220         866 check_required($self, 'datavalue');
71             }
72 223         2281 check_required($self, 'datatype');
73 222         1918 check_required($self, 'property');
74              
75             # Check data type.
76 221 100   2002   3114 if (none { $self->{'datatype'} eq $_ } keys %DATA_TYPES) {
  2002         18236  
77 1         5 err "Parameter 'datatype' = '$self->{'datatype'}' isn't supported.";
78             }
79              
80             # Check data value.
81 220 100       2006 if ($self->{'snaktype'} eq 'value') {
82 216         1391 check_isa($self, 'datavalue', $DATA_TYPES{$self->{'datatype'}});
83             }
84              
85             # Check property.
86 219         9445 check_property($self, 'property');
87              
88 218         614 return;
89             }
90              
91             1;
92              
93             __END__
94              
95             =pod
96              
97             =encoding utf8
98              
99             =head1 NAME
100              
101             Wikibase::Datatype::Snak - Wikibase snak datatype.
102              
103             =head1 SYNOPSIS
104              
105             use Wikibase::Datatype::Snak;
106              
107             my $obj = Wikibase::Datatype::Snak->new(%params);
108             my $datatype = $obj->datatype;
109             my $datavalue = $obj->datavalue;
110             my $property = $obj->property;
111             my $snaktype = $obj->snaktype;
112              
113             =head1 DESCRIPTION
114              
115             This datatype is snak class for representing relation between property and value.
116              
117             =head1 METHODS
118              
119             =head2 C<new>
120              
121             my $obj = Wikibase::Datatype::Snak->new(%params);
122              
123             Constructor.
124              
125             Returns instance of object.
126              
127             =over 8
128              
129             =item * C<datatype>
130              
131             Type of data.
132             Parameter is required.
133              
134             Possible datatypes are (datavalue instance in parenthesis):
135             - commonsMedia (Wikibase::Datatype::Value::String)
136             - external-id (Wikibase::Datatype::Value::String)
137             - geo-shape (Wikibase::Datatype::Value::String)
138             - globe-coordinate (Wikibase::Datatype::Value::Globecoordinate)
139             - math (Wikibase::Datatype::Value::String)
140             - monolingualtext (Wikibase::Datatype::Value::Monolingual)
141             - musical-notation (Wikibase::Datatype::Value::String)
142             - quantity (Wikibase::Datatype::Value::Quantity)
143             - string (Wikibase::Datatype::Value::String)
144             - tabular-data (Wikibase::Datatype::Value::String)
145             - time (Wikibase::Datatype::Value::Time)
146             - url (Wikibase::Datatype::Value::String)
147             - wikibase-item (Wikibase::Datatype::Value::Item)
148             - wikibase-lexeme (Wikibase::Datatype::Value::Lexeme)
149             - wikibase-property (Wikibase::Datatype::Value::Property)
150             - wikibase-sense (Wikibase::Datatype::Value::Sense)
151              
152             =item * C<datavalue>
153              
154             Value of data in form of Wikibase::Datatype::Value instance for concrete datatype.
155             Parameter is required in situation when snaktype = 'value'.
156              
157             =item * C<property>
158              
159             Property name (like /^P\d+$/).
160             Parameter is required.
161              
162             =item * C<snaktype>
163              
164             Snak type.
165             Parameter is string with these possible values: novalue somevalue value
166             Parameter is optional.
167             Default value is 'value'.
168              
169             =back
170              
171             =head2 C<datatype>
172              
173             my $datatype = $obj->datatype;
174              
175             Get data type.
176              
177             Returns string.
178              
179             =head2 C<datavalue>
180              
181             my $datavalue = $obj->datavalue;
182              
183             Get data value.
184              
185             Returns instance of Wikibase::Datatype::Value.
186              
187             =head2 C<property>
188              
189             my $property = $obj->property;
190              
191             Get property name.
192              
193             Returns string.
194              
195             =head2 C<snaktype>
196              
197             my $snaktype = $obj->snaktype;
198              
199             Get snak type.
200              
201             Returns string.
202              
203             =head1 ERRORS
204              
205             new():
206             From Mo::utils::check_required():
207             Parameter 'datatype' is required.
208             Parameter 'datavalue' is required.
209             Parameter 'property' is required.
210             From Mo::utils::check_isa():
211             Parameter 'datavalue' must be a 'Wikibase::Datatype::Value::%s' object.
212             From Wikibase::Datatype::Utils::check_property():
213             Parameter 'property' must begin with 'P' and number after it.
214             Parameter 'datatype' = '%s' isn't supported.
215             Parameter 'snaktype' = '%s' isn't supported.
216              
217             =head1 EXAMPLE
218              
219             =for comment filename=create_and_print_snak.pl
220              
221             use strict;
222             use warnings;
223              
224             use Wikibase::Datatype::Snak;
225             use Wikibase::Datatype::Value::Item;
226              
227             # Object.
228             my $obj = Wikibase::Datatype::Snak->new(
229             'datatype' => 'wikibase-item',
230             'datavalue' => Wikibase::Datatype::Value::Item->new(
231             'value' => 'Q5',
232             ),
233             'property' => 'P31',
234             );
235              
236             # Get value.
237             my $datavalue = $obj->datavalue->value;
238              
239             # Get datatype.
240             my $datatype = $obj->datatype;
241              
242             # Get property.
243             my $property = $obj->property;
244              
245             # Get snak type.
246             my $snaktype = $obj->snaktype;
247              
248             # Print out.
249             print "Property: $property\n";
250             print "Type: $datatype\n";
251             print "Value: $datavalue\n";
252             print "Snak type: $snaktype\n";
253              
254             # Output:
255             # Property: P31
256             # Type: wikibase-item
257             # Value: Q5
258             # Snak type: value
259              
260             =head1 DEPENDENCIES
261              
262             L<Error::Pure>,
263             L<List::Util>,
264             L<Mo>,
265             L<Mo::utils>,
266             L<Readonly>,
267             L<Wikibase::Datatype::Utils>.
268              
269             =head1 SEE ALSO
270              
271             =over
272              
273             =item L<Wikibase::Datatype>
274              
275             Wikibase datatypes.
276              
277             =item L<Wikibase::Datatype::MediainfoSnak>
278              
279             Wikibase mediainfo snak datatype.
280              
281             =back
282              
283             =head1 REPOSITORY
284              
285             L<https://github.com/michal-josef-spacek/Wikibase-Datatype>
286              
287             =head1 AUTHOR
288              
289             Michal Josef Špaček L<mailto:skim@cpan.org>
290              
291             L<http://skim.cz>
292              
293             =head1 LICENSE AND COPYRIGHT
294              
295             © 2020-2025 Michal Josef Špaček
296              
297             BSD 2-Clause License
298              
299             =head1 VERSION
300              
301             0.39
302              
303             =cut