File Coverage

blib/lib/Data/HTML/Element/Form.pm
Criterion Covered Total %
statement 38 38 100.0
branch 8 8 100.0
condition n/a
subroutine 11 11 100.0
pod 0 1 0.0
total 57 58 98.2


line stmt bran cond sub pod time code
1             package Data::HTML::Element::Form;
2              
3 11     11   246749 use strict;
  11         26  
  11         467  
4 11     11   61 use warnings;
  11         20  
  11         809  
5              
6 11     11   10313 use Data::HTML::Element::Utils qw(check_data check_data_type);
  11         47  
  11         290  
7 11     11   957 use Error::Pure qw(err);
  11         24  
  11         610  
8 11     11   68 use List::Util 1.33 qw(none);
  11         170  
  11         824  
9 11     11   5669 use Mo qw(build is);
  11         8042  
  11         91  
10 11     11   26145 use Mo::utils::CSS 0.02 qw(check_css_class);
  11         157367  
  11         310  
11 11     11   1166 use Readonly;
  11         24  
  11         5445  
12              
13             Readonly::Array our @METHODS => qw(get post);
14             Readonly::Array our @ENCTYPES => (
15             'application/x-www-form-urlencoded',
16             'multipart/form-data',
17             'text/plain',
18             );
19              
20             our $VERSION = 0.17;
21              
22             has action => (
23             is => 'ro',
24             );
25              
26             has css_class => (
27             is => 'ro',
28             );
29              
30             has data => (
31             default => [],
32             ro => 1,
33             );
34              
35             has data_type => (
36             ro => 1,
37             );
38              
39             has enctype => (
40             is => 'ro',
41             );
42              
43             has id => (
44             is => 'ro',
45             );
46              
47             has label => (
48             is => 'ro',
49             );
50              
51             has method => (
52             is => 'ro',
53             );
54              
55             sub BUILD {
56 21     21 0 3494207 my $self = shift;
57              
58             # Check CSS class.
59 21         156 check_css_class($self, 'css_class');
60              
61             # Check data type.
62 21         434 check_data_type($self);
63              
64             # Check data based on type.
65 21         90 check_data($self);
66              
67             # Check enctype.
68 21 100       90 if (defined $self->{'enctype'}) {
69 3 100   7   20 if (none { $self->{'enctype'} eq $_ } @ENCTYPES) {
  7         49  
70             err "Parameter 'enctype' has bad value.",
71 1         21 'Value', $self->{'enctype'},
72             ;
73             }
74             }
75              
76             # Check method.
77 20 100       107 if (! defined $self->{'method'}) {
78 17         62 $self->{'method'} = 'get';
79             }
80 20 100   23   134 if (none { $self->{'method'} eq $_ } @METHODS) {
  23         221  
81             err "Parameter 'method' has bad value.",
82 1         10 'Value', $self->{'method'};
83             }
84              
85 19         294 return;
86             }
87              
88             1;
89              
90             __END__
91              
92             =pod
93              
94             =encoding utf8
95              
96             =head1 NAME
97              
98             Data::HTML::Element::Form - Data object for HTML form element.
99              
100             =head1 SYNOPSIS
101              
102             use Data::HTML::Element::Form;
103              
104             my $obj = Data::HTML::Element::Form->new(%params);
105             my $action = $obj->action;
106             my $css_class = $obj->css_class;
107             my $data = $obj->data;
108             my $data_type = $obj->data_type;
109             my $enctype = $obj->enctype;
110             my $id = $obj->id;
111             my $label = $obj->label;
112             my $method = $obj->method;
113              
114             =head1 METHODS
115              
116             =head2 C<new>
117              
118             my $obj = Data::HTML::Element::Form->new(%params);
119              
120             Constructor.
121              
122             =over 8
123              
124             =item * C<action>
125              
126             Form action.
127              
128             Default value is undef.
129              
130             =item * C<css_class>
131              
132             Form CSS class.
133              
134             Default value is undef.
135              
136             =item * C<data>
137              
138             Data content. It's reference to array.
139              
140             Data type of data is described in 'data_type' parameter.
141              
142             Default value is [].
143              
144             =item * C<data_type>
145              
146             Data type for content.
147              
148             Possible value are: cb plain tags
149              
150             The 'cb' content is code reference.
151             The 'plain' content are string(s).
152             The 'tags' content is structure described in L<Tags>.
153              
154             Default value is 'plain'.
155              
156             =item * C<enctype>
157              
158             Form enctype, attribute which specifies how the form-data should be encoded when
159             submitting it to the server.
160              
161             Possible values are:
162              
163             =over
164              
165             =item * (undefined - same as application/x-www-form-urlencoded)
166              
167             =item * application/x-www-form-urlencoded
168              
169             =item * multipart/form-data
170              
171             =item * text/plain
172              
173             =back
174              
175             Default value is undef.
176              
177             =item * C<id>
178              
179             Form identifier.
180              
181             Default value is undef.
182              
183             =item * C<label>
184              
185             Form label.
186              
187             Default value is undef.
188              
189             =item * C<method>
190              
191             Form method.
192              
193             Default value is 'get'.
194              
195             Possible methods are: get and post
196              
197             =back
198              
199             Returns instance of object.
200              
201             =head2 C<action>
202              
203             my $action = $obj->action;
204              
205             Get form action.
206              
207             Returns string.
208              
209             =head2 C<css_class>
210              
211             my $css_class = $obj->css_class;
212              
213             Get CSS class for form.
214              
215             Returns string.
216              
217             =head2 C<data>
218              
219             my $data = $obj->data;
220              
221             Get data inside button element.
222              
223             Returns reference to array.
224              
225             =head2 C<data_type>
226              
227             my $data_type = $obj->data_type;
228              
229             Get button data type.
230              
231             Returns string.
232              
233             =head2 C<enctype>
234              
235             my $enctype = $obj->enctype;
236              
237             Get enctype, attribute which specifies how the form-data should be encoded when
238             submitting it to the server.
239              
240             Returns string.
241              
242             =head2 C<id>
243              
244             my $id = $obj->id;
245              
246             Get form identifier.
247              
248             Returns string.
249              
250             =head2 C<label>
251              
252             my $label = $obj->label;
253              
254             Get form label.
255              
256             Returns string.
257              
258             =head2 C<method>
259              
260             my $method = $obj->method;
261              
262             Get form method.
263              
264             Returns string.
265              
266             =head1 ERRORS
267              
268             new():
269             Parameter 'css_class' has bad CSS class name.
270             Value: %s
271             Parameter 'css_class' has bad CSS class name (number on begin).
272             Value: %s
273             Parameter 'data' must be a array.
274             Value: %s
275             Reference: %s
276             Parameter 'data' in 'plain' mode must contain reference to array with scalars.
277             Parameter 'data' in 'tags' mode must contain reference to array with references to array with Tags structure.
278             Parameter 'data_type' has bad value.
279             Parameter 'enctype' has bad value.
280             Value: %s
281             Parameter 'method' has bad value.
282             Value: %s
283              
284             =head1 EXAMPLE1
285              
286             =for comment filename=form_default.pl
287              
288             use strict;
289             use warnings;
290              
291             use Data::HTML::Element::Form;
292              
293             my $obj = Data::HTML::Element::Form->new;
294              
295             # Print out.
296             print 'Method: '.$obj->method."\n";
297              
298             # Output:
299             # Method: get
300              
301             =head1 EXAMPLE2
302              
303             =for comment filename=form.pl
304              
305             use strict;
306             use warnings;
307              
308             use Data::HTML::Element::Form;
309              
310             my $obj = Data::HTML::Element::Form->new(
311             'action' => '/action.pl',
312             'css_class' => 'form',
313             'enctype' => 'multipart/form-data',
314             'id' => 'form-id',
315             'label' => 'Form label',
316             'method' => 'post',
317             );
318              
319             # Print out.
320             print 'Action: '.$obj->action."\n";
321             print 'CSS class: '.$obj->css_class."\n";
322             print 'Enctype: '.$obj->enctype."\n";
323             print 'Id: '.$obj->id."\n";
324             print 'Label: '.$obj->label."\n";
325             print 'Method: '.$obj->method."\n";
326              
327             # Output:
328             # Action: /action.pl
329             # CSS class: form
330             # Enctype: multipart/form-data
331             # Id: form-id
332             # Label: Form label
333             # Method: post
334              
335             =head1 DEPENDENCIES
336              
337             L<Error::Pure>,
338             L<List::Util>,
339             L<Mo>,
340             L<Mo::utils::CSS>,
341             L<Readonly>.
342              
343             =head1 REPOSITORY
344              
345             L<https://github.com/michal-josef-spacek/Data-HTML-Element>
346              
347             =head1 AUTHOR
348              
349             Michal Josef Špaček L<mailto:skim@cpan.org>
350              
351             L<http://skim.cz>
352              
353             =head1 LICENSE AND COPYRIGHT
354              
355             © 2022-2024 Michal Josef Špaček
356              
357             BSD 2-Clause License
358              
359             =head1 VERSION
360              
361             0.17
362              
363             =cut