File Coverage

blib/lib/Stancer/Core/Types/String.pm
Criterion Covered Total %
statement 62 62 100.0
branch 24 24 100.0
condition 3 3 100.0
subroutine 16 16 100.0
pod n/a
total 105 105 100.0


line stmt bran cond sub pod time code
1             package Stancer::Core::Types::String;
2              
3 61     61   442455 use 5.020;
  61         252  
4 61     61   343 use strict;
  61         139  
  61         2031  
5 61     61   312 use warnings;
  61         132  
  61         8410  
6              
7             # ABSTRACT: Internal strings types
8             our $VERSION = '1.0.3'; # VERSION
9              
10             our @EXPORT_OK = ();
11             our %EXPORT_TAGS = ('all' => \@EXPORT_OK);
12              
13 61     61   1634 use Stancer::Core::Types::Helper qw(error_message);
  61         163  
  61         4442  
14              
15 61     61   31197 use Stancer::Exceptions::InvalidDescription;
  61         323  
  61         2908  
16 61     61   35877 use Stancer::Exceptions::InvalidEmail;
  61         259  
  61         2797  
17 61     61   31549 use Stancer::Exceptions::InvalidExternalId;
  61         271  
  61         2874  
18 61     61   31947 use Stancer::Exceptions::InvalidMobile;
  61         277  
  61         2865  
19 61     61   31669 use Stancer::Exceptions::InvalidName;
  61         298  
  61         2664  
20 61     61   31638 use Stancer::Exceptions::InvalidOrderId;
  61         280  
  61         2902  
21 61     61   32125 use Stancer::Exceptions::InvalidUniqueId;
  61         263  
  61         2774  
22              
23 61     61   474 use namespace::clean;
  61         132  
  61         315  
24              
25 61     61   14788 use Exporter qw(import);
  61         178  
  61         43802  
26              
27             sub _create_type {
28 488     488   956 my ($name, $params) = @_;
29              
30             return {
31             name => $name,
32             exception => $params->{exception},
33             test => sub {
34 298     298   109174 my ($value, $min, $max) = @_;
35              
36 298 100       1306 $min = $params->{min} if defined $params->{min};
37 298 100       1165 $max = $params->{max} if defined $params->{max};
38              
39 298 100       939 return 0 if not defined $value;
40              
41 288 100       822 $max = 0 if not defined $max;
42 288 100       823 $min = 0 if not defined $min;
43              
44 288 100       779 if ($min > $max) {
45 14         31 ($min, $max) = ($max, $min);
46             }
47              
48 288   100     1957 return length($value) >= $min && length($value) <= $max;
49             },
50             message => sub {
51 30     30   117 my ($value, $min, $max) = @_;
52              
53 30 100       85 $min = $params->{min} if defined $params->{min};
54 30 100       78 $max = $params->{max} if defined $params->{max};
55              
56 30 100       68 if (defined $value) {
57 20         57 $value = q/"/ . $value . q/"/;
58             } else {
59 10         23 $value = 'undef';
60             }
61              
62 30 100       102 if (not defined $max) {
63 2         33 return 'Must be at maximum ' . $min . ' characters, tried with ' . $value . q/./;
64             }
65              
66 28 100       136 if (not defined $min) {
67 7         159 return 'Must be at maximum ' . $max . ' characters, tried with ' . $value . q/./;
68             }
69              
70 21 100       47 if ($min > $max) {
71 3         9 ($min, $max) = ($max, $min);
72             }
73              
74 21         275 return 'Must be an string between ' . $min . ' and ' . $max . ' characters, tried with ' . $value . q/./;
75             },
76 488         4901 };
77             }
78              
79             my @defs = ();
80              
81             push @defs, {
82             name => 'Char',
83             test => sub {
84             my ($value, $param) = @_;
85              
86             if (not defined $value) {
87             return 0;
88             }
89              
90             length($value) == $param;
91             },
92             ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
93             message => error_message('Must be exactly %2$d characters, tried with %1$s.'),
94             ## use critic
95             };
96              
97             push @defs, _create_type('Varchar');
98              
99             push @defs, _create_type('Description', { min => 3, max => 64, exception => 'Stancer::Exceptions::InvalidDescription' });
100             push @defs, _create_type('Email', { min => 5, max => 64, exception => 'Stancer::Exceptions::InvalidEmail' });
101             push @defs, _create_type('ExternalId', { max => 36, exception => 'Stancer::Exceptions::InvalidExternalId' });
102             push @defs, _create_type('Mobile', { min => 8, max => 16, exception => 'Stancer::Exceptions::InvalidMobile' });
103             push @defs, _create_type('Name', { min => 4, max => 64, exception => 'Stancer::Exceptions::InvalidName' });
104             push @defs, _create_type('OrderId', { max => 36, exception => 'Stancer::Exceptions::InvalidOrderId' });
105             push @defs, _create_type('UniqueId', { max => 36, exception => 'Stancer::Exceptions::InvalidUniqueId' });
106              
107             Stancer::Core::Types::Helper::register_types(\@defs, __PACKAGE__);
108              
109             1;
110              
111             __END__
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Stancer::Core::Types::String - Internal strings types
120              
121             =head1 VERSION
122              
123             version 1.0.3
124              
125             =head1 USAGE
126              
127             =head2 Logging
128              
129              
130              
131             We use the L<Log::Any> framework for logging events.
132             You may tell where it should log using any available L<Log::Any::Adapter> module.
133              
134             For example, to log everything to a file you just have to add a line to your script, like this:
135             #! /usr/bin/env perl
136             use Log::Any::Adapter (File => '/var/log/payment.log');
137             use Stancer::Core::Types::String;
138              
139             You must import C<Log::Any::Adapter> before our libraries, to initialize the logger instance before use.
140              
141             You can choose your log level on import directly:
142             use Log::Any::Adapter (File => '/var/log/payment.log', log_level => 'info');
143              
144             Read the L<Log::Any> documentation to know what other options you have.
145              
146             =cut
147              
148             =head1 SECURITY
149              
150             =over
151              
152             =item *
153              
154             Never, never, NEVER register a card or a bank account number in your database.
155              
156             =item *
157              
158             Always uses HTTPS in card/SEPA in communication.
159              
160             =item *
161              
162             Our API will never give you a complete card/SEPA number, only the last four digits.
163             If you need to keep track, use these last four digit.
164              
165             =back
166              
167             =cut
168              
169             =head1 BUGS
170              
171             Please report any bugs or feature requests on the bugtracker website
172             L<https://gitlab.com/wearestancer/library/lib-perl/-/issues> or by email to
173             L<bug-stancer@rt.cpan.org|mailto:bug-stancer@rt.cpan.org>.
174              
175             When submitting a bug or request, please include a test-file or a
176             patch to an existing test-file that illustrates the bug or desired
177             feature.
178              
179             =head1 AUTHOR
180              
181             Joel Da Silva <jdasilva@cpan.org>
182              
183             =head1 COPYRIGHT AND LICENSE
184              
185             This software is Copyright (c) 2018-2024 by Stancer / Iliad78.
186              
187             This is free software, licensed under:
188              
189             The Artistic License 2.0 (GPL Compatible)
190              
191             =cut