DBI/Filter/utf8.pm

filter: Statementstmt 100% 26 / 26 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 97.6% 41 / 42 CC 5 SCAR 7.9
CC
5
Cov
97%
decode
3.0
encode
3.0
BEGIN
1.0
CRAP 2.2
« DBI/Filter/URI.pm ↑ Summary DBI/Filter/SaltHash.pm »
1  
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Jifty::DBI::Filter::utf8;
6 1 ▶use base qw/Jifty::DBI::Filter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Jifty::DBI::Filter::utf8 - Jifty::DBI UTF-8 data filter
12  
13 =head1 DESCRIPTION
14  
15 This filter allow you to check that you operate with
16 valid UTF-8 data.
17  
18 Usage as type specific filter is recommended.
19  
20 =head1 METHODS
21  
22 =head2 encode
23  
24 Method always unset UTF-8 flag on the value, but
25 if value doesn't have flag then method checks
26 value for malformed UTF-8 data and stop on
27 the first bad code.
28  
29 =cut
30  
31 sub encode {
32 5 ▶ my $self = shift;
Subroutine encode: called CC=3 SCAR=11.0
Pod documented
33  
34 5 my $value_ref = $self->value_ref;
35 5 ▶ return undef unless ( defined($$value_ref) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$value_ref 1 4 5
36  
37 4 ▶ if ( Encode::is_utf8($$value_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($$value_ref)) { } 1 3 4
38 1 $$value_ref = Encode::encode_utf8($$value_ref);
39 } else {
40  
41 # if value has no utf8 flag but filter on the stack
42 # we do double encoding, and stop on the first bad characters
43 # with FB_QUIET fallback schema. We this schema because we
44 # don't want data grow
45 3 $$value_ref = Encode::encode_utf8(
46 Encode::decode_utf8( $$value_ref, Encode::FB_QUIET ) );
47 }
48 4 return 1;
49 }
50  
51 =head2 decode
52  
53 Checks whether value is correct UTF-8 data or not and
54 substitute all malformed data with the C<0xFFFD> code point.
55  
56 Always set UTF-8 flag on the value.
57  
58 =cut
59  
60 sub decode {
61 6 ▶ my $self = shift;
Subroutine decode: called CC=3 SCAR=11.0
Pod documented
62  
63 6 my $value_ref = $self->value_ref;
64 6 ▶ return undef unless ( defined($$value_ref) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$value_ref 2 4 6
65  
66 4 ▶ unless ( Encode::is_utf8($$value_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Encode::is_utf8($$value_ref)) 4 0 4
67 4 $$value_ref = Encode::decode_utf8($$value_ref);
68 }
69 4 return 1;
70 }
71  
72 1 1;
73 __END__
74  
75 =head1 SEE ALSO
76  
77 L<Jifty::DBI::Filter>, L<perlunicode>
78  
79 =cut
« DBI/Filter/URI.pm ↑ Summary DBI/Filter/SaltHash.pm »