File Coverage

blib/lib/Protocol/XMLRPC/ValueFactory.pm
Criterion Covered Total %
statement 48 48 100.0
branch 21 22 95.4
condition 39 42 92.8
subroutine 12 12 100.0
pod 1 1 100.0
total 121 125 96.8


line stmt bran cond sub pod time code
1             package Protocol::XMLRPC::ValueFactory;
2              
3 8     8   24521 use strict;
  8         16  
  8         279  
4 8     8   41 use warnings;
  8         18  
  8         185  
5              
6 8     8   49 use B;
  8         15  
  8         479  
7 8     8   64 use Scalar::Util qw(blessed);
  8         12  
  8         1850  
8              
9 8     8   1744 use Protocol::XMLRPC::Value::Double;
  8         511  
  8         210  
10 8     8   721 use Protocol::XMLRPC::Value::String;
  8         59  
  8         350  
11 8     8   571 use Protocol::XMLRPC::Value::Integer;
  8         13  
  8         296  
12 8     8   1223 use Protocol::XMLRPC::Value::Array;
  8         21  
  8         182  
13 8     8   4964 use Protocol::XMLRPC::Value::Boolean;
  8         19  
  8         349  
14 8     8   5311 use Protocol::XMLRPC::Value::DateTime;
  8         22  
  8         292  
15 8     8   4464 use Protocol::XMLRPC::Value::Struct;
  8         22  
  8         3665  
16              
17             sub build {
18 38     38 1 7493 my $class = shift;
19              
20 38 100       87 return unless @_;
21              
22 37         50 my ($type, $value) = @_;
23 37 100       97 ($value, $type) = ($type, '') unless defined $value;
24              
25 37 100       162 return $value if blessed($value);
26              
27             # From JSON::PP
28 22         140 my $flags = B::svref_2object(\$value)->FLAGS;
29 22 50       89 my $is_number = $flags & (B::SVp_IOK | B::SVp_NOK)
    100          
30             and !($flags & B::SVp_POK) ? 1 : 0;
31              
32 22 100 100     427 if (($type && $type eq 'array') || ref($value) eq 'ARRAY') {
    100 100        
    100 100        
    100 100        
    100 100        
    100 100        
      66        
      100        
      66        
      66        
      100        
      100        
      100        
      100        
33 3         16 return Protocol::XMLRPC::Value::Array->new($value);
34             }
35             elsif (($type && $type eq 'struct') || ref($value) eq 'HASH') {
36 3         27 return Protocol::XMLRPC::Value::Struct->new($value);
37             }
38             elsif (($type && $type eq 'int') || ($is_number && $value =~ m/^(?:\+|-)?\d+$/)) {
39 3         23 return Protocol::XMLRPC::Value::Integer->new($value);
40             }
41             elsif (($type && $type eq 'double') || ($is_number && $value =~ m/^(?:\+|-)?\d+\.\d+$/)) {
42 2         14 return Protocol::XMLRPC::Value::Double->new($value);
43             }
44             elsif (($type && $type eq 'boolean') || ref($value) eq 'SCALAR') {
45 3         18 return Protocol::XMLRPC::Value::Boolean->new($value);
46             }
47             elsif (($type && $type eq 'datetime')
48             || $value =~ m/^(\d\d\d\d)(\d\d)(\d\d)T(\d\d):(\d\d):(\d\d)$/)
49             {
50 2         12 return Protocol::XMLRPC::Value::DateTime->parse($value);
51             }
52              
53 6         27 return Protocol::XMLRPC::Value::String->new($value);
54             }
55              
56             1;
57             __END__