File Coverage

blib/lib/Data/HTML/Button.pm
Criterion Covered Total %
statement 54 56 96.4
branch 24 26 92.3
condition n/a
subroutine 12 12 100.0
pod 0 1 0.0
total 90 95 94.7


line stmt bran cond sub pod time code
1             package Data::HTML::Button;
2              
3 16     16   100420 use strict;
  16         130  
  16         464  
4 16     16   77 use warnings;
  16         43  
  16         553  
5              
6 16     16   7110 use Error::Pure qw(err);
  16         177001  
  16         374  
7 16     16   1234 use List::Util qw(none);
  16         42  
  16         2465  
8 16     16   11909 use Mo qw(build default is);
  16         11079  
  16         4892  
9 16     16   55797 use Mo::utils qw(check_array check_bool check_required);
  16         28593  
  16         266  
10 16     16   1645 use Readonly;
  16         32  
  16         10840  
11              
12             Readonly::Array our @DATA_TYPES => qw(plain tags);
13             Readonly::Array our @ENCTYPES => (
14             'application/x-www-form-urlencoded',
15             'multipart/form-data',
16             'text/plain',
17             );
18             Readonly::Array our @FORM_METHODS => qw(get post);
19             Readonly::Array our @TYPES => qw(button reset submit);
20              
21             our $VERSION = 0.05;
22              
23             has autofocus => (
24             ro => 1,
25             );
26              
27             has css_class => (
28             ro => 1,
29             );
30              
31             has data => (
32             default => [],
33             ro => 1,
34             );
35              
36             has data_type => (
37             ro => 1,
38             );
39              
40             has disabled => (
41             ro => 1,
42             );
43              
44             has form => (
45             ro => 1,
46             );
47              
48             has form_enctype => (
49             ro => 1,
50             );
51              
52             has form_method => (
53             ro => 1,
54             );
55              
56             has id => (
57             ro => 1,
58             );
59              
60             has label => (
61             ro => 1,
62             );
63              
64             has name => (
65             ro => 1,
66             );
67              
68             has type => (
69             ro => 1,
70             );
71              
72             has value => (
73             ro => 1,
74             );
75              
76             sub BUILD {
77 37     37 0 24365 my $self = shift;
78              
79             # Check autofocus.
80 37 100       188 if (! defined $self->{'autofocus'}) {
81 33         89 $self->{'autofocus'} = 0;
82             }
83 37         139 check_bool($self, 'autofocus');
84              
85             # Check data type.
86 36 100       758 if (! defined $self->{'data_type'}) {
87 29         75 $self->{'data_type'} = 'plain';
88             }
89 36 100   40   266 if (none { $self->{'data_type'} eq $_ } @DATA_TYPES) {
  40         766  
90 1         4 err "Parameter 'data_type' has bad value.";
91             }
92              
93             # Check data based on type.
94 35         209 check_array($self, 'data');
95 34         347 foreach my $data_item (@{$self->{'data'}}) {
  34         120  
96             # Plain mode
97 6 100       20 if ($self->{'data_type'} eq 'plain') {
98 4 100       13 if (ref $data_item ne '') {
99 1         4 err "Parameter 'data' in 'plain' mode must contain ".
100             'reference to array with scalars.';
101             }
102             # Tags mode.
103             } else {
104 2 100       7 if (ref $data_item ne 'ARRAY') {
105 1         5 err "Parameter 'data' in 'tags' mode must contain ".
106             "reference to array with references ".
107             'to array with Tags structure.';
108             }
109             }
110             }
111              
112             # Check disabled.
113 32 100       99 if (! defined $self->{'disabled'}) {
114 29         68 $self->{'disabled'} = 0;
115             }
116 32         89 check_bool($self, 'disabled');
117              
118             # Check form_enctype.
119 32 100       493 if (defined $self->{'form_enctype'}) {
120 1 50   1   5 if (none { $self->{'form_enctype'} eq $_ } @ENCTYPES) {
  1         20  
121             err "Parameter 'form_enctype' has bad value.",
122 0         0 'Value', $self->{'form_enctype'},
123             ;
124             }
125             }
126              
127             # Check form_method.
128 32 100       92 if (! defined $self->{'form_method'}) {
129 31         70 $self->{'form_method'} = 'get';
130             }
131 32 50   33   150 if (none { $self->{'form_method'} eq $_ } @FORM_METHODS) {
  33         516  
132 0         0 err "Parameter 'form_method' has bad value.";
133             }
134              
135             # Check type.
136 32 100       172 if (! defined $self->{'type'}) {
137 28         79 $self->{'type'} = 'button';
138             }
139 32 100   39   129 if (none { $self->{'type'} eq $_ } @TYPES) {
  39         589  
140 1         4 err "Parameter 'type' has bad value.";
141             }
142              
143 31         116 return;
144             }
145              
146             1;
147              
148             __END__
149              
150             =pod
151              
152             =encoding utf8
153              
154             =head1 NAME
155              
156             Data::HTML::Button - Data object for HTML button element.
157              
158             =head1 SYNOPSIS
159              
160             use Data::HTML::Button;
161              
162             my $obj = Data::HTML::Button->new(%params);
163             my $autofocus = $obj->autofocus;
164             my $css_class = $obj->css_class;
165             my $data = $obj->data;
166             my $data_type = $obj->data_type;
167             my $disabled = $obj->disabled;
168             my $form = $obj->form;
169             my $form_enctype = $obj->form_enctype;
170             my $form_method = $obj->form_method;
171             my $id = $obj->id;
172             my $label = $obj->label;
173             my $name = $obj->name;
174             my $type = $obj->type;
175             my $value = $obj->value;
176              
177             =head1 METHODS
178              
179             =head2 C<new>
180              
181             my $obj = Data::HTML::Button->new(%params);
182              
183             Constructor.
184              
185             Returns instance of object.
186              
187             =over 8
188              
189             =item * C<autofocus>
190              
191             Button autofocus flag.
192              
193             Default value is 0.
194              
195             =item * C<css_class>
196              
197             Button CSS class.
198              
199             Default value is undef.
200              
201             =item * C<data>
202              
203             Button data content. It's reference to array.
204             Data type of data is described in 'data_type' parameter.
205              
206             Default value is [].
207              
208             =item * C<data_type>
209              
210             Button data type for content.
211              
212             Possible value are: plain tags
213              
214             Default value is 'plain'.
215              
216             =item * C<disabled>
217              
218             Button autofocus flag.
219              
220             Default value is 0.
221              
222             =item * C<form>
223              
224             Button form id.
225              
226             Default value is undef.
227              
228             =item * C<form_enctype>
229              
230             Button form encoding.
231             It's valuable for 'submit' type.
232              
233             Possible values are: application/x-www-form-urlencoded multipart/form-data text/plain
234              
235             Default value is undef.
236              
237             =item * C<form_method>
238              
239             Button form method.
240             It's valuable for 'submit' type.
241              
242             Possible values are: get post
243              
244             Default value is 'get'.
245              
246             =item * C<id>
247              
248             Button identifier.
249              
250             Default value is undef.
251              
252             =item * C<label>
253              
254             Button label.
255              
256             Default value is undef.
257              
258             =item * C<name>
259              
260             Button name.
261              
262             Default value is undef.
263              
264             =item * C<type>
265              
266             Button element type.
267              
268             Possible types: button reset submit
269              
270             Default value is 'button'.
271              
272             =item * C<value>
273              
274             Button value.
275              
276             Default value is undef.
277              
278             =back
279              
280             =head2 C<autofocus>
281              
282             my $autofocus = $obj->autofocus;
283              
284             Get button autofocus flag.
285              
286             Returns bool value (1/0).
287              
288             =head2 C<css_class>
289              
290             my $css_class = $obj->css_class;
291              
292             Get CSS class for button.
293              
294             Returns string.
295              
296             =head2 C<data>
297              
298             my $data = $obj->data;
299              
300             Get data inside button element.
301              
302             Returns reference to array.
303              
304             =head2 C<data_type>
305              
306             my $data_type = $obj->data_type;
307              
308             Get button data type.
309              
310             Returns string.
311              
312             =head2 C<disabled>
313              
314             my $disabled = $obj->disabled;
315              
316             Get button disabled flag.
317              
318             Returns bool value (1/0).
319              
320             =head2 C<form>
321              
322             my $form = $obj->form;
323              
324             Get button form id.
325              
326             Returns string.
327              
328             =head2 C<form_enctype>
329              
330             my $form_enctype = $obj->form_enctype;
331              
332             Get button form enctype.
333              
334             Returns string.
335              
336             =head2 C<form_method>
337              
338             my $form_method = $obj->form_method;
339              
340             Get button form method.
341              
342             Returns string.
343              
344             =head2 C<id>
345              
346             my $id = $obj->id;
347              
348             Get button identifier.
349              
350             Returns string.
351              
352             =head2 C<label>
353              
354             my $label = $obj->label;
355              
356             Get button label.
357              
358             Returns string.
359              
360             =head2 C<name>
361              
362             my $name = $obj->name;
363              
364             Get button name.
365              
366             Returns string.
367              
368             =head2 C<type>
369              
370             my $type = $obj->type;
371              
372             Get button type.
373              
374             Returns string.
375              
376             =head2 C<value>
377              
378             my $value = $obj->value;
379              
380             Get button value.
381              
382             Returns string.
383              
384             =head1 ERRORS
385              
386             new():
387             Parameter 'autofocus' must be a bool (0/1).
388             Value: %s
389             Parameter 'data' must be a array.
390             Value: %s
391             Reference: %s
392             Parameter 'data' in 'plain' mode must contain reference to array with scalars.
393             Parameter 'data' in 'tags' mode must contain reference to array with references to array with Tags structure.
394             Parameter 'data_type' has bad value.
395             Parameter 'disabled' must be a bool (0/1).
396             Value: %s
397             Parameter 'form_enctype' has bad value.
398             Value: %s
399             Parameter 'form_method' has bad value.
400             Parameter 'type' has bad value.
401              
402             =head1 EXAMPLE1
403              
404             =for comment filename=button_default.pl
405              
406             use strict;
407             use warnings;
408              
409             use Data::HTML::Button;
410              
411             my $obj = Data::HTML::Button->new;
412              
413             # Print out.
414             print 'Data type: '.$obj->data_type."\n";
415             print 'Form method: '.$obj->form_method."\n";
416             print 'Type: '.$obj->type."\n";
417              
418             # Output:
419             # Data type: plain
420             # Form method: get
421             # Type: button
422              
423             =head1 EXAMPLE2
424              
425             =for comment filename=button_tags.pl
426              
427             use strict;
428             use warnings;
429              
430             use Data::HTML::Button;
431             use Tags::Output::Raw;
432              
433             my $obj = Data::HTML::Button->new(
434             # Tags(3pm) structure.
435             'data' => [
436             ['b', 'span'],
437             ['d', 'Button'],
438             ['e', 'span'],
439             ],
440             'data_type' => 'tags',
441             );
442              
443             my $tags = Tags::Output::Raw->new;
444              
445             # Serialize data to output.
446             $tags->put(@{$obj->data});
447             my $data = $tags->flush(1);
448              
449             # Print out.
450             print 'Data (serialized): '.$data."\n";
451             print 'Data type: '.$obj->data_type."\n";
452             print 'Form method: '.$obj->form_method."\n";
453             print 'Type: '.$obj->type."\n";
454              
455             # Output:
456             # Data (serialized): <span>Button</span>
457             # Data type: tags
458             # Form method: get
459             # Type: button
460              
461             =head1 EXAMPLE3
462              
463             =for comment filename=button_plain.pl
464              
465             use strict;
466             use warnings;
467              
468             use Data::HTML::Button;
469              
470             my $obj = Data::HTML::Button->new(
471             # Plain content.
472             'data' => [
473             'Button',
474             ],
475             'data_type' => 'plain',
476             );
477              
478             # Serialize data to output.
479             my $data = join ' ', @{$obj->data};
480              
481             # Print out.
482             print 'Data: '.$data."\n";
483             print 'Data type: '.$obj->data_type."\n";
484             print 'Form method: '.$obj->form_method."\n";
485             print 'Type: '.$obj->type."\n";
486              
487             # Output:
488             # Data: Button
489             # Data type: plain
490             # Form method: get
491             # Type: button
492              
493             =head1 DEPENDENCIES
494              
495             L<Error::Pure>,
496             L<List::Util>,
497             L<Mo>,
498             L<Mo::utils>,
499             L<Readonly>.
500              
501             =head1 REPOSITORY
502              
503             L<https://github.com/michal-josef-spacek/Data-HTML-Button>
504              
505             =head1 AUTHOR
506              
507             Michal Josef Špaček L<mailto:skim@cpan.org>
508              
509             L<http://skim.cz>
510              
511             =head1 LICENSE AND COPYRIGHT
512              
513             © 2022-2023 Michal Josef Špaček
514              
515             BSD 2-Clause License
516              
517             =head1 VERSION
518              
519             0.05
520              
521             =cut