File Coverage

blib/lib/OIDC/Client/Utils.pm
Criterion Covered Total %
statement 44 44 100.0
branch 16 16 100.0
condition 2 2 100.0
subroutine 9 9 100.0
pod 4 4 100.0
total 75 75 100.0


line stmt bran cond sub pod time code
1             package OIDC::Client::Utils;
2              
3 9     9   482081 use utf8;
  9         300  
  9         94  
4 9     9   982 use Moose;
  9         447837  
  9         76  
5 9     9   75875 use Moose::Exporter;
  9         18  
  9         58  
6 9     9   1780 use MooseX::Params::Validate;
  9         145403  
  9         85  
7 9     9   5461 use Carp qw(croak);
  9         21  
  9         7798  
8              
9             =encoding utf8
10              
11             =head1 NAME
12              
13             OIDC::Client::Utils - Utility functions
14              
15             =head1 DESCRIPTION
16              
17             Exports utility functions.
18              
19             =cut
20              
21             Moose::Exporter->setup_import_methods(as_is => [qw/get_values_from_space_delimited_string
22             reach_data
23             affect_data
24             delete_data/]);
25              
26              
27             =head1 FUNCTIONS
28              
29             =head2 get_values_from_space_delimited_string( $value )
30              
31             Returns the values (arrayref) from a space-delimited string value.
32              
33             =cut
34              
35             sub get_values_from_space_delimited_string {
36 23     23 1 14448 my ($str) = pos_validated_list(\@_, { isa => 'Str', optional => 0 });
37 23         4952 return [ grep { $_ ne '' } split(/\s+/, $str) ];
  45         219  
38             }
39              
40              
41             =head2 reach_data( $data_tree, \@path, $optional )
42              
43             Simplified data diver. No support for arrayref nodes.
44              
45             Tries to find a node under root $data_tree, walking down the tree and choosing subnodes
46             according to values given in $path (which should be an arrayref of scalar values).
47              
48             While walking down the tree, if a key doesn't exist, undef is returned if the $optional
49             parameter is true (default value), otherwise an exception is thrown if the $optional parameter
50             is false.
51              
52             No autovivification is performed by this function.
53              
54             =cut
55              
56             sub reach_data {
57 101     101 1 16350 my ($data_tree, $path, $optional) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 },
58             { isa => 'ArrayRef[Str]', optional => 0 },
59             { isa => 'Bool', default => 1 });
60 101         42027 foreach my $key (@$path) {
61 447 100       948 ref $data_tree eq 'HASH' or croak("OIDC: not a hashref to reach the value of the '$key' key");
62 446 100       894 if (exists $data_tree->{$key}) {
    100          
63 432         725 $data_tree = $data_tree->{$key};
64             }
65             elsif ($optional) {
66 12         66 return;
67             }
68             else {
69 2         35 croak("OIDC: the '$key' key is not present");
70             }
71             }
72              
73 86         373 return $data_tree;
74             }
75              
76              
77             =head2 affect_data( $data_tree, \@path, $value )
78              
79             Walks down the $data_tree and sets the $value to the subnode according to values given
80             in $path (which should be an arrayref of scalar values).
81              
82             Arrayref nodes are not supported.
83              
84             Autovivification can be performed by this function.
85              
86             =cut
87              
88             sub affect_data {
89 69     69 1 16245 my ($data_tree, $path, $value) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 },
90             { isa => 'ArrayRef[Str]', optional => 0 },
91             { optional => 0 });
92 69 100       22069 @$path >= 1 or croak(q{OIDC: to affect data, at least one value must be provided in the 'path' arrayref});
93              
94 68         251 my @path_to_iterate = @$path;
95 68         142 my $key_to_assign_value = pop @path_to_iterate;
96              
97 68         181 foreach my $key (@path_to_iterate) {
98 305   100     810 $data_tree = ($data_tree->{$key} //= {});
99 305 100       628 ref $data_tree eq 'HASH' or croak("OIDC: the value of the '$key' key is not a hash reference");
100             }
101 67         157 $data_tree->{$key_to_assign_value} = $value;
102              
103 67         313 return;
104             }
105              
106              
107             =head2 delete_data( $data_tree, \@path )
108              
109             Walks down the $data_tree, deletes the key of the last subnode and returns its value
110             according to values given in $path (which should be an arrayref of scalar values).
111              
112             Arrayref nodes are not supported.
113              
114             While walking down the tree, if a key doesn't exist, undef is returned.
115              
116             No autovivification is performed by this function.
117              
118             =cut
119              
120             sub delete_data {
121 15     15 1 26768 my ($data_tree, $path) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 },
122             { isa => 'ArrayRef[Str]', optional => 0 });
123 15 100       8214 @$path >= 1 or croak(q{OIDC: to delete data, at least one value must be provided in the 'path' arrayref});
124              
125 14         39 my @path_to_iterate = @$path;
126 14         28 my $key_to_delete = pop @path_to_iterate;
127              
128 14         30 foreach my $key (@path_to_iterate) {
129 15 100       55 return unless defined $data_tree->{$key};
130 12         21 $data_tree = $data_tree->{$key};
131 12 100       39 ref $data_tree eq 'HASH' or croak("OIDC: the value of the '$key' key is not a hash reference");
132             }
133              
134 10         44 return delete $data_tree->{$key_to_delete};
135             }
136              
137              
138             1;