blib/lib/GDPR/IAB/TCFv2/BitField.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 73.6% 28 / 38 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 88.6% 117 / 132 CC 21 SCAR 19.2
CC
21
Cov
89%
TO_JSON
11.3
Parse
8.5
contains
3.0
CRAP 6.8
« blib/lib/GDPR/IAB/TCFv2/BitUtils.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm »
1 package GDPR::IAB::TCFv2::BitField 0.512;
2 72 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 72 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 72 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 72 ▶use GDPR::IAB::TCFv2::BitUtils qw<is_set>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use Carp qw<croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub Parse {
11 20950 ▶ my ($klass, %args) = @_;
Subroutine Parse: called CC=8 SCAR=21.4
Pod undocumented
12  
13 20950 ▶ croak "missing 'data'" unless defined $args{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data"} 0 20950 20950
14 20950 ▶ croak "missing 'data_size'" unless defined $args{data_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data_size"} 0 20950 20950
15 20950 ▶ croak "missing 'max_id'" unless defined $args{max_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"max_id"} 0 20950 20950
16  
17 20950 ▶ croak "missing 'options'" unless defined $args{options};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"} 0 20950 20950
18 20950 ▶ croak "missing 'options.json'" unless defined $args{options}->{json};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"}{"json"} 0 20950 20950
19  
20 20950 my $data = $args{data};
21 20950 my $data_size = $args{data_size};
22 20950 ▶ my $offset = $args{offset} // 0;
Condition50%1/2
$args{"offset"} // 0
l!l
209500
MC/DC0%0/1
$args{"offset"} // 0
$args{"offset"}
Truth table50%1/2
A
A= $args{"offset"}
Aresult
11
00
23 20950 my $max_id = $args{max_id};
24 20950 my $options = $args{options};
25  
26 20950 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $offset + $max_id > $data_size << 3 2 20948 20950
27 2 "a BitField for $max_id bits requires a consent string of at least @{[ $offset + $max_id ]} bits. This consent string only had @{[ $data_size << 3 ]} bits"
28 if $offset + $max_id > ($data_size << 3);
29  
30 20948 my $self = {data => $data, offset => $offset, max_id => $max_id, options => $options,};
31  
32 20948 bless $self, $klass;
33  
34 20948 return ($self, $offset + $max_id);
35 }
36  
37 sub max_id {
38 5 ▶ my $self = shift;
Subroutine max_id: called CC=1 SCAR=0.0
Pod documented
39  
40 5 return $self->{max_id};
41 }
42  
43 sub contains {
44 7712 ▶ my ($self, $id) = @_;
Subroutine contains: called CC=3 SCAR=11.0
Pod documented
45  
46 7712 ▶ croak "invalid vendor id $id: must be positive integer bigger than 0" if $id < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $id < 1 0 7712 7712
47  
48 7712 ▶ return if $id > $self->{max_id};
Branch: 1 branch
BranchTrueFalseTotal
if $id > $self->{"max_id"} 1147 6565 7712
49  
50 6565 return is_set($self->{data}, $self->{offset} + $id - 1);
51 }
52  
53 sub all {
54 30 ▶ my $self = shift;
Subroutine all: called CC=2 SCAR=6.9
Pod documented
55  
56 30 my $bitstring = unpack("B*", substr($self->{data}, $self->{offset} >> 3));
57 30 $bitstring = substr($bitstring, $self->{offset} & 7, $self->{max_id});
58  
59 30 my @set_bits;
60 30 ▶ while ($bitstring =~ /1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring =~ /1/dg) 732 30 762
61 732 push @set_bits, pos($bitstring);
62 }
63  
64 30 return \@set_bits;
65 }
66  
67 sub TO_JSON {
68 1949 ▶ my ($self, $filter_id) = @_;
Subroutine TO_JSON: called CC=11 SCAR=24.2
Pod documented
69  
70 1949 ▶ if (defined $filter_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_id) 20 1929 1949
71 20 my $val = $self->contains($filter_id);
72  
73 20 ▶ if (!!$self->{options}->{json}->{compact}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"compact"}) 0 20 20
74 0 ▶ return $val ? [$filter_id] : [];
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
75 }
76  
77 20 my ($false, $true) = @{$self->{options}->{json}->{boolean_values}};
78 20 ▶ my $bool_val = $val ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 14 6 20
79  
80 20 ▶ if (!!$self->{options}->{json}->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"verbose"}) 0 20 20
81 0 return {$filter_id => $bool_val};
82 }
83  
84 20 ▶ return $val ? {$filter_id => $true} : {};
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 14 6 20
85 }
86  
87 1929 ▶ if (!!$self->{options}->{json}->{compact}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"compact"}) 30 1899 1929
88 30 return $self->all;
89 }
90  
91 1899 my $bitstring = unpack("B*", substr($self->{data}, $self->{offset} >> 3));
92 1899 $bitstring = substr($bitstring, $self->{offset} & 7, $self->{max_id});
93  
94 1899 my ($false, $true) = @{$self->{options}->{json}->{boolean_values}};
95  
96 1899 ▶ if (!!$self->{options}->{json}->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"verbose"}) 2 1897 1899
97 2 my %map = map { $_ => $false } 1 .. $self->{max_id};
98 2 ▶ while ($bitstring =~ /1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring =~ /1/dg) 68 2 70
99 68 $map{pos($bitstring)} = $true;
100 }
101 2 return \%map;
102 }
103  
104 1897 my %map;
105 1897 ▶ while ($bitstring =~ /1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring =~ /1/dg) 978402 1897 980299
106 978402 $map{pos($bitstring)} = $true;
107 }
108 1897 return \%map;
109 }
110  
111 72 1;
112 __END__
113  
114 =head1 NAME
115  
116 GDPR::IAB::TCFv2::BitField - TCF v2.3 bitfield parser
117  
118 =head1 SYNOPSIS
119  
120 use GDPR::IAB::TCFv2::BitField;
121 my $data = '...'; # raw binary data
122
123 my $max_id_consent = 100;
124  
125 my ($bit_field) = GDPR::IAB::TCFv2::BitField->Parse(
126 data => $data,
127 data_size => length($data),
128 max_id => $max_id_consent,
129 options => { json => {} },
130 );
131  
132 if ($bit_field->contains(284)) {
133 # ...
134 }
135  
136 =head1 CONSTRUCTOR
137  
138 Constructor C<Parse> receives a hash of 5 parameters:
139  
140 =over
141  
142 =item *
143  
144 Key C<data> is the binary data
145  
146 =item *
147  
148 Key C<data_size> is the original binary data size
149  
150 =item *
151  
152 Key C<offset> is the bit offset. Optional (defaults to 0).
153  
154 =item *
155  
156 Key C<max_id> is the max id (used to validate the ranges if all data is between 1 and C<max_id>)
157  
158 =item *
159  
160 Key C<options> is the L<GDPR::IAB::TCFv2> options (includes the C<json> field to modify the L</TO_JSON> method output.
161  
162 =back
163  
164 =head1 METHODS
165  
166 =head2 contains
167  
168 Return the vendor id bit status (if enable or not) from the bit field.
169 Will return false if id is bigger than max vendor id.
170  
171 my $ok = $bit_field->contains(284);
172  
173 =head2 max_id
174  
175 Returns the max vendor id.
176  
177 =head2 all
178  
179 Returns an array of all vendors mapped with the bit enabled.
180  
181 =head2 TO_JSON
182  
183 By default it returns an hashref mapping id to a boolean, that represent if the id is active or not in the bitfield.
184  
185 The json option C<verbose> controls if all ids between 1 to L</max_id> will be present on the C<json> or only the ones that are true.
186  
187 The json option C<compact> change the response, will return an arrayref of all ids active on the bitfield.
« blib/lib/GDPR/IAB/TCFv2/BitUtils.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm »