File Coverage

blib/lib/Data/HTML/A.pm
Criterion Covered Total %
statement 36 36 100.0
branch 10 10 100.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 55 56 98.2


line stmt bran cond sub pod time code
1             package Data::HTML::A;
2              
3 7     7   95748 use strict;
  7         47  
  7         214  
4 7     7   35 use warnings;
  7         16  
  7         244  
5              
6 7     7   3288 use Error::Pure qw(err);
  7         72484  
  7         151  
7 7     7   496 use List::Util qw(none);
  7         15  
  7         1039  
8 7     7   3246 use Mo qw(build is);
  7         3649  
  7         38  
9 7     7   23742 use Mo::utils qw(check_array);
  7         13454  
  7         115  
10 7     7   762 use Readonly;
  7         14  
  7         2055  
11              
12             Readonly::Array our @DATA_TYPES => qw(plain tags);
13              
14             our $VERSION = 0.02;
15              
16             has css_class => (
17             is => 'ro',
18             );
19              
20             has data => (
21             default => [],
22             ro => 1,
23             );
24              
25             has data_type => (
26             ro => 1,
27             );
28              
29             has url => (
30             is => 'ro',
31             );
32              
33             sub BUILD {
34 16     16 0 11104 my $self = shift;
35              
36             # Check data type.
37 16 100       77 if (! defined $self->{'data_type'}) {
38 7         27 $self->{'data_type'} = 'plain';
39             }
40 16 100   21   109 if (none { $self->{'data_type'} eq $_ } @DATA_TYPES) {
  21         332  
41 1         4 err "Parameter 'data_type' has bad value.";
42             }
43              
44             # Check data based on type.
45 15         90 check_array($self, 'data');
46 14         116 foreach my $data_item (@{$self->{'data'}}) {
  14         45  
47             # Plain mode
48 6 100       42 if ($self->{'data_type'} eq 'plain') {
49 3 100       12 if (ref $data_item ne '') {
50 1         3 err "Parameter 'data' in 'plain' mode must contain ".
51             'reference to array with scalars.';
52             }
53             # Tags mode.
54             } else {
55 3 100       11 if (ref $data_item ne 'ARRAY') {
56 1         4 err "Parameter 'data' in 'tags' mode must contain ".
57             "reference to array with references ".
58             'to array with Tags structure.';
59             }
60             }
61             }
62              
63 12         31 return;
64             }
65              
66             1;
67              
68             __END__
69              
70             =pod
71              
72             =encoding utf8
73              
74             =head1 NAME
75              
76             Data::HTML::A - Data object for HTML a element.
77              
78             =head1 SYNOPSIS
79              
80             use Data::HTML::A;
81              
82             my $obj = Data::HTML::A->new(%params);
83             my $css_class = $obj->css_class;
84             my $data = $obj->data;
85             my $data_type = $obj->data_type;
86             my $url = $obj->url;
87              
88             =head1 METHODS
89              
90             =head2 C<new>
91              
92             my $obj = Data::HTML::A->new(%params);
93              
94             Constructor.
95              
96             Returns instance of object.
97              
98             =over 8
99              
100             =item * C<css_class>
101              
102             Form CSS class.
103              
104             Default value is undef.
105              
106             =item * C<data>
107              
108             Data content. It's reference to array.
109              
110             Data type of data is described in 'data_type' parameter.
111              
112             Default value is [].
113              
114             =item * C<data_type>
115              
116             Data type for content.
117              
118             Possible value are: plain tags
119              
120             The 'plain' content are string(s).
121             The 'tags' content is structure described in L<Tags>.
122              
123             Default value is 'plain'.
124              
125             =item * C<url>
126              
127             URL of link.
128              
129             Default value is undef.
130              
131             =back
132              
133             =head2 C<css_class>
134              
135             my $css_class = $obj->css_class;
136              
137             Get CSS class for form.
138              
139             Returns string.
140              
141             =head2 C<data>
142              
143             my $data = $obj->data;
144              
145             Get data inside button element.
146              
147             Returns reference to array.
148              
149             =head2 C<data_type>
150              
151             my $data_type = $obj->data_type;
152              
153             Get button data type.
154              
155             Returns string.
156              
157             =head2 C<url>
158              
159             my $url = $obj->url;
160              
161             Get URL of link.
162              
163             Returns string.
164              
165             =head1 ERRORS
166              
167             new():
168             Parameter 'data' must be a array.
169             Value: %s
170             Reference: %s
171             Parameter 'data' in 'plain' mode must contain reference to array with scalars.
172             Parameter 'data' in 'tags' mode must contain reference to array with references to array with Tags structure.
173             Parameter 'data_type' has bad value.
174              
175             =head1 EXAMPLE1
176              
177             =for comment filename=link.pl
178              
179             use strict;
180             use warnings;
181              
182             use Data::HTML::A;
183              
184             my $obj = Data::HTML::A->new(
185             'css_class' => 'link',
186             'data' => ['Michal Josef Spacek homepage'],
187             'url' => 'https://skim.cz',
188             );
189              
190             # Print out.
191             print 'CSS class: '.$obj->css_class."\n";
192             print 'Data: '.(join '', @{$obj->data})."\n";
193             print 'Data type: '.$obj->data_type."\n";
194             print 'URL: '.$obj->url."\n";
195              
196             # Output:
197             # CSS class: link
198             # Data: Michal Josef Spacek homepage
199             # Data type: plain
200             # URL: https://skim.cz
201              
202             =head1 EXAMPLE2
203              
204             =for comment filename=link_tags.pl
205              
206             use strict;
207             use warnings;
208              
209             use Data::HTML::A;
210             use Tags::Output::Raw;
211              
212             my $obj = Data::HTML::A->new(
213             'css_class' => 'link',
214             # Tags(3pm) structure.
215             'data' => [
216             ['b', 'span'],
217             ['a', 'class', 'span-link'],
218             ['d', 'Link'],
219             ['e', 'span'],
220             ],
221             'data_type' => 'tags',
222             'url' => 'https://skim.cz',
223             );
224              
225             my $tags = Tags::Output::Raw->new;
226              
227             # Serialize data to output.
228             $tags->put(@{$obj->data});
229             my $data = $tags->flush(1);
230              
231             # Print out.
232             print 'CSS class: '.$obj->css_class."\n";
233             print 'Data (serialized): '.$data."\n";
234             print 'Data type: '.$obj->data_type."\n";
235             print 'URL: '.$obj->url."\n";
236              
237             # Output:
238             # CSS class: link
239             # Data (serialized): <span class="span-link">Link</span>
240             # Data type: tags
241             # URL: https://skim.cz
242              
243             =head1 DEPENDENCIES
244              
245             L<Error::Pure>,
246             L<List::Util>,
247             L<Mo>,
248             L<Readonly>.
249              
250             =head1 REPOSITORY
251              
252             L<https://github.com/michal-josef-spacek/Data-HTML-A>
253              
254             =head1 AUTHOR
255              
256             Michal Josef Špaček L<mailto:skim@cpan.org>
257              
258             L<http://skim.cz>
259              
260             =head1 LICENSE AND COPYRIGHT
261              
262             © 2022-2023 Michal Josef Špaček
263              
264             BSD 2-Clause License
265              
266             =head1 VERSION
267              
268             0.02
269              
270             =cut