File Coverage

blib/lib/Config/Apple/Profile/Payload/Types/Serialize.pm
Criterion Covered Total %
statement 35 50 70.0
branch 12 22 54.5
condition 4 6 66.6
subroutine 9 9 100.0
pod 1 1 100.0
total 61 88 69.3


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 7     7   105 use 5.14.4;
  7         26  
  7         332  
7 7     7   46 use strict;
  7         15  
  7         256  
8 7     7   40 use warnings FATAL => 'all';
  7         16  
  7         427  
9              
10             our $VERSION = '0.55';
11              
12 7     7   42 use DateTime;
  7         17  
  7         237  
13 7     7   40 use Encode qw(encode);
  7         14  
  7         531  
14             use Exporter::Easy (
15 7         90 OK => [qw(
16             serialize
17             )],
18             TAGS => [
19             'all' => [qw(
20             serialize
21             )],
22             ],
23 7     7   38 );
  7         22  
24 7     7   1341 use Mac::PropertyList;
  7         23  
  7         371  
25 7     7   41 use Config::Apple::Profile::Payload::Types qw(:all);
  7         24  
  7         5111  
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 259     259 1 6061 my ($type, $value, $subtype) = @_;
68            
69             # Strings need to be encoded as UTF-8 before export
70 259 100 100     789 if ( ($type == $ProfileString)
    100 33        
    100          
    50          
    50          
    50          
    100          
    50          
    0          
    0          
71             || ($type == $ProfileIdentifier)
72             ) {
73 31         358 $value = Mac::PropertyList::string->new(
74             Encode::encode('UTF-8', $value)
75             );
76             }
77            
78             # Numbers are easy
79             elsif ($type == $ProfileNumber) {
80 42         690 $value = Mac::PropertyList::integer->new($value);
81             }
82            
83             # Reals are similarly easy, but use an uppercase E
84             elsif ($type == $ProfileReal) {
85 36         932 $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         2308 $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         5307 my @array;
126            
127             # Go through each array item, serialize it, and add it to our array
128 100         397 foreach my $item (@$value) {
129 63         187 push @array, serialize($subtype, $item, undef);
130             }
131            
132 100         976 $value = Mac::PropertyList::array->new(\@array);
133             }
134            
135             # For hashed, make a Perl hash of fragments, then plist that
136             elsif ($type == $ProfileDict) {
137 0         0 my %hash;
138            
139             # Go through each hash key, serialize it, and add it to our array
140 0         0 foreach my $key (keys %$value) {
141 0         0 $hash{$key} = serialize($subtype, $value->{$key}, undef);
142             }
143            
144 0         0 $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 259         9173 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;