AbstractSPKIDataType   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 32
c 1
b 0
f 0
dl 0
loc 93
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getTuples() 0 3 1
B fromXML() 0 29 10
A toXML() 0 10 2
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\Registry\ElementRegistry;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XML\SerializableElementInterface;
14
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
15
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
16
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
17
18
/**
19
 * Abstract class representing the SPKIDataType.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
abstract class AbstractSPKIDataType extends AbstractDsElement implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\AbstractSPKIDataType: $message, $line
Loading history...
26
27
28
    /**
29
     * Initialize a SPKIData element.
30
     *
31
     * @param array{
32
     *   array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}
33
     * } $tuples
34
     */
35
    final public function __construct(
36
        protected array $tuples,
37
    ) {
38
        Assert::allIsArray($tuples, SchemaViolationException::class);
39
        Assert::allCount($tuples, 2);
40
41
        foreach ($tuples as $tuple) {
42
            Assert::isInstanceOf($tuple[0], SPKISexp::class, SchemaViolationException::class);
43
            Assert::nullOrIsInstanceOf($tuple[1], SerializableElementInterface::class, SchemaViolationException::class);
44
        }
45
    }
46
47
48
    /**
49
     * Collect the value of the SPKISexp-property
50
     *
51
     * @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...
52
     */
53
    public function getTuples(): array
54
    {
55
        return $this->tuples;
56
    }
57
58
59
    /**
60
     * Convert XML into a SPKIData
61
     *
62
     * @param \DOMElement $xml The XML element we should load
63
     * @return static
64
     *
65
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
66
     *   If the qualified name of the supplied element is wrong
67
     */
68
    public static function fromXML(DOMElement $xml): static
69
    {
70
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
71
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
72
73
        $registry = ElementRegistry::getInstance();
74
        $tuples = [];
75
        $tuple = [null, null];
76
        foreach ($xml->childNodes as $node) {
77
            if ($node instanceof DOMElement) {
78
                if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') {
79
                    if ($tuple[0] !== null) {
80
                        $tuples[] = $tuple;
81
                    }
82
                    $tuple = [SPKISexp::fromXML($node), null];
83
                } elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) {
84
                    $handler = $registry->getElementHandler($node->namespaceURI, $node->localName);
85
                    $tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node);
86
                    $tuples[] = $tuple;
87
                    $tuple = [null, null];
88
                }
89
            }
90
        }
91
92
        if ($tuple[0] !== null) {
93
            $tuples[] = $tuple;
94
        }
95
96
        return new static($tuples);
97
    }
98
99
100
    /**
101
     * Convert this SPKIData to XML.
102
     *
103
     * @param \DOMElement|null $parent The element we should append this SPKIData to.
104
     * @return \DOMElement
105
     */
106
    public function toXML(?DOMElement $parent = null): DOMElement
107
    {
108
        $e = $this->instantiateParentElement($parent);
109
110
        foreach ($this->getTuples() as $tuple) {
111
            $tuple[0]->toXML($e);
112
            $tuple[1]?->toXML($e);
113
        }
114
115
        return $e;
116
    }
117
}
118