| 1 | |||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package Jifty::DBI::Filter::utf8; | ||
| 6 | use base qw/Jifty::DBI::Filter/; | ||
| 7 | use Encode (); | ||
| 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 recommneded. | ||
| 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 | my $self = shift; | ||
| 33 | |||
| 34 | my $value_ref = $self->value_ref; | ||
| 35 | return undef unless ( defined($$value_ref) ); | ||
| 36 | |||
| 37 | if ( Encode::is_utf8($$value_ref) ) { | ||
| 38 | $$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 | $$value_ref = Encode::encode_utf8( | ||
| 46 | Encode::decode_utf8( $$value_ref, Encode::FB_QUIET ) ); | ||
| 47 | } | ||
| 48 | 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 0xFFFD code point. | ||
| 55 | |||
| 56 | Always set UTF-8 flag on the value. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub decode { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | my $value_ref = $self->value_ref; | ||
| 64 | return undef unless ( defined($$value_ref) ); | ||
| 65 | |||
| 66 | unless ( Encode::is_utf8($$value_ref) ) { | ||
| 67 | $$value_ref = Encode::decode_utf8($$value_ref); | ||
| 68 | } | ||
| 69 | return 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | __END__ | ||
| 74 | |||
| 75 | =head1 SEE ALSO | ||
| 76 | |||
| 77 | L<Jifty::DBI::Filter>, L<perlunicode> | ||
| 78 | |||
| 79 | =cut |