File Coverage

blib/lib/Config/Apple/Profile/Payload/Types/Serialize.pm
Criterion Covered Total %
statement 39 50 78.0
branch 14 22 63.6
condition 4 6 66.6
subroutine 9 9 100.0
pod 1 1 100.0
total 67 88 76.1


line stmt bran cond sub pod time code
1             # This is the code for Config::Apple::Profile::Payload::Types::Serialize.
2             # For Copyright, please see the bottom of the file.
3              
4             package Config::Apple::Profile::Payload::Types::Serialize;
5              
6 15     15   171 use 5.10.1;
  15         38  
  15         634  
7 15     15   65 use strict;
  15         21  
  15         429  
8 15     15   58 use warnings FATAL => 'all';
  15         23  
  15         740  
9              
10             our $VERSION = '0.87.1';
11              
12 15     15   110 use DateTime;
  15         27  
  15         308  
13 15     15   58 use Encode qw(encode);
  15         20  
  15         851  
14             use Exporter::Easy (
15 15         123 OK => [qw(
16             serialize
17             )],
18             TAGS => [
19             'all' => [qw(
20             serialize
21             )],
22             ],
23 15     15   73 );
  15         23  
24 15     15   2638 use Mac::PropertyList;
  15         21  
  15         625  
25 15     15   67 use Config::Apple::Profile::Payload::Types qw(:all);
  15         21  
  15         7554  
26              
27              
28             =encoding utf8
29              
30             =head1 NAME
31              
32             Config::Apple::Profile::Payload::Types::Serialize - Convert common payload
33             types to plist form.
34              
35              
36             =head1 DESCRIPTION
37              
38             This module contains code that is used to convert common payload types into
39             plist form.
40              
41              
42             =head1 FUNCTIONS
43              
44             =head2 serialize
45              
46             my $plist_fragment = serialize($type, $value, [$subtype]);
47              
48             Given C<$value>, returns a C object containing the contents
49             of C<$value>. C<$type> must be one of the types listed in
50             L, and is used to identify which type
51             of plist item to create (string, number, array, etc.).
52              
53             If C<$type> is C<$ProfileArray> or C<$ProfileDict>, then C<$subtype> must be
54             defined. C will recurse into the structure, serialize it, and then
55             put everything into the appropriate plist array or dictionary, which will be
56             returned. C<$subtype> is used to tell C what type of data is being
57             used within the array or dictionary.
58              
59             If C<$type> is C<$ProfileClass>, then C<< $value->plist >> will be called,
60             and the result will be returned.
61              
62             An exception will be thrown if C<$type> or C<$subtype> are not recognized.
63              
64             =cut
65              
66             sub serialize {
67 302     302 1 4560 my ($type, $value, $subtype) = @_;
68            
69             # Strings need to be encoded as UTF-8 before export
70 302 100 100     632 if ( ($type == $ProfileString)
    100 33        
    100          
    50          
    50          
    50          
    100          
    100          
    50          
    0          
71             || ($type == $ProfileIdentifier)
72             ) {
73 31         230 $value = Mac::PropertyList::string->new(
74             Encode::encode('UTF-8', $value)
75             );
76             }
77            
78             # Numbers are easy
79             elsif ($type == $ProfileNumber) {
80 73         942 $value = Mac::PropertyList::integer->new($value);
81             }
82            
83             # Reals are similarly easy, but use an uppercase E
84             elsif ($type == $ProfileReal) {
85 36         577 $value = Mac::PropertyList::real->new(uc($value));
86             }
87            
88             # All data is Base64-encoded for us by Mac::PropertyList
89             elsif ( ($type == $ProfileData)
90             || ($type == $ProfileNSDataBlob)
91             ) {
92             # Slurp the file, and then hand it over
93 0         0 local $/ = undef;
94 0         0 $value = <$value>;
95 0         0 $value = Mac::PropertyList::data->new($value);
96             }
97            
98             # There are separate objects for true/false booleans
99             elsif ($type == $ProfileBool) {
100 0 0       0 if ($value) {
101 0         0 $value = Mac::PropertyList::true->new;
102             }
103             else {
104 0         0 $value = Mac::PropertyList::false->new;
105             }
106             }
107            
108             # Date
109             elsif ($type == $ProfileDate) {
110             # Set the time zone to UTC, make a string, and plist that
111 0         0 $value->set_time_zone('UTC');
112 0         0 my $string = $value->ymd . 'T' . $value->hms . 'Z';
113 0         0 $value = Mac::PropertyList::date->new($string);
114             }
115            
116             # UUIDs are converted to strings, then processed as such
117             elsif ($type == $ProfileUUID) {
118 50         1988 $value = Mac::PropertyList::string->new(
119             Encode::encode('UTF-8', $value->as_string())
120             );
121             }
122            
123             # For arrays, make a Perl array of fragments, then plist that
124             elsif ($type == $ProfileArray) {
125 100         3649 my @array;
126            
127             # Go through each array item, serialize it, and add it to our array
128 100         341 foreach my $item (@$value) {
129 63         123 push @array, serialize($subtype, $item, undef);
130             }
131            
132 100         630 $value = Mac::PropertyList::array->new(\@array);
133             }
134            
135             # For dictionaries, make a Perl hash of fragments, then plist that
136             elsif ($type == $ProfileDict) {
137 12         426 my %hash;
138            
139             # Go through each hash key, serialize it, and add it to our array
140 12         44 foreach my $key (keys %$value) {
141 31         113 $hash{$key} = serialize($subtype, $value->{$key}, undef);
142             }
143            
144 12         52 $value = Mac::PropertyList::dict->new(\%hash);
145             }
146            
147             # For classes, let the object plist itself
148             elsif ($type == $ProfileClass) {
149 0         0 $value = $value->plist;
150             }
151            
152             # We've checked all the types we know about
153             else {
154 0         0 die "Unknown type $type";
155             }
156            
157 302         8306 return $value;
158             }
159              
160              
161              
162              
163             =head1 ACKNOWLEDGEMENTS
164              
165             Refer to the L for acknowledgements.
166              
167             =head1 AUTHOR
168              
169             A. Karl Kornel, C<< >>
170              
171             =head1 COPYRIGHT AND LICENSE
172              
173             Copyright © 2014 A. Karl Kornel.
174              
175             This program is free software; you can redistribute it and/or modify it
176             under the terms of either: the GNU General Public License as published
177             by the Free Software Foundation; or the Artistic License.
178              
179             See L for more information.
180              
181             =cut
182              
183             1;