SubjectDirectoryAttributesExtension::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Certificate\Extension;
6
7
use ASN1\Type\UnspecifiedType;
8
use ASN1\Type\Constructed\Sequence;
9
use X501\ASN1\Attribute;
10
use X509\Feature\AttributeContainer;
11
12
/**
13
 * Implements 'Subject Directory Attributes' certificate extension.
14
 *
15
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.8
16
 */
17
class SubjectDirectoryAttributesExtension extends Extension implements 
18
    \Countable,
19
    \IteratorAggregate
20
{
21
    use AttributeContainer;
22
    
23
    /**
24
     * Constructor.
25
     *
26
     * @param bool $critical
27
     * @param Attribute ...$attribs One or more Attribute objects
28
     */
29 11
    public function __construct(bool $critical, Attribute ...$attribs)
30
    {
31 11
        parent::__construct(self::OID_SUBJECT_DIRECTORY_ATTRIBUTES, $critical);
32 11
        $this->_attributes = $attribs;
33 11
    }
34
    
35
    /**
36
     *
37
     * {@inheritdoc}
38
     * @return self
39
     */
40 10
    protected static function _fromDER(string $data, bool $critical): self
41
    {
42 10
        $attribs = array_map(
43 10
            function (UnspecifiedType $el) {
44 9
                return Attribute::fromASN1($el->asSequence());
45 10
            }, UnspecifiedType::fromDER($data)->asSequence()->elements());
46 10
        if (!count($attribs)) {
47 1
            throw new \UnexpectedValueException(
48 1
                "SubjectDirectoryAttributes must have at least one Attribute.");
49
        }
50 9
        return new self($critical, ...$attribs);
51
    }
52
    
53
    /**
54
     *
55
     * {@inheritdoc}
56
     * @return Sequence
57
     */
58 16
    protected function _valueASN1(): Sequence
59
    {
60 16
        if (!count($this->_attributes)) {
61 1
            throw new \LogicException("No attributes");
62
        }
63 15
        $elements = array_map(
64 15
            function (Attribute $attr) {
65 15
                return $attr->toASN1();
66 15
            }, array_values($this->_attributes));
67 15
        return new Sequence(...$elements);
68
    }
69
}
70