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 3     3   90514 use strict;
  3         18  
  3         86  
4 3     3   14 use warnings;
  3         6  
  3         80  
5              
6 3     3   1367 use Error::Pure qw(err);
  3         24604  
  3         58  
7 3     3   195 use List::Util qw(none);
  3         8  
  3         408  
8 3     3   1366 use Mo qw(build is);
  3         1552  
  3         15  
9 3     3   9223 use Mo::utils qw(check_array);
  3         5504  
  3         50  
10 3     3   289 use Readonly;
  3         5  
  3         894  
11              
12             Readonly::Array our @DATA_TYPES => qw(plain tags);
13              
14             our $VERSION = 0.01;
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 7     7 0 6902 my $self = shift;
35              
36             # Check data type.
37 7 100       25 if (! defined $self->{'data_type'}) {
38 1         3 $self->{'data_type'} = 'plain';
39             }
40 7 100   10   40 if (none { $self->{'data_type'} eq $_ } @DATA_TYPES) {
  10         128  
41 1         5 err "Parameter 'data_type' has bad value.";
42             }
43              
44             # Check data based on type.
45 6         38 check_array($self, 'data');
46 5         42 foreach my $data_item (@{$self->{'data'}}) {
  5         12  
47             # Plain mode
48 4 100       25 if ($self->{'data_type'} eq 'plain') {
49 2 100       7 if (ref $data_item ne '') {
50 1         4 err "Parameter 'data' in 'plain' mode must contain ".
51             'reference to array with scalars.';
52             }
53             # Tags mode.
54             } else {
55 2 100       7 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 3         7 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             Button data content. It's reference to array.
109             Data type of data is described in 'data_type' parameter.
110              
111             Default value is [].
112              
113             =item * C<data_type>
114              
115             Button data type for content.
116              
117             Possible value are: plain tags
118              
119             Default value is 'plain'.
120              
121             =item * C<url>
122              
123             URL of link.
124              
125             Default value is undef.
126              
127             =back
128              
129             =head2 C<css_class>
130              
131             my $css_class = $obj->css_class;
132              
133             Get CSS class for form.
134              
135             Returns string.
136              
137             =head2 C<data>
138              
139             my $data = $obj->data;
140              
141             Get data inside button element.
142              
143             Returns reference to array.
144              
145             =head2 C<data_type>
146              
147             my $data_type = $obj->data_type;
148              
149             Get button data type.
150              
151             Returns string.
152              
153             =head2 C<url>
154              
155             my $url = $obj->url;
156              
157             Get URL of link.
158              
159             Returns string.
160              
161             =head1 ERRORS
162              
163             new():
164             Parameter 'data' must be a array.
165             Value: %s
166             Reference: %s
167             Parameter 'data' in 'plain' mode must contain reference to array with scalars.
168             Parameter 'data' in 'tags' mode must contain reference to array with references to array with Tags structure.
169             Parameter 'data_type' has bad value.
170              
171             =head1 EXAMPLE1
172              
173             =for comment filename=link.pl
174              
175             use strict;
176             use warnings;
177              
178             use Data::HTML::A;
179              
180             my $obj = Data::HTML::A->new(
181             'css_class' => 'link',
182             'data' => 'Michal Josef Spacek homepage',
183             'url' => 'https://skim.cz',
184             );
185              
186             # Print out.
187             print 'CSS class: '.$obj->css_class."\n";
188             print 'Data: '.$obj->data."\n";
189             print 'Data type: '.$obj->data_type."\n";
190             print 'URL: '.$obj->url."\n";
191              
192             # Output:
193             # CSS class: link
194             # Data: Michal Josef Spacek homepage
195             # Data type: plain
196             # URL: https://skim.cz
197              
198             =head1 EXAMPLE2
199              
200             =for comment filename=link_tags.pl
201              
202             use strict;
203             use warnings;
204              
205             use Data::HTML::A;
206             use Tags::Output::Raw;
207              
208             my $obj = Data::HTML::A->new(
209             'css_class' => 'link',
210             # Tags(3pm) structure.
211             'data' => [
212             ['b', 'span'],
213             ['a', 'class', 'span-link'],
214             ['d', 'Link'],
215             ['e', 'span'],
216             ],
217             'data_type' => 'tags',
218             'url' => 'https://skim.cz',
219             );
220              
221             my $tags = Tags::Output::Raw->new;
222              
223             # Serialize data to output.
224             $tags->put(@{$obj->data});
225             my $data = $tags->flush(1);
226              
227             # Print out.
228             print 'CSS class: '.$obj->css_class."\n";
229             print 'Data (serialized): '.$data."\n";
230             print 'Data type: '.$obj->data_type."\n";
231             print 'URL: '.$obj->url."\n";
232              
233             # Output:
234             # CSS class: link
235             # Data (serialized): <span class="span-link">Link</span>
236             # Data type: tags
237             # URL: https://skim.cz
238              
239             =head1 DEPENDENCIES
240              
241             L<Error::Pure>,
242             L<List::Util>,
243             L<Mo>,
244             L<Readonly>.
245              
246             =head1 REPOSITORY
247              
248             L<https://github.com/michal-josef-spacek/Data-HTML-A>
249              
250             =head1 AUTHOR
251              
252             Michal Josef Špaček L<mailto:skim@cpan.org>
253              
254             L<http://skim.cz>
255              
256             =head1 LICENSE AND COPYRIGHT
257              
258             © 2022-2023 Michal Josef Špaček
259              
260             BSD 2-Clause License
261              
262             =head1 VERSION
263              
264             0.01
265              
266             =cut