File Coverage

blib/lib/Wikibase/Datatype/Print/Form.pm
Criterion Covered Total %
statement 47 47 100.0
branch 10 12 83.3
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 68 70 97.1


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Print::Form;
2              
3 7     7   355043 use base qw(Exporter);
  7         32  
  7         648  
4 7     7   55 use strict;
  7         16  
  7         129  
5 7     7   32 use warnings;
  7         16  
  7         233  
6              
7 7     7   1387 use Error::Pure qw(err);
  7         24706  
  7         235  
8 7     7   208 use Readonly;
  7         13  
  7         277  
9 7     7   3495 use Wikibase::Datatype::Print::Statement;
  7         26  
  7         349  
10 7     7   52 use Wikibase::Datatype::Print::Utils qw(print_statements);
  7         20  
  7         324  
11 7     7   50 use Wikibase::Datatype::Print::Value::Item;
  7         17  
  7         219  
12 7     7   54 use Wikibase::Datatype::Print::Value::Monolingual;
  7         16  
  7         2218  
13              
14             Readonly::Array our @EXPORT_OK => qw(print);
15              
16             our $VERSION = 0.09;
17              
18             sub print {
19 5     5 1 6341 my ($obj, $opts_hr) = @_;
20              
21 5 100       20 if (! defined $opts_hr) {
22 2         5 $opts_hr = {};
23             }
24              
25 5 100       19 if (! exists $opts_hr->{'lang'}) {
26 4         12 $opts_hr->{'lang'} = 'en';
27             }
28              
29 5 100       29 if (! $obj->isa('Wikibase::Datatype::Form')) {
30 1         4 err "Object isn't 'Wikibase::Datatype::Form'.";
31             }
32              
33 4         23 my @ret = (
34             'Id: '.$obj->id,
35             );
36              
37             # Representation.
38             # XXX In every time one?
39 4         45 my ($representation) = @{$obj->representations};
  4         18  
40 4 50       59 if (defined $representation) {
41 4         23 push @ret, 'Representation: '.
42             Wikibase::Datatype::Print::Value::Monolingual::print($representation, $opts_hr);
43             }
44              
45             # Grammatical features
46 4         65 my @gr_features;
47 4         15 foreach my $gr_feature (@{$obj->grammatical_features}) {
  4         20  
48 8         69 push @gr_features,
49             Wikibase::Datatype::Print::Value::Item::print($gr_feature, $opts_hr);
50             }
51 4 50       17 if (@gr_features) {
52 4         19 push @ret, 'Grammatical features: '.(join ', ', @gr_features);
53             }
54              
55             # Statements.
56 4         32 push @ret, print_statements($obj, $opts_hr,
57             \&Wikibase::Datatype::Print::Statement::print);
58              
59 4 100       25 return wantarray ? @ret : (join "\n", @ret);
60             }
61              
62             1;
63              
64             __END__
65              
66             =pod
67              
68             =encoding utf8
69              
70             =head1 NAME
71              
72             Wikibase::Datatype::Print::Form - Wikibase form pretty print helpers.
73              
74             =head1 SYNOPSIS
75              
76             use Wikibase::Datatype::Print::Form qw(print);
77              
78             my $pretty_print_string = print($obj, $opts_hr);
79              
80             =head1 SUBROUTINES
81              
82             =head2 C<print>
83              
84             my $pretty_print_string = print($obj, $opts_hr);
85              
86             Construct pretty print output for L<Wikibase::Datatype::Form>
87             object.
88              
89             Returns string.
90              
91             =head1 ERRORS
92              
93             print():
94             Object isn't 'Wikibase::Datatype::Form'.
95              
96             =head1 EXAMPLE
97              
98             =for comment filename=create_and_print_form.pl
99              
100             use strict;
101             use warnings;
102              
103             use Unicode::UTF8 qw(decode_utf8 encode_utf8);
104             use Wikibase::Datatype::Form;
105             use Wikibase::Datatype::Print::Form;
106             use Wikibase::Datatype::Snak;
107             use Wikibase::Datatype::Statement;
108             use Wikibase::Datatype::Value::Item;
109             use Wikibase::Datatype::Value::String;
110             use Wikibase::Datatype::Value::Monolingual;
111              
112             # Object.
113             my $obj = Wikibase::Datatype::Form->new(
114             'grammatical_features' => [
115             # singular
116             Wikibase::Datatype::Value::Item->new(
117             'value' => 'Q110786',
118             ),
119             # nominative case
120             Wikibase::Datatype::Value::Item->new(
121             'value' => 'Q131105',
122             ),
123             ],
124             'id' => 'L469-F1',
125             'representations' => [
126             Wikibase::Datatype::Value::Monolingual->new(
127             'language' => 'cs',
128             'value' => 'pes',
129             ),
130             ],
131             'statements' => [
132             Wikibase::Datatype::Statement->new(
133             'snak' => Wikibase::Datatype::Snak->new(
134             'datatype' => 'string',
135             'datavalue' => Wikibase::Datatype::Value::String->new(
136             'value' => decode_utf8('pɛs'),
137             ),
138             'property' => 'P898',
139             ),
140             ),
141             ],
142             );
143              
144             # Print.
145             print encode_utf8(scalar Wikibase::Datatype::Print::Form::print($obj))."\n";
146              
147             # Output:
148             # Id: L469-F1
149             # Representation: pes (cs)
150             # Grammatical features: Q110786, Q131105
151             # Statements:
152             # P898: pɛs (normal)
153              
154             =head1 DEPENDENCIES
155              
156             L<Error::Pure>,
157             L<Exporter>,
158             L<Readonly>,
159             L<Wikibase::Datatype::Print::Statement>,
160             L<Wikibase::Datatype::Print::Utils>,
161             L<Wikibase::Datatype::Print::Value::Item>,
162             L<Wikibase::Datatype::Print::Value::Monolingual>.
163              
164             =head1 SEE ALSO
165              
166             =over
167              
168             =item L<Wikibase::Datatype::Form>
169              
170             Wikibase form datatype.
171              
172             =back
173              
174             =head1 REPOSITORY
175              
176             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Print>
177              
178             =head1 AUTHOR
179              
180             Michal Josef Špaček L<mailto:skim@cpan.org>
181              
182             L<http://skim.cz>
183              
184             =head1 LICENSE AND COPYRIGHT
185              
186             © 2020-2023 Michal Josef Špaček
187              
188             BSD 2-Clause License
189              
190             =head1 VERSION
191              
192             0.09
193              
194             =cut
195