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