File Coverage

blib/lib/Wikibase/Datatype/Print/Sitelink.pm
Criterion Covered Total %
statement 33 33 100.0
branch 6 8 75.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 47 49 95.9


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Print::Sitelink;
2              
3 7     7   904281 use base qw(Exporter);
  7         47  
  7         703  
4 7     7   48 use strict;
  7         15  
  7         149  
5 7     7   33 use warnings;
  7         16  
  7         216  
6              
7 7     7   913 use Error::Pure qw(err);
  7         22566  
  7         268  
8 7     7   145 use Readonly;
  7         15  
  7         285  
9 7     7   3007 use Wikibase::Datatype::Print::Value::Item;
  7         18  
  7         1588  
10              
11             Readonly::Array our @EXPORT_OK => qw(print);
12              
13             our $VERSION = 0.13;
14              
15             sub print {
16 4     4 1 1347 my ($obj, $opts_hr) = @_;
17              
18 4 100       39 if (! $obj->isa('Wikibase::Datatype::Sitelink')) {
19 1         5 err "Object isn't 'Wikibase::Datatype::Sitelink'.";
20             }
21              
22 3         12 my $ret = '';
23 3 50       14 if (defined $obj->title) {
24 3         49 $ret .= $obj->title;
25             }
26 3 50       28 if (defined $obj->site) {
27 3         33 $ret .= ' ('.$obj->site.')';
28             }
29 3 100       22 if (@{$obj->badges}) {
  3         11  
30 1         16 my @print = map { Wikibase::Datatype::Print::Value::Item::print($_, $opts_hr) } @{$obj->badges};
  2         17  
  1         3  
31 1         6 $ret .= ' ['.(join ' ', @print).']';
32             }
33              
34 3         30 return $ret;
35             }
36              
37             1;
38              
39             __END__
40              
41             =pod
42              
43             =encoding utf8
44              
45             =head1 NAME
46              
47             Wikibase::Datatype::Print::Sitelink - Wikibase sitelink pretty print helpers.
48              
49             =head1 SYNOPSIS
50              
51             use Wikibase::Datatype::Print::Sitelink qw(print);
52              
53             my $pretty_print_string = print($obj, $opts_hr);
54             my @pretty_print_lines = print($obj, $opts_hr);
55              
56             =head1 SUBROUTINES
57              
58             =head2 C<print>
59              
60             my $pretty_print_string = print($obj, $opts_hr);
61             my @pretty_print_lines = print($obj, $opts_hr);
62              
63             Construct pretty print output for L<Wikibase::Datatype::Sitelink>
64             object.
65              
66             Returns string in scalar context.
67             Returns list of lines in array context.
68              
69             =head1 ERRORS
70              
71             print():
72             Object isn't 'Wikibase::Datatype::Sitelink'.
73              
74             =head1 EXAMPLE
75              
76             =for comment filename=create_and_print_sitelink.pl
77              
78             use strict;
79             use warnings;
80              
81             use Unicode::UTF8 qw(decode_utf8 encode_utf8);
82             use Wikibase::Datatype::Print::Sitelink;
83             use Wikibase::Datatype::Sitelink;
84              
85             # Object.
86             my $obj = Wikibase::Datatype::Sitelink->new(
87             'badges' => [
88             Wikibase::Datatype::Value::Item->new(
89             'value' => 'Q123',
90             ),
91             ],
92             'site' => 'cswiki',
93             'title' => decode_utf8('Hlavní strana'),
94             );
95              
96             # Print.
97             print encode_utf8(Wikibase::Datatype::Print::Sitelink::print($obj))."\n";
98              
99             # Output:
100             # Hlavní strana (cswiki) [Q123]
101              
102             =head1 DEPENDENCIES
103              
104             L<Error::Pure>,
105             L<Exporter>,
106             L<Readonly>,
107             L<Wikibase::Datatype::Print::Value::Item>.
108              
109             =head1 SEE ALSO
110              
111             =over
112              
113             =item L<Wikibase::Datatype::Sitelink>
114              
115             Wikibase sitelink datatype.
116              
117             =back
118              
119             =head1 REPOSITORY
120              
121             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Print>
122              
123             =head1 AUTHOR
124              
125             Michal Josef Špaček L<mailto:skim@cpan.org>
126              
127             L<http://skim.cz>
128              
129             =head1 LICENSE AND COPYRIGHT
130              
131             © 2020-2023 Michal Josef Špaček
132              
133             BSD 2-Clause License
134              
135             =head1 VERSION
136              
137             0.13
138              
139             =cut