IetfAttrSyntax::_transcodedString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\AttributeCertificate\Attribute;
6
7
use ASN1\Element;
8
use ASN1\Type\UnspecifiedType;
9
use ASN1\Type\Constructed\Sequence;
10
use ASN1\Type\Tagged\ImplicitlyTaggedType;
11
use X501\ASN1\AttributeValue\AttributeValue;
12
use X501\MatchingRule\BinaryMatch;
13
use X509\GeneralName\GeneralNames;
14
15
/**
16
 * Base class implementing <i>IetfAttrSyntax</i> ASN.1 type used by
17
 * attribute certificate attribute values.
18
 *
19
 * @link https://tools.ietf.org/html/rfc5755#section-4.4
20
 */
21
abstract class IetfAttrSyntax extends AttributeValue implements 
22
    \Countable,
23
    \IteratorAggregate
24
{
25
    /**
26
     * Policy authority.
27
     *
28
     * @var GeneralNames|null $_policyAuthority
29
     */
30
    protected $_policyAuthority;
31
    
32
    /**
33
     * Values.
34
     *
35
     * @var IetfAttrValue[] $_values
36
     */
37
    protected $_values;
38
    
39
    /**
40
     * Constructor.
41
     * 
42
     * @param IetfAttrValue ...$values
43
     */
44 9
    public function __construct(IetfAttrValue ...$values)
45
    {
46 9
        $this->_policyAuthority = null;
47 9
        $this->_values = $values;
48 9
    }
49
    
50
    /**
51
     *
52
     * @param UnspecifiedType $el
53
     * @return self
54
     */
55 4
    public static function fromASN1(UnspecifiedType $el): self
56
    {
57 4
        $seq = $el->asSequence();
58 4
        $authority = null;
59 4
        $idx = 0;
60 4
        if ($seq->hasTagged(0)) {
61 4
            $authority = GeneralNames::fromASN1(
62 4
                $seq->getTagged(0)
63 4
                    ->asImplicit(Element::TYPE_SEQUENCE)
64 4
                    ->asSequence());
65 4
            ++$idx;
66
        }
67 4
        $values = array_map(
68 4
            function (UnspecifiedType $el) {
69 4
                return IetfAttrValue::fromASN1($el);
70 4
            },
71 4
            $seq->at($idx)
72 4
                ->asSequence()
73 4
                ->elements());
74 4
        $obj = new static(...$values);
75 4
        $obj->_policyAuthority = $authority;
76 4
        return $obj;
77
    }
78
    
79
    /**
80
     * Get self with policy authority.
81
     *
82
     * @param GeneralNames $names
83
     * @return self
84
     */
85 2
    public function withPolicyAuthority(GeneralNames $names): self
86
    {
87 2
        $obj = clone $this;
88 2
        $obj->_policyAuthority = $names;
89 2
        return $obj;
90
    }
91
    
92
    /**
93
     * Check whether policy authority is present.
94
     *
95
     * @return bool
96
     */
97 3
    public function hasPolicyAuthority(): bool
98
    {
99 3
        return isset($this->_policyAuthority);
100
    }
101
    
102
    /**
103
     * Get policy authority.
104
     *
105
     * @throws \LogicException
106
     * @return GeneralNames
107
     */
108 3
    public function policyAuthority(): GeneralNames
109
    {
110 3
        if (!$this->hasPolicyAuthority()) {
111 1
            throw new \LogicException("policyAuthority not set.");
112
        }
113 2
        return $this->_policyAuthority;
114
    }
115
    
116
    /**
117
     * Get values.
118
     *
119
     * @return IetfAttrValue[]
120
     */
121 3
    public function values(): array
122
    {
123 3
        return $this->_values;
124
    }
125
    
126
    /**
127
     * Get first value.
128
     *
129
     * @throws \LogicException
130
     * @return IetfAttrValue
131
     */
132 2
    public function first(): IetfAttrValue
133
    {
134 2
        if (!count($this->_values)) {
135 1
            throw new \LogicException("No values.");
136
        }
137 1
        return $this->_values[0];
138
    }
139
    
140
    /**
141
     *
142
     * @see \X501\ASN1\AttributeValue\AttributeValue::toASN1()
143
     * @return Sequence
144
     */
145 8
    public function toASN1(): Sequence
146
    {
147 8
        $elements = array();
148 8
        if (isset($this->_policyAuthority)) {
149 5
            $elements[] = new ImplicitlyTaggedType(0,
150 5
                $this->_policyAuthority->toASN1());
151
        }
152 8
        $values = array_map(
153 8
            function (IetfAttrValue $val) {
154 5
                return $val->toASN1();
155 8
            }, $this->_values);
156 8
        $elements[] = new Sequence(...$values);
157 8
        return new Sequence(...$elements);
158
    }
159
    
160
    /**
161
     *
162
     * @see \X501\ASN1\AttributeValue\AttributeValue::stringValue()
163
     * @return string
164
     */
165 3
    public function stringValue(): string
166
    {
167 3
        return "#" . bin2hex($this->toASN1()->toDER());
168
    }
169
    
170
    /**
171
     *
172
     * @see \X501\ASN1\AttributeValue\AttributeValue::equalityMatchingRule()
173
     * @return BinaryMatch
174
     */
175 1
    public function equalityMatchingRule(): BinaryMatch
176
    {
177 1
        return new BinaryMatch();
178
    }
179
    
180
    /**
181
     *
182
     * @see \X501\ASN1\AttributeValue\AttributeValue::rfc2253String()
183
     * @return string
184
     */
185 1
    public function rfc2253String(): string
186
    {
187 1
        return $this->stringValue();
188
    }
189
    
190
    /**
191
     *
192
     * @see \X501\ASN1\AttributeValue\AttributeValue::_transcodedString()
193
     * @return string
194
     */
195 1
    protected function _transcodedString(): string
196
    {
197 1
        return $this->stringValue();
198
    }
199
    
200
    /**
201
     * Get number of values.
202
     *
203
     * @see \Countable::count()
204
     * @return int
205
     */
206 2
    public function count(): int
207
    {
208 2
        return count($this->_values);
209
    }
210
    
211
    /**
212
     * Get iterator for values.
213
     *
214
     * @see \IteratorAggregate::getIterator()
215
     * @return \ArrayIterator
216
     */
217 1
    public function getIterator(): \ArrayIterator
218
    {
219 1
        return new \ArrayIterator($this->_values);
220
    }
221
}
222