blib/lib/GDPR/IAB/TCFv2/Validator/Result.pm

filter: Statementstmt 100% 31 / 31 Branchbran 66.6% 8 / 12 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 90.6% 58 / 64 CC 9 SCAR 8.5
CC
9
Cov
89%
__ANON__
4.0
new
3.0
failures
2.0
CRAP 2.3
« blib/lib/GDPR/IAB/TCFv2/Validator/Reason.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/Validator/Failure.pm »
1 package GDPR::IAB::TCFv2::Validator::Result 0.520;
2  
3 25 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 use overload
7 43 ▶ bool => sub { $_[0]->{ok} },
Subroutine __ANON__: called CC=1 SCAR=0.0
8 '""' => sub {
9 12 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
10 12 ▶ return '' if $self->{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ok"} 2 10 12
11  
12 # Use $ORS (Output Record Separator) or newline as fallback
13 10 ▶ my $sep = defined($\) ? $\ : "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $\ ? : 1 9 10
14 10 ▶ return join($sep, map { $_->message } @{$self->{failures} || []});
Branch: 1 branch
BranchTrueFalseTotal
$self->{"failures"} or [] 0 10 10
15 25 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub new {
18 9281 ▶ my ($klass, %args) = @_;
Subroutine new: called CC=3 SCAR=11.1
Pod documented
19  
20 9281 ▶ my $self = {ok => $args{ok} || 0, failures => $args{failures} || [],};
Condition75%3/4
$args{'ok'} || 0
l!l
69832298
$args{'failures'} || []
l!l
92810
MC/DC100%1/1
$args{'ok'} || 0
$args{'ok'}
MC/DC0%0/1
$args{'failures'} || []
$args{'failures'}
Truth table100%2/2
A
A= $args{'ok'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'failures'}
Aresult
11
00
21  
22 9281 return bless $self, $klass;
23 }
24  
25 9251 ▶sub is_valid { $_[0]->{ok} }
Subroutine is_valid: called CC=1 SCAR=0.0
Pod documented
26  
27 sub failures {
28 9 ▶ my $self = shift;
Subroutine failures: called CC=2 SCAR=7.1
Pod documented
29 9 ▶ return @{$self->{failures} || []};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"failures"} or [] 0 9 9
30 }
31  
32 sub reason_codes {
33 2 ▶ my $self = shift;
Subroutine reason_codes: called CC=2 SCAR=7.0
Pod documented
34 2 ▶ return map { $_->code } @{$self->{failures} || []};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"failures"} or [] 0 2 2
35 }
36  
37 sub reasons {
38 9233 ▶ my $self = shift;
Subroutine reasons: called CC=2 SCAR=7.0
Pod documented
39 9233 ▶ return map { $_->message } @{$self->{failures} || []};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"failures"} or [] 0 9233 9233
40 }
41  
42 25 1;
43 __END__
44  
45 =encoding utf8
46  
47 =head1 NAME
48  
49 GDPR::IAB::TCFv2::Validator::Result - outcome object returned by L<GDPR::IAB::TCFv2::Validator>
50  
51 =head1 SYNOPSIS
52  
53 my ($validator, $tc_string) = ('...', '...');
54 my $result = $validator->validate($tc_string);
55  
56 if ($result) {
57 # Validation passed.
58 }
59 else {
60 warn "$result\n"; # one reason per line by default
61 for my $reason ( $result->reasons ) {
62 warn $reason;
63 }
64 }
65  
66 # Use Perl's output record separator to control how reasons join:
67 {
68 local $\ = " | ";
69 print "$result"; # "reason1 | reason2"
70 }
71  
72 =head1 DESCRIPTION
73  
74 A small immutable carrier for the outcome of a
75 L<GDPR::IAB::TCFv2::Validator/validate> or
76 L<GDPR::IAB::TCFv2::Validator/validate_all> run. It
77 overloads boolean and string contexts so it drops into typical
78 error-handling idioms without an explicit accessor call.
79  
80 =head1 OVERLOADS
81  
82 =head2 Boolean
83  
84 if ($result) { ... }
85 if (!$result) { ... }
86  
87 True when validation passed (no rules failed); false otherwise.
88  
89 =head2 Stringification
90  
91 print "$result\n";
92  
93 Returns the empty string for a passing result. For a failing result,
94 returns the failure reasons joined by Perl's output record separator
95 (C<$\>); if C<$\> is undefined, reasons are joined by C<"\n">.
96  
97 This means:
98  
99 print "$result\n"; # one reason per line
100 local $\ = " | ";
101 print "$result\n"; # "reason1 | reason2"
102  
103 aligns naturally with the way C<print> would have laid out the reasons
104 if you had iterated and printed them yourself.
105  
106 =head1 METHODS
107  
108 =head2 is_valid
109  
110 my $ok = $result->is_valid;
111  
112 Returns truthy for a passing result, falsy otherwise. Equivalent to the
113 boolean overload but available as an explicit method for callers that
114 prefer it.
115  
116 =head2 reasons
117  
118 my @reasons = $result->reasons;
119  
120 Returns the list of human-readable failure reason strings. Empty for
121 a passing result. Equivalent to C<map { $_-E<gt>message } $result-E<gt>failures>.
122  
123 =head2 failures
124  
125 my @failures = $result->failures;
126  
127 Returns the list of structured L<GDPR::IAB::TCFv2::Validator::Failure>
128 objects. Each failure carries a stable integer C<code>, the
129 human-readable C<message>, and any structured context (purpose,
130 vendor, restriction type, CMP). Use this when you need to react
131 programmatically to specific failure types.
132  
133 use GDPR::IAB::TCFv2::Validator::Reason qw<:all>;
134  
135 for my $f ( $result->failures ) {
136 if ( $f->code == ReasonVendorNotAllowedConsent ) {
137 handle_consent_gap( $f->purpose_id );
138 }
139 }
140  
141 =head2 reason_codes
142  
143 my @codes = $result->reason_codes;
144  
145 Returns the list of integer reason codes. Equivalent to
146 C<map { $_-E<gt>code } $result-E<gt>failures>; useful when only the
147 codes are needed (e.g. for assertions or counters).
148  
149 =head1 CONSTRUCTOR
150  
151 =head2 new
152  
153 Internal — construct via L<GDPR::IAB::TCFv2::Validator/validate> rather
154 than directly.
155  
156 =head1 SEE ALSO
157  
158 L<GDPR::IAB::TCFv2::Validator>,
159 L<GDPR::IAB::TCFv2::Validator::Failure>,
160 L<GDPR::IAB::TCFv2::Validator::Reason>.
161  
162 =cut
« blib/lib/GDPR/IAB/TCFv2/Validator/Reason.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/Validator/Failure.pm »