File Coverage

blib/lib/Crypt/PKCS11/Attribute/AttributeArray.pm
Criterion Covered Total %
statement 50 50 100.0
branch 8 8 100.0
condition 9 9 100.0
subroutine 12 12 100.0
pod 8 8 100.0
total 87 87 100.0


line stmt bran cond sub pod time code
1             # Copyright (c) 2015 Jerry Lundström
2             # Copyright (c) 2015 .SE (The Internet Infrastructure Foundation)
3             # All rights reserved.
4             #
5             # Redistribution and use in source and binary forms, with or without
6             # modification, are permitted provided that the following conditions
7             # are met:
8             # 1. Redistributions of source code must retain the above copyright
9             # notice, this list of conditions and the following disclaimer.
10             # 2. Redistributions in binary form must reproduce the above copyright
11             # notice, this list of conditions and the following disclaimer in the
12             # documentation and/or other materials provided with the distribution.
13             #
14             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15             # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16             # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17             # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18             # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19             # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20             # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21             # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22             # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23             # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24             # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25              
26             package Crypt::PKCS11::Attribute::AttributeArray;
27              
28 2     2   10 use common::sense;
  2         4  
  2         14  
29 2     2   83 use Carp;
  2         4  
  2         114  
30 2     2   8 use Scalar::Util qw(blessed);
  2         2  
  2         76  
31              
32 2     2   9 use base qw(Crypt::PKCS11::Attribute);
  2         2  
  2         1291  
33              
34             sub push {
35 3     3 1 14 my ($self) = CORE::shift;
36              
37 3         8 CORE::foreach (@_) {
38 3 100 100     37 unless (blessed($_) and $_->isa('Crypt::PKCS11::Attribute')) {
39 2         330 confess 'Value to push is not a Crypt::PKCS11::Attribute object';
40             }
41             }
42 1         2 CORE::push(@{$self->{attributes}}, @_);
  1         12  
43              
44 1         5 return $self;
45             }
46              
47             sub pop {
48 1     1 1 3 return CORE::pop(@{$_[0]->{attributes}});
  1         7  
49             }
50              
51             sub shift {
52 1     1 1 3 return CORE::shift(@{$_[0]->{attributes}});
  1         7  
53             }
54              
55             sub unshift {
56 3     3 1 7 my ($self) = CORE::shift;
57              
58 3         8 CORE::foreach (@_) {
59 3 100 100     42 unless (blessed($_) and $_->isa('Crypt::PKCS11::Attribute')) {
60 2         369 confess 'Value to unshift is not a Crypt::PKCS11::Attribute object';
61             }
62             }
63 1         4 CORE::unshift(@{$self->{attributes}}, @_);
  1         4  
64              
65 1         5 return $self;
66             }
67              
68             sub foreach {
69 2     2 1 305 my ($self, $cb) = @_;
70              
71 2 100       12 unless (ref($cb) eq 'CODE') {
72 1         169 confess '$cb argument is not CODE';
73             }
74 1         3 CORE::foreach (@{$self->{attributes}}) {
  1         5  
75 1         5 $cb->($_);
76             }
77              
78 1         8 return $self;
79             }
80              
81             sub toArray {
82 2     2 1 5 my ($self) = @_;
83 2         4 my @array;
84              
85 2         3 CORE::foreach (@{$self->{attributes}}) {
  2         9  
86 2         30 CORE::push(@array, { type => $_->type, pValue => $_->pValue });
87             }
88              
89 2         16 return \@array;
90             }
91              
92             sub set {
93 3     3 1 6 my $self = CORE::shift;
94              
95 3         7 foreach (@_) {
96 3 100 100     40 unless (blessed($_) and $_->isa('Crypt::PKCS11::Attribute')) {
97 2         336 confess 'Value to set is not a Crypt::PKCS11::Attribute object';
98             }
99             }
100              
101 1         5 $self->{attributes} = [ @_ ];
102              
103 1         5 return $self;
104             }
105              
106             sub pValue {
107 1     1 1 9 return $_[0]->toArray;
108             }
109              
110             1;
111              
112             __END__