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