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   13 use common::sense;
  2         3  
  2         15  
29 2     2   90 use Carp;
  2         4  
  2         121  
30 2     2   10 use Scalar::Util qw(blessed);
  2         3  
  2         77  
31              
32 2     2   9 use base qw(Crypt::PKCS11::Attribute);
  2         3  
  2         1520  
33              
34             sub push {
35 3     3 1 10 my ($self) = CORE::shift;
36              
37 3         6 CORE::foreach (@_) {
38 3 100 100     27 unless (blessed($_) and $_->isa('Crypt::PKCS11::Attribute')) {
39 2         269 confess 'Value to push is not a Crypt::PKCS11::Attribute object';
40             }
41             }
42 1         1 CORE::push(@{$self->{attributes}}, @_);
  1         9  
43              
44 1         4 return $self;
45             }
46              
47             sub pop {
48 1     1 1 2 return CORE::pop(@{$_[0]->{attributes}});
  1         6  
49             }
50              
51             sub shift {
52 1     1 1 2 return CORE::shift(@{$_[0]->{attributes}});
  1         7  
53             }
54              
55             sub unshift {
56 3     3 1 7 my ($self) = CORE::shift;
57              
58 3         5 CORE::foreach (@_) {
59 3 100 100     28 unless (blessed($_) and $_->isa('Crypt::PKCS11::Attribute')) {
60 2         202 confess 'Value to unshift is not a Crypt::PKCS11::Attribute object';
61             }
62             }
63 1         3 CORE::unshift(@{$self->{attributes}}, @_);
  1         3  
64              
65 1         3 return $self;
66             }
67              
68             sub foreach {
69 2     2 1 488 my ($self, $cb) = @_;
70              
71 2 100       9 unless (ref($cb) eq 'CODE') {
72 1         112 confess '$cb argument is not CODE';
73             }
74 1         2 CORE::foreach (@{$self->{attributes}}) {
  1         4  
75 1         3 $cb->($_);
76             }
77              
78 1         7 return $self;
79             }
80              
81             sub toArray {
82 2     2 1 4 my ($self) = @_;
83 2         3 my @array;
84              
85 2         3 CORE::foreach (@{$self->{attributes}}) {
  2         6  
86 2         23 CORE::push(@array, { type => $_->type, pValue => $_->pValue });
87             }
88              
89 2         10 return \@array;
90             }
91              
92             sub set {
93 3     3 1 4 my $self = CORE::shift;
94              
95 3         7 foreach (@_) {
96 3 100 100     32 unless (blessed($_) and $_->isa('Crypt::PKCS11::Attribute')) {
97 2         268 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 5 return $_[0]->toArray;
108             }
109              
110             1;
111              
112             __END__