AbstractSPKIDataType::toXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
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\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;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
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);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\SPKISexp was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     */
105
    public function toXML(?DOMElement $parent = null): DOMElement
106
    {
107
        $e = $this->instantiateParentElement($parent);
108
109
        foreach ($this->getTuples() as $tuple) {
110
            $tuple[0]->toXML($e);
111
            $tuple[1]?->toXML($e);
112
        }
113
114
        return $e;
115
    }
116
}
117