RPC/Value.pm

filter: Statementstmt 39.6% 44 / 111 Branchbran 10.0% 2 / 20 Conditioncond 5.0% 1 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 73.3% 11 / 15 Podpod 83.3% 5 / 6 totaltotal 34.0% 63 / 185 CC 21 SCAR 26.6
CC
21
Cov
31%
value
30.0
node_to_value
30.0
type
20.0
CRAP 14.3
« RPC/Response.pm ↑ Summary RPC/Request.pm »
1 package POE::Filter::XML::RPC::Value;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base('POE::Filter::XML::Node', 'Exporter');
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util('looks_like_number', 'reftype');
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Regexp::Common('time');
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Hash::Util('fieldhash');
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.04';
13  
14 use constant
15 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 'ARRAY' => 'array',
17 'BASE64' => 'base64',
18 'BOOL' => 'bool',
19 'DATETIME' => 'dateTime.iso8601',
20 'DOUBLE' => 'double',
21 'INT' => 'int',
22 'STRING' => 'string',
23 'STRUCT' => 'struct',
24 'DATA' => 'data',
25 'NAME' => 'name',
26 'VALUE' => 'value',
27 'MEMBER' => 'member',
28 2 };
29  
30 2 our @EXPORT= qw/ ARRAY BASE64 BOOL DATETIME DOUBLE INT STRING STRUCT /;
31  
32 sub new
33 1 ▶{
Subroutine new: called CC=2 SCAR=10.5
Pod documented
34 1 my $class = shift(@_);
35 1 my $arg = shift(@_);
36 1 my $force_type = shift(@_);
37
38 1 my $val = process($arg, $force_type);
39 0 bless($val, $class);
40  
41 0 ▶ $val->_type($force_type // determine_type($arg));
Condition0%0/3
$force_type // &determine_type($arg)
l!l&&r!l&&!r
000
MC/DC0%0/2
$force_type // &determine_type($arg)
$force_type &determine_type($arg)
Truth table0%0/3
A // B
A= $force_typeB= &determine_type($arg)
ABresult
1X1
011
000
42 0 return $val;
43 }
44  
45 sub process
46 1 ▶{
Subroutine process: called CC=4 SCAR=20.5
Pod undocumented
47 1 my ($arg, $force) = (shift(@_), shift(@_));
48
49 1 my $val = __PACKAGE__->SUPER::new(+VALUE);
50
51 1 ▶ given($force // determine_type($arg))
Condition33%1/3
$force // &determine_type($arg)
l!l&&r!l&&!r
010
MC/DC0%0/2
$force // &determine_type($arg)
$force &determine_type($arg)
Truth table33%1/3
A // B
A= $forceB= &determine_type($arg)
ABresult
1X1
011
000
52 {
53 when(+ARRAY)
54 1 {
55 0 my $data = $val->appendChild(+ARRAY)->appendChild(+DATA);
56
57 0 foreach(@$arg)
58 {
59 0 $data->appendChild(process($_));
60 }
61 }
62 when(+STRUCT)
63 1 {
64 0 my $struct = $val->appendChild(+STRUCT);
65  
66 0 ▶ while(my ($key, $val) = each %$arg)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$arg) 0 0 0
67 {
68 0 my $member = $struct->appendChild(+MEMBER);
69 0 $member->appendChild(+NAME)->appendText($key);
70 0 $member->appendChild(process($val));
71 }
72 }
73 default
74 1 {
75 1 $val->appendChild($_)->appendText($arg);
76 }
77 }
78  
79 0 return $val;
80 }
81  
82 sub value()
83 0 ▶{
Subroutine value: not called CC=5 SCAR=34.0
Pod documented
84 0 my ($self, $arg, $force_type) = (shift(@_), shift(@_), shift(@_));
85
86 0 ▶ if(defined($arg))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) { } 0 0 0
87 {
88 0 $self->removeChild($self->firstChild());
89 0 ▶ my $type = $force_type // determine_type($arg);
Condition0%0/3
$force_type // &determine_type($arg)
l!l&&r!l&&!r
000
MC/DC0%0/2
$force_type // &determine_type($arg)
$force_type &determine_type($arg)
Truth table0%0/3
A // B
A= $force_typeB= &determine_type($arg)
ABresult
1X1
011
000
90 0 $self->appendChild($type)->appendText($arg);
91 0 $self->_type($type);
92 }
93 else
94 {
95 0 my $content = $self->findvalue('child::text()');
96 0 ▶ if(defined($content) && length($content))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and length $content) { } 0 0 0
Condition0%0/3
defined $content and length $content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table0%0/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
97 {
98 0 return $content;
99 }
100 else
101 {
102 0 return node_to_value($self);
103 }
104 }
105 }
106  
107 sub node_to_value
108 0 ▶{
Subroutine node_to_value: not called CC=5 SCAR=34.0
Pod documented
109 0 my $node = shift(@_);
110
111 0 my $content = $node->findvalue('child::text()');
112 0 ▶ return $content if defined($content) && length($content);
Branch: 1 branch
BranchTrueFalseTotal
if defined $content and length $content 0 0 0
Condition0%0/3
defined $content and length $content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table0%0/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
113  
114 0 my $val = $node->firstChild();
115 0 given($val->nodeName())
116 {
117 when(+STRUCT)
118 0 {
119 0 my $struct = {};
120 0 foreach($val->findnodes('child::member'))
121 {
122 0 $struct->{$_->findvalue('child::name/child::text()')} =
123 node_to_value(($_->findnodes('child::value'))[0]);
124 }
125  
126 0 return $struct;
127 }
128 when(+ARRAY)
129 0 {
130 0 my $array = [];
131  
132 0 foreach($val->findnodes('child::data/child::value'))
133 {
134 0 push(@$array, node_to_value($_));
135 }
136  
137 0 return $array;
138 }
139 default
140 0 {
141 0 return $val->findvalue('child::text()');
142 }
143 }
144 }
145  
146 sub type()
147 0 ▶{
Subroutine type: not called CC=4 SCAR=30.0
Pod documented
148 0 my $self = shift(@_);
149 0 ▶ if(!defined($self->_type()))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->_type) { } 0 0 0
150 {
151 0 my $content = $self->findvalue('child::text()');
152
153 0 ▶ if(defined($content) && length($content))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and length $content) 0 0 0
Condition0%0/3
defined $content and length $content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table0%0/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
154 {
155 # string
156 0 $self->_type(+STRING);
157 0 return +STRING;
158 }
159
160 0 my $determined = determine_type($self->value());
161 0 $self->_type($determined);
162 0 return $determined;
163 }
164 else
165 {
166 0 return $self->_type();
167 }
168 }
169  
170 sub _type()
171 0 ▶{
Subroutine _type: not called CC=2 SCAR=17.9
172 0 my ($self, $arg) = (shift(@_), shift(@_));
173 0 fieldhash state %type;
174  
175 0 ▶ if(defined($arg))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) { } 0 0 0
176 {
177 0 $type{$self} = $arg;
178 }
179 else
180 {
181 0 return $type{$self};
182 }
183 }
184  
185 sub determine_type($)
186 1 ▶{
Subroutine determine_type: called CC=5 SCAR=25.5
Pod documented
187 1 my $arg = shift(@_);
188  
189 1 given($arg)
190 {
191 when(m@^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$@)
192 1 {
193 0 return +BASE64;
194 }
195 when(/^(?:1|0){1}$|^true$|^false$/i)
196 1 {
197 0 return +BOOL;
198 }
199 }
200  
201 1 ▶ if(looks_like_number($arg))
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($arg)) 1 0 1
202 {
203 1 ▶ if($arg =~ /\.{1}/)
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\.{1}/) { } 0 1 1
204 {
205 0 return +DOUBLE;
206 }
207 else
208 {
209 1 return +INT;
210 }
211 }
212  
213 0 ▶ given(reftype($arg) // '')
Condition0%0/2
builtin::reftype($arg) // ""
l!l
00
MC/DC0%0/1
builtin::reftype($arg) // ""
builtin::reftype($arg)
Truth table0%0/2
A
A= builtin::reftype($arg)
Aresult
11
00
214 {
215 when('ARRAY')
216 0 {
217 0 return +ARRAY;
218 }
219 when('HASH')
220 0 {
221 0 return +STRUCT;
222 }
223 default
224 0 {
225 0 state $iso = "$RE{'time'}{'iso'}";
226 0 ▶ if($arg =~ /$iso/)
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /$iso/) 0 0 0
227 {
228 0 return +DATETIME;
229 }
230 0 return +STRING;
231 }
232 }
233 }
234  
235 =pod
236  
237 =head1 NAME
238  
239 POE::Filter::XML::RPC::Value - Represents XMLRPC value types
240  
241 =head1 SYNOPSIS
242  
243 use 5.010;
244 use POE::Filter::XML::RPC::Value;
245  
246 my $val1 = POE::Filter::XML::RPC::Value->new([qw/one two three/]);
247 my $val2 = POE::Filter::XML::RPC::Value->new('1A2B3C==');
248 my $val3 = POE::Filter::XML::RPC::Value->new(1);
249 my $val4 = POE::Filter::XML::RPC::Value->new('19980717T14:08:55');
250 my $val5 = POE::Filter::XML::RPC::Value->new(1.00);
251 my $val6 = POE::Filter::XML::RPC::Value->new(42);
252 my $val7 = POE::Filter::XML::RPC::Value->new('some text');
253 my $val8 = POE::Filter::XML::RPC::Value->new({'key' => 'val'});
254 my $val9 = POE::Filter::XML::RPC::Value->new(1234, +STRING);
255  
256 say $val1->type(); # array
257 say $val2->type(); # base64
258 say $val3->type(); # bool
259 say $val4->type(); # dateTime.iso8601
260 say $val5->type(); # double
261 say $val6->type(); # int
262 say $val7->type(); # string
263 say $val8->type(); # struct
264 say $val9->type(); # string
265  
266 =head1 DESCRIPTION
267  
268 POE::Filter::XML::RPC::Value does most of the automagical marshalling that is
269 expected when dealing with XMLRPC value types. Structs are converted to hashes.
270 Arrays are converted to arrays, etc. And it works both ways. So if passed a
271 complex, nested Perl data structure, it will Do The Right Thing.
272  
273 =head1 PUBLIC METHODS
274  
275 =over 4
276  
277 =item new()
278  
279 new() accepts a scalar, and an optional type argument to use to construct the
280 the value. See EXPORTED CONSTANTS for acceptable types.
281  
282 The scalar provided can contain a string, hash or array reference, or may be a
283 numerical value. Scalar::Util is put to good use to determine what kind of
284 value was passed, and some good old fashion regular expression magic thrown at
285 it to see if it is a ISO 8601 datetime, or perhaps BASE64 encoded data.
286  
287 If the type determination turns out wrong for whatever reason, a type argument
288 can also be supplied to force a particular type.
289  
290 =item type()
291  
292 type() returns what type of value is represented. For values received from some
293 where else, it will spelunk into the data and determine the type using the same
294 heuristics used for construction. Will be one of the EXPORTED CONSTANTS
295  
296 =item value()
297  
298 value() returns the data properly marshalled into whatever Perl type is valid.
299 Arrays and Structs will be marshalled into their Perl equivalent and returned
300 as a reference to that type, while all other types will be return as a scalar.
301  
302 value() can also take a new value to replace the old one. It can even be of a
303 different type. And again if the heuristics for your data don't do the right
304 thing, you can also provide a second argument of what type the data should be.
305  
306 =back
307  
308 =head1 PRIVATE METHODS
309  
310 =over 4
311  
312 =item _type()
313  
314 _type() stores the cached type of the current Value with examining the content
315 to determine if that still holds true. Use with care.
316  
317 =back
318  
319 =head1 PROTECTED FUNCTIONS
320  
321 These are not exported or available for export at all.
322  
323 =over 4
324  
325 =item determine_type
326  
327 This function contains the logic behind the type guessing heuristic. Simply
328 supply whatever scalar you want to it and it will return one of the EXPORTED
329 CONSTANTS.
330  
331 =item node_to_value
332  
333 This function takes a POE::Filter::XML::Node of the following structure:
334  
335 <value>
336 <!-- some other stuff in here, could be <array/>,<struct/>, etc -->
337 </value>
338  
339 then marshals and returns that data to you.
340  
341 =back
342  
343 =head1 EXPORTED CONSTANTS
344  
345 Here are the exported constants and their values. Note that the values for
346 these constants are the same as valid tag names for value types in XMLRPC.
347  
348 +ARRAY => 'array',
349 +BASE64 => 'base64',
350 +BOOL => 'bool',
351 +DATETIME => 'dateTime.iso8601',
352 +DOUBLE => 'double',
353 +INT => 'int',
354 +STRING => 'string',
355 +STRUCT => 'struct',
356  
357 =head1 NOTES
358  
359 Value is actually a subclass of POE::Filter::XML::Node and so all of its
360 methods, including XML::LibXML::Element's, are available for use. This could
361 ultimately be useful to avoid marshalling all of the data out of the Node and
362 instead apply an XPATH expression to target specifically what is desired deep
363 within a nested structure.
364  
365 =head1 AUTHOR
366  
367 Copyright 2009 Nicholas Perez.
368 Licensed and distributed under the GPL.
369  
370 =cut
371  
372 2 1;
« RPC/Response.pm ↑ Summary RPC/Request.pm »