line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Braintree::Util; |
2
|
|
|
|
|
|
|
$WebService::Braintree::Util::VERSION = '0.93'; |
3
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
7
|
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
103
|
|
6
|
1
|
|
|
1
|
|
407
|
use URI::Query; |
|
1
|
|
|
|
|
8957
|
|
|
1
|
|
|
|
|
94
|
|
7
|
1
|
|
|
1
|
|
16
|
use Exporter qw(import); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
689
|
|
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
our @EXPORT = qw(to_instance_array flatten is_hashref is_arrayref hash_to_query_string equal_arrays difference_arrays validate_id contains); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub flatten { |
12
|
0
|
|
|
0
|
0
|
|
my($hash, $namespace) = @_; |
13
|
0
|
|
|
|
|
|
my %flat_hash = (); |
14
|
0
|
|
|
|
|
|
while (my ($key, $value) = each(%$hash)) { |
15
|
0
|
0
|
|
|
|
|
if (is_hashref($value)) { |
16
|
0
|
|
|
|
|
|
my $sub_entries = flatten($value, add_namespace($key, $namespace)); |
17
|
0
|
|
|
|
|
|
%flat_hash = (%flat_hash, %$sub_entries); |
18
|
|
|
|
|
|
|
} else { |
19
|
0
|
|
|
|
|
|
$flat_hash{add_namespace($key, $namespace)} = $value; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
} |
22
|
0
|
|
|
|
|
|
return \%flat_hash; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub add_namespace { |
26
|
0
|
|
|
0
|
0
|
|
my ($key, $namespace) = @_; |
27
|
0
|
0
|
|
|
|
|
return $key unless $namespace; |
28
|
0
|
|
|
|
|
|
return "${namespace}[${key}]"; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub is_hashref { |
32
|
0
|
|
|
0
|
0
|
|
ref(shift) eq 'HASH'; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub is_arrayref { |
36
|
0
|
|
|
0
|
0
|
|
ref(shift) eq 'ARRAY'; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub equal_arrays { |
40
|
0
|
|
|
0
|
0
|
|
my ($first, $second) = @_; |
41
|
0
|
0
|
|
|
|
|
return 0 unless @$first == @$second; |
42
|
0
|
|
|
|
|
|
for (my $i = 0; $i < @$first; $i++) { |
43
|
0
|
0
|
|
|
|
|
return 0 if $first->[$i] ne $second->[$i]; |
44
|
|
|
|
|
|
|
} |
45
|
0
|
|
|
|
|
|
return 1; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub difference_arrays { |
49
|
0
|
|
|
0
|
0
|
|
my ($array1, $array2) = @_; |
50
|
0
|
|
|
|
|
|
my @diff; |
51
|
0
|
|
|
|
|
|
foreach my $element (@$array1) { |
52
|
0
|
0
|
|
|
|
|
push(@diff, $element) unless contains($element, $array2); |
53
|
|
|
|
|
|
|
} |
54
|
0
|
|
|
|
|
|
return \@diff; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub hash_to_query_string { |
58
|
0
|
|
|
0
|
0
|
|
my $query = URI::Query -> new(flatten(shift)); |
59
|
0
|
|
|
|
|
|
return $query->stringify(); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub to_instance_array { |
63
|
0
|
|
|
0
|
0
|
|
my ($attrs, $class) = @_; |
64
|
0
|
|
|
|
|
|
my @result = (); |
65
|
0
|
0
|
|
|
|
|
if (ref $attrs ne "ARRAY") { |
66
|
0
|
|
|
|
|
|
push(@result, $class->new($attrs)); |
67
|
|
|
|
|
|
|
} else { |
68
|
0
|
|
|
|
|
|
for (@$attrs) { |
69
|
0
|
|
|
|
|
|
push(@result, $class->new($_)); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
return \@result; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
sub trim { |
75
|
0
|
|
|
0
|
0
|
|
my $string = shift; |
76
|
0
|
|
|
|
|
|
$string =~ s/^\s+//; |
77
|
0
|
|
|
|
|
|
$string =~ s/\s+$//; |
78
|
0
|
|
|
|
|
|
return $string; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub validate_id { |
82
|
0
|
|
|
0
|
0
|
|
my $id = shift; |
83
|
0
|
0
|
0
|
|
|
|
return 0 if(!defined($id) || trim($id) eq ""); |
84
|
0
|
|
|
|
|
|
return 1; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub contains { |
88
|
0
|
|
|
0
|
0
|
|
my ($element, $array) = @_; |
89
|
0
|
|
|
|
|
|
for (@$array) { |
90
|
0
|
0
|
|
|
|
|
return 1 if $_ eq $element; |
91
|
|
|
|
|
|
|
} |
92
|
0
|
|
|
|
|
|
return 0; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
1; |