File Coverage

blib/lib/Wikibase/Datatype/Print/Statement.pm
Criterion Covered Total %
statement 33 33 100.0
branch 3 4 75.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 46 47 97.8


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Print::Statement;
2              
3 22     22   1047661 use base qw(Exporter);
  22         97  
  22         2111  
4 22     22   155 use strict;
  22         74  
  22         425  
5 22     22   107 use warnings;
  22         82  
  22         721  
6              
7 22     22   1551 use Error::Pure qw(err);
  22         24525  
  22         890  
8 22     22   296 use Readonly;
  22         55  
  22         965  
9 22     22   9947 use Wikibase::Datatype::Print::Reference;
  22         77  
  22         1075  
10 22     22   187 use Wikibase::Datatype::Print::Snak;
  22         65  
  22         748  
11 22     22   10095 use Wikibase::Datatype::Print::Utils qw(print_references);
  22         67  
  22         1106  
12              
13             Readonly::Array our @EXPORT_OK => qw(print);
14              
15             our $VERSION = 0.09;
16              
17             sub print {
18 18     18 1 9895 my ($obj, $opts_hr) = @_;
19              
20 18 100       97 if (! $obj->isa('Wikibase::Datatype::Statement')) {
21 1         20 err "Object isn't 'Wikibase::Datatype::Statement'.";
22             }
23              
24 17         108 my @ret = (
25             Wikibase::Datatype::Print::Snak::print($obj->snak, $opts_hr).' ('.$obj->rank.')',
26             );
27 17         226 foreach my $property_snak (@{$obj->property_snaks}) {
  17         87  
28 3         32 push @ret, ' '.Wikibase::Datatype::Print::Snak::print($property_snak, $opts_hr);
29             }
30              
31             # References.
32 17         216 push @ret, print_references($obj, $opts_hr,
33             \&Wikibase::Datatype::Print::Reference::print);
34              
35 17 50       90 return wantarray ? @ret : (join "\n", @ret);
36             }
37              
38             1;
39              
40             __END__
41              
42             =pod
43              
44             =encoding utf8
45              
46             =head1 NAME
47              
48             Wikibase::Datatype::Print::Statement - Wikibase statement pretty print helpers.
49              
50             =head1 SYNOPSIS
51              
52             use Wikibase::Datatype::Print::Statement qw(print);
53              
54             my $pretty_print_string = print($obj, $opts_hr);
55              
56             =head1 SUBROUTINES
57              
58             =head2 C<print>
59              
60             my $pretty_print_string = print($obj, $opts_hr);
61              
62             Construct pretty print output for L<Wikibase::Datatype::Statement>
63             object.
64              
65             Returns string.
66              
67             =head1 ERRORS
68              
69             print():
70             Object isn't 'Wikibase::Datatype::Statement'.
71              
72             =head1 EXAMPLE1
73              
74             =for comment filename=create_and_print_statement.pl
75              
76             use strict;
77             use warnings;
78              
79             use Wikibase::Datatype::Print::Statement;
80             use Wikibase::Datatype::Reference;
81             use Wikibase::Datatype::Statement;
82             use Wikibase::Datatype::Snak;
83             use Wikibase::Datatype::Value::Item;
84             use Wikibase::Datatype::Value::String;
85             use Wikibase::Datatype::Value::Time;
86              
87             # Object.
88             my $obj = Wikibase::Datatype::Statement->new(
89             'id' => 'Q123$00C04D2A-49AF-40C2-9930-C551916887E8',
90              
91             # instance of (P31) human (Q5)
92             'snak' => Wikibase::Datatype::Snak->new(
93             'datatype' => 'wikibase-item',
94             'datavalue' => Wikibase::Datatype::Value::Item->new(
95             'value' => 'Q5',
96             ),
97             'property' => 'P31',
98             ),
99             'property_snaks' => [
100             # of (P642) alien (Q474741)
101             Wikibase::Datatype::Snak->new(
102             'datatype' => 'wikibase-item',
103             'datavalue' => Wikibase::Datatype::Value::Item->new(
104             'value' => 'Q474741',
105             ),
106             'property' => 'P642',
107             ),
108             ],
109             'references' => [
110             Wikibase::Datatype::Reference->new(
111             'snaks' => [
112             # stated in (P248) Virtual International Authority File (Q53919)
113             Wikibase::Datatype::Snak->new(
114             'datatype' => 'wikibase-item',
115             'datavalue' => Wikibase::Datatype::Value::Item->new(
116             'value' => 'Q53919',
117             ),
118             'property' => 'P248',
119             ),
120              
121             # VIAF ID (P214) 113230702
122             Wikibase::Datatype::Snak->new(
123             'datatype' => 'external-id',
124             'datavalue' => Wikibase::Datatype::Value::String->new(
125             'value' => '113230702',
126             ),
127             'property' => 'P214',
128             ),
129              
130             # retrieved (P813) 7 December 2013
131             Wikibase::Datatype::Snak->new(
132             'datatype' => 'time',
133             'datavalue' => Wikibase::Datatype::Value::Time->new(
134             'value' => '+2013-12-07T00:00:00Z',
135             ),
136             'property' => 'P813',
137             ),
138             ],
139             ),
140             ],
141             );
142              
143             # Print.
144             print Wikibase::Datatype::Print::Statement::print($obj)."\n";
145              
146             # Output:
147             # P31: Q5 (normal)
148             # P642: Q474741
149             # References:
150             # {
151             # P248: Q53919
152             # P214: 113230702
153             # P813: 7 December 2013 (Q1985727)
154             # }
155              
156             =head1 EXAMPLE2
157              
158             =for comment filename=create_and_print_statement_translated.pl
159              
160             use strict;
161             use warnings;
162              
163             use Wikibase::Cache;
164             use Wikibase::Cache::Backend::Basic;
165             use Wikibase::Datatype::Print::Statement;
166             use Wikibase::Datatype::Reference;
167             use Wikibase::Datatype::Statement;
168             use Wikibase::Datatype::Snak;
169             use Wikibase::Datatype::Value::Item;
170             use Wikibase::Datatype::Value::String;
171             use Wikibase::Datatype::Value::Time;
172              
173             # Object.
174             my $obj = Wikibase::Datatype::Statement->new(
175             'id' => 'Q123$00C04D2A-49AF-40C2-9930-C551916887E8',
176              
177             # instance of (P31) human (Q5)
178             'snak' => Wikibase::Datatype::Snak->new(
179             'datatype' => 'wikibase-item',
180             'datavalue' => Wikibase::Datatype::Value::Item->new(
181             'value' => 'Q5',
182             ),
183             'property' => 'P31',
184             ),
185             'property_snaks' => [
186             # of (P642) alien (Q474741)
187             Wikibase::Datatype::Snak->new(
188             'datatype' => 'wikibase-item',
189             'datavalue' => Wikibase::Datatype::Value::Item->new(
190             'value' => 'Q474741',
191             ),
192             'property' => 'P642',
193             ),
194             ],
195             'references' => [
196             Wikibase::Datatype::Reference->new(
197             'snaks' => [
198             # stated in (P248) Virtual International Authority File (Q53919)
199             Wikibase::Datatype::Snak->new(
200             'datatype' => 'wikibase-item',
201             'datavalue' => Wikibase::Datatype::Value::Item->new(
202             'value' => 'Q53919',
203             ),
204             'property' => 'P248',
205             ),
206              
207             # VIAF ID (P214) 113230702
208             Wikibase::Datatype::Snak->new(
209             'datatype' => 'external-id',
210             'datavalue' => Wikibase::Datatype::Value::String->new(
211             'value' => '113230702',
212             ),
213             'property' => 'P214',
214             ),
215              
216             # retrieved (P813) 7 December 2013
217             Wikibase::Datatype::Snak->new(
218             'datatype' => 'time',
219             'datavalue' => Wikibase::Datatype::Value::Time->new(
220             'value' => '+2013-12-07T00:00:00Z',
221             ),
222             'property' => 'P813',
223             ),
224             ],
225             ),
226             ],
227             );
228              
229             # Cache.
230             my $cache = Wikibase::Cache->new(
231             'backend' => 'Basic',
232             );
233              
234             # Print.
235             print Wikibase::Datatype::Print::Statement::print($obj, {
236             'cache' => $cache,
237             })."\n";
238              
239             # Output:
240             # P31 (instance of): Q5 (normal)
241             # P642: Q474741
242             # References:
243             # {
244             # P248 (stated in): Q53919
245             # P214 (VIAF ID): 113230702
246             # P813 (retrieved): 7 December 2013 (Q1985727)
247             # }
248              
249             =head1 DEPENDENCIES
250              
251             L<Error::Pure>,
252             L<Exporter>,
253             L<Readonly>,
254             L<Wikibase::Datatype::Print::Reference>,
255             L<Wikibase::Datatype::Print::Snak>,
256             L<Wikibase::Datatype::Print::Utils>.
257              
258             =head1 SEE ALSO
259              
260             =over
261              
262             =item L<Wikibase::Datatype::Statement>
263              
264             Wikibase statement datatype.
265              
266             =back
267              
268             =head1 REPOSITORY
269              
270             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Print>
271              
272             =head1 AUTHOR
273              
274             Michal Josef Špaček L<mailto:skim@cpan.org>
275              
276             L<http://skim.cz>
277              
278             =head1 LICENSE AND COPYRIGHT
279              
280             © 2020-2023 Michal Josef Špaček
281              
282             BSD 2-Clause License
283              
284             =head1 VERSION
285              
286             0.09
287              
288             =cut
289