ICal/Property.pm

filter: Statementstmt 97.9% 97 / 99 Branchbran 87.5% 28 / 32 Conditioncond 75.0% 12 / 16 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 92.0% 162 / 176 CC 26 SCAR 12.7
CC
26
Cov
93%
_value_as_string
6.1
_fold
6.0
_quoted_parameter_values
4.0
CRAP 3.6
« ICal/Entry.pm ↑ Summary ICal/Entry/FreeBusy.pm »
1 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Data::ICal::Property;
5  
6 14 ▶use base qw/Class::Accessor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use MIME::QuotedPrint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 our $VERSION = '0.06';
12  
13 =head1 NAME
14  
15 Data::ICal::Property - Represents a property on an entry in an iCalendar file
16  
17 =head1 DESCRIPTION
18  
19 A L<Data::ICal::Property> object represents a single property on an
20 entry in an iCalendar file. Properties have parameters in addition to
21 their value.
22  
23 You shouldn't need to create L<Data::ICal::Property> values directly
24 -- just use C<add_property> in L<Data::ICal::Entry>.
25  
26 The C<encoding> parameter value is only interpreted by L<Data::ICal>
27 in the C<decoded_value> and C<encode> methods: all other methods
28 access the encoded version directly (if there is an encoding).
29  
30 Currently, the only supported encoding is C<QUOTED-PRINTABLE>.
31  
32 =head1 METHODS
33  
34 =cut
35  
36 =head2 new $key, $value, [$parameter_hash]
37  
38 Creates a new L<Data::ICal::Property> with key C<$key> and value C<$value>.
39  
40 If C<$parameter_hash> is provided, sets the property's parameters to
41 it. The parameter hash should have keys equal to the names of the
42 parameters (case insensitive; parameter hashes should not contain two
43 different keys which are the same when converted to upper case); the
44 values should either be a string if the parameter has a single value
45 or an array reference of strings if the parameter has multiple values.
46  
47 =cut
48  
49 362 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
50 362 my $class = shift;
51 362 my $self = {};
52  
53 362 bless $self, $class;
54  
55 362 $self->key(shift);
56 362 $self->value(shift);
57 362 ▶ $self->parameters( shift || {} );
Condition50%1/2
shift() || {}
l!l
3620
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
58 362 return ($self);
59 }
60  
61 =head2 key [$key]
62  
63 Gets or sets the key name of this property.
64  
65 =head2 value [$value]
66  
67 Gets or sets the value of this property.
68  
69 =head2 parameters [$param_hash]
70  
71 Gets or sets the parameter hash reference of this property. Parameter
72 keys are converted to upper case.
73  
74 =head2 vcal10 [$bool]
75  
76 Gets or sets a boolean saying whether this should be interpreted as
77 vCalendar 1.0 (as opposed to iCalendar 2.0). Generally, you can just
78 set this on your main L<Data::ICal> object when you construct it;
79 C<add_entry> automatically makes sure that sub-entries end up with the
80 same value as their parents, and C<add_property> makes sure that
81 properties end up with the same value as their entry.
82  
83 =cut
84  
85 14 __PACKAGE__->mk_accessors(qw(key value _parameters vcal10));
86  
87 651 ▶sub parameters {
Subroutine parameters: called CC=3 SCAR=11.0
Pod documented
88 651 my $self = shift;
89  
90 651 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 362 289 651
91 362 my $params = shift;
92 362 my $new_params = {};
93 362 ▶ while ( my ( $k, $v ) = each %$params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$params) 41 362 403
94 41 $new_params->{ uc $k } = $v;
95 }
96 362 $self->_parameters($new_params);
97 }
98  
99 651 return $self->_parameters;
100 }
101  
102 my %ENCODINGS = (
103 'QUOTED-PRINTABLE' => {
104 2 ▶ encode => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
105 2 ▶ my $dec = shift || '';
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
106 2 $dec =~ s/\n/\r\n/g;
107 2 return MIME::QuotedPrint::encode( $dec, '' );
108 },
109 2 ▶ decode => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
110 2 ▶ my $dec = MIME::QuotedPrint::decode( shift || '' );
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
111 2 $dec =~ s/\r\n/\n/g;
112 2 return $dec;
113 }
114 },
115 14 );
116  
117 =head2 decoded_value
118  
119 Gets the value of this property, converted from the encoding specified
120 in its encoding parameter. (That is, C<value> will return the encoded
121 version; this will apply the encoding.) If the encoding is not
122 specified or recognized, just returns the raw value.
123  
124 =cut
125  
126 4 ▶sub decoded_value {
Subroutine decoded_value: called CC=3 SCAR=11.0
Pod documented
127 4 my $self = shift;
128 4 my $value = $self->value;
129 4 ▶ my $encoding = uc( $self->parameters->{'ENCODING'} || "" );
Condition100%2/2
$self->parameters->{'ENCODING'} || ''
l!l
22
MC/DC100%1/1
$self->parameters->{'ENCODING'} || ''
$self->parameters->{'ENCODING'}
Truth table100%2/2
A
A= $self->parameters->{'ENCODING'}
Aresult
11
00
130  
131 4 ▶ if ( $ENCODINGS{$encoding} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENCODINGS{$encoding}) { } 2 2 4
132 2 return $ENCODINGS{$encoding}{'decode'}->($value);
133 } else {
134 2 return $value;
135 }
136 }
137  
138 =head2 encode $encoding
139  
140 Calls C<decoded_value> to get the current decoded value, then encodes
141 it in C<$encoding>, sets the value to that, and sets the encoding
142 parameter to C<$encoding>. (C<$encoding> is first converted to upper
143 case.)
144  
145 If C<$encoding> is undef, deletes the encoding parameter and sets the
146 value to the decoded value. Does nothing if the encoding is not
147 recognized.
148  
149 =cut
150  
151 2 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.8
Pod documented
152 2 my $self = shift;
153 2 my $encoding = uc shift;
154  
155 2 my $decoded_value = $self->decoded_value;
156  
157 2 ▶ if ( not defined $encoding ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $encoding) { } 0 2 2
elsif ($ENCODINGS{$encoding}) { } 2 0 2
158 0 $self->value($decoded_value);
159 0 delete $self->parameters->{'ENCODING'};
160 } elsif ( $ENCODINGS{$encoding} ) {
161 2 $self->value( $ENCODINGS{$encoding}{'encode'}->($decoded_value) );
162 2 $self->parameters->{'ENCODING'} = $encoding;
163 }
164 2 return $self;
165 }
166  
167 =head2 as_string ARGS
168  
169 Returns the property formatted as a string (including trailing
170 newline).
171  
172 Takes named arguments:
173  
174 =over
175  
176 =item fold
177  
178 Defaults to true. pass in a false value if you need to generate
179 non-rfc-compliant calendars.
180  
181 =item crlf
182  
183 Defaults to C<\x0d\x0a>, per RFC 2445 spec. This option is primarily
184 for backwards compatibility with version of this module prior to 0.16,
185 which used C<\x0a>.
186  
187 =back
188  
189 =cut
190  
191 204 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=6.9
Pod documented
192 204 my $self = shift;
193 204 my %args = (
194 fold => 1,
195 crlf => Data::ICal::Entry->CRLF,
196 @_
197 );
198 my $string
199 = uc( $self->key )
200 . $self->_parameters_as_string . ":"
201 . $self->_value_as_string( $self->key )
202 204 . $args{crlf};
203  
204 # Assumption: the only place in an iCalendar that needs folding are property
205 # lines
206 204 ▶ if ( $args{'fold'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"fold"}) 202 2 204
207 202 return $self->_fold( $string, $args{crlf} );
208 }
209  
210 2 return $string;
211 }
212  
213 =begin private
214  
215 =head2 _value_as_string
216  
217 Returns the property's value as a string. Comma and semicolon are not
218 escaped when the value is recur type (the key is rrule).
219  
220 Values are quoted according the iCal spec, unless this is in vCal 1.0
221 mode.
222  
223 =end private
224  
225 =cut
226  
227 204 ▶sub _value_as_string {
Subroutine _value_as_string: called CC=6 SCAR=18.1
228 204 my $self = shift;
229 204 my $key = shift;
230 204 ▶ my $value = defined( $self->value() ) ? $self->value() : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->value ? : 204 0 204
231  
232 204 ▶ unless ( $self->vcal10 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->vcal10) 151 53 204
233 151 $value =~ s/\\/\\\\/gs;
234 151 ▶ $value =~ s/;/\\;/gs unless lc($key) eq 'rrule' || lc($key) eq 'geo';
Branch: 1 branch
BranchTrueFalseTotal
unless lc $key eq "rrule" or lc $key eq "geo" 150 1 151
Condition66%2/3
lc $key eq "rrule" or lc $key eq "geo"
l!l&&r!l&&!r
01150
MC/DC50%1/2
lc $key eq "rrule" or lc $key eq "geo"
lc $key eq "rrule" lc $key eq "geo"
Truth table66%2/3
A or B
A= lc $key eq "rrule"B= lc $key eq "geo"
ABresult
1X1
011
000
235 151 ▶ $value =~ s/,/\\,/gs unless lc($key) eq 'rrule';
Branch: 1 branch
BranchTrueFalseTotal
unless lc $key eq "rrule" 151 0 151
236 151 $value =~ s/\x0d?\x0a/\\n/gs;
237 }
238  
239 204 return $value;
240 }
241  
242 =begin private
243  
244 =head2 _parameters_as_string
245  
246 Returns the property's parameters as a string. Properties are sorted alphabetically
247 to aid testing.
248  
249 =end private
250  
251 =cut
252  
253 204 ▶sub _parameters_as_string {
Subroutine _parameters_as_string: called CC=3 SCAR=11.0
254 204 my $self = shift;
255 204 my $out = '';
256 204 for my $name ( sort keys %{ $self->parameters } ) {
257 23 my $value = $self->parameters->{$name};
258 23 ▶ $out
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 2 21 23
259 .= ';'
260 . $name . '='
261 . $self->_quoted_parameter_values(
262 ref $value ? @$value : $value );
263 }
264 204 return $out;
265 }
266  
267 =begin private
268  
269 =head2 _quoted_parameter_values @values
270  
271 Quotes any of the values in C<@values> that need to be quoted and
272 returns the quoted values joined by commas.
273  
274 If any of the values contains a double-quote, erases it and emits a
275 warning.
276  
277 =end private
278  
279 =cut
280  
281 23 ▶sub _quoted_parameter_values {
Subroutine _quoted_parameter_values: called CC=4 SCAR=13.9
282 23 my $self = shift;
283 23 my @values = @_;
284  
285 23 for my $val (@values) {
286 25 ▶ if ( $val =~ /"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /"/) 1 24 25
287  
288 # Get all the way back to the user's code
289 1 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
290 1 carp "Invalid parameter value (contains double quote): $val";
291 1 $val =~ tr/"//d;
292 }
293 }
294  
295 23 ▶ return join ',', map { /[;,:]/ ? qq("$_") : $_ } @values;
Branch: 1 branch
BranchTrueFalseTotal
/[;,:]/ ? : 4 21 25
296 }
297  
298 =begin private
299  
300 =head2 _fold $string $crlf
301  
302 Returns C<$string> folded with newlines and leading whitespace so that
303 each line is at most 75 characters.
304  
305 (Note that it folds at 75 characters, not 75 bytes as specified in the
306 standard.)
307  
308 If this is vCalendar 1.0 and encoded with QUOTED-PRINTABLE, does not
309 fold at all.
310  
311 =end private
312  
313 =cut
314  
315 202 ▶sub _fold {
Subroutine _fold: called CC=6 SCAR=17.9
316 202 my $self = shift;
317 202 my $string = shift;
318 202 my $crlf = shift;
319  
320 my $quoted_printable = $self->vcal10
321 202 ▶ && uc( $self->parameters->{'ENCODING'} || '' ) eq 'QUOTED-PRINTABLE';
Condition100%5/5
$self->vcal10 && uc($self->parameters->{'ENCODING'} || '') eq 'QUOTED-PRINTABLE'
!ll&&!rl&&r
149503
$self->parameters->{'ENCODING'} || ''
l!l
350
MC/DC100%2/2
$self->vcal10 && uc($self->parameters->{'ENCODING'} || '') eq 'QUOTED-PRINTABLE'
$self->vcal10 $self->parameters->{'ENCODING'}
Truth table100%3/3
A && B
A= $self->vcal10B= $self->parameters->{'ENCODING'}
ABresult
0X0
100
111
322  
323 202 ▶ if ($quoted_printable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted_printable) { } 3 199 202
324  
325 # In old vcal, quoted-printable properties have different folding rules.
326 # But some interop tests suggest it's wiser just to not fold for vcal 1.0
327 # at all (in quoted-printable).
328 } else {
329 199 ▶ return $string unless length $string > 75;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string > 75 183 16 199
330  
331 16 $string =~ s{$crlf\z}{};
332  
333 16 my $out = substr($string, 0, 75, "") . $crlf;
334  
335 16 ▶ while (length $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 25 16 41
336 25 my $substr = substr $string, 0, 74, "";
337 25 $out .= " $substr$crlf";
338 }
339  
340 16 return $out;
341 }
342  
343 3 return $string;
344 }
345  
346 =head1 AUTHOR
347  
348 Best Practical Solutions, LLC E<lt>modules@bestpractical.comE<gt>
349  
350 =head1 LICENCE AND COPYRIGHT
351  
352 Copyright (c) 2005 - 2020, Best Practical Solutions, LLC. All rights reserved.
353  
354 This module is free software; you can redistribute it and/or
355 modify it under the same terms as Perl itself. See L<perlartistic>.
356  
357 =cut
358  
359 14 1;
360  
« ICal/Entry.pm ↑ Summary ICal/Entry/FreeBusy.pm »