Passed
Push — master ( 2f478b...56a532 )
by Tim
02:08
created

AbstractSPKIDataType::getTuples()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\Registry\ElementRegistry;
13
use SimpleSAML\XML\SerializableElementInterface;
14
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
15
16
/**
17
 * Abstract class representing the SPKIDataType.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
abstract class AbstractSPKIDataType extends AbstractDsElement
22
{
23
    /**
24
     * Initialize a SPKIData element.
25
     *
26
     * @param array{array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}} $tuples
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{array{\SimpleSAML\...ElementInterface|null}} at position 2 could not be parsed: Expected ':' at position 2, but found 'array'.
Loading history...
27
     */
28
    final public function __construct(
29
        protected array $tuples,
30
    ) {
31
        Assert::allIsArray($tuples, SchemaViolationException::class);
32
        Assert::allCount($tuples, 2);
33
34
        foreach ($tuples as $tuple) {
35
            Assert::isInstanceOf($tuple[0], SPKISexp::class, SchemaViolationException::class);
36
            Assert::nullOrIsInstanceOf($tuple[1], SerializableElementInterface::class, SchemaViolationException::class);
37
        }
38
    }
39
40
41
    /**
42
     * Collect the value of the SPKISexp-property
43
     *
44
     * @return array{array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{array{\SimpleSAML\...ElementInterface|null}} at position 2 could not be parsed: Expected ':' at position 2, but found 'array'.
Loading history...
45
     */
46
    public function getTuples(): array
47
    {
48
        return $this->tuples;
49
    }
50
51
52
    /**
53
     * Convert XML into a SPKIData
54
     *
55
     * @param \DOMElement $xml The XML element we should load
56
     * @return static
57
     *
58
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
59
     *   If the qualified name of the supplied element is wrong
60
     */
61
    public static function fromXML(DOMElement $xml): static
62
    {
63
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
64
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
65
66
        $registry = ElementRegistry::getInstance();
67
        $tuples = [];
68
        $tuple = [null, null];
69
        foreach ($xml->childNodes as $node) {
70
            if ($node instanceof DOMElement) {
71
                if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') {
72
                    if ($tuple[0] !== null) {
73
                        $tuples[] = $tuple;
74
                    }
75
                    $tuple = [SPKISexp::fromXML($node), null];
76
                } elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) {
77
                    $handler = $registry->getElementHandler($node->namespaceURI, $node->localName);
78
                    $tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node);
79
                    $tuples[] = $tuple;
80
                    $tuple = [null, null];
81
                }
82
            }
83
        }
84
85
        if ($tuple[0] !== null) {
86
            $tuples[] = $tuple;
87
        }
88
89
        return new static($tuples);
90
    }
91
92
93
    /**
94
     * Convert this SPKIData to XML.
95
     *
96
     * @param \DOMElement|null $parent The element we should append this SPKIData to.
97
     * @return \DOMElement
98
     */
99
    public function toXML(?DOMElement $parent = null): DOMElement
100
    {
101
        $e = $this->instantiateParentElement($parent);
102
103
        foreach ($this->getTuples() as $tuple) {
104
            $tuple[0]->toXML($e);
105
            $tuple[1]?->toXML($e);
106
        }
107
108
        return $e;
109
    }
110
}
111