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