X509Data::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 16
rs 9.9
c 1
b 0
f 0
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\ExtendableElementTrait;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16
use SimpleSAML\XMLSecurity\Exception\ProtocolViolationException;
17
use SimpleSAML\XMLSecurity\XML\dsig11\X509Digest;
18
19
/**
20
 * Class representing a ds:X509Data element.
21
 *
22
 * @package simplesamlphp/xml-security
23
 */
24
final class X509Data extends AbstractDsElement implements SchemaValidatableElementInterface
25
{
26
    use ExtendableElementTrait;
27
    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\X509Data: $message, $line
Loading history...
28
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
32
33
    /** The exclusions for the xs:any element */
34
    public const XS_ANY_ELT_EXCLUSIONS = [
35
        [X509Digest::NS, 'X509Digest'],
36
    ];
37
38
39
    /**
40
     * Initialize a X509Data.
41
     *
42
     * @param (
43
     *   \SimpleSAML\XMLSecurity\XML\ds\X509Certificate|
44
     *   \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial|
45
     *   \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName|
46
     *   \SimpleSAML\XMLSecurity\XML\ds\X509SKI|
47
     *   \SimpleSAML\XMLSecurity\XML\ds\X509CRL|
48
     *   \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest
49
     * )[] $data
50
     * @param \SimpleSAML\XML\SerializableElementInterface[] $children
51
     */
52
    public function __construct(
53
        protected array $data,
54
        protected array $children = [],
55
    ) {
56
        /**
57
         * At least one element from the dsig namespaces should be present and
58
         * additional elements from an external namespace to accompany/complement them.
59
         */
60
        Assert::minCount($data, 1, ProtocolViolationException::class);
61
        Assert::maxCount($data, C::UNBOUNDED_LIMIT);
62
        Assert::allIsInstanceOfAny(
63
            $data,
64
            [
65
                X509Certificate::class,
66
                X509IssuerSerial::class,
67
                X509SubjectName::class,
68
                X509Digest::class,
69
                X509SKI::class,
70
                X509CRL::class,
71
            ],
72
            InvalidArgumentException::class,
73
        );
74
75
        $this->setElements($children);
76
    }
77
78
79
    /**
80
     * Collect the value of the data-property
81
     *
82
     * @return (
83
     *   \SimpleSAML\XMLSecurity\XML\ds\X509Certificate|
84
     *   \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial|
85
     *   \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName|
86
     *   \SimpleSAML\XMLSecurity\XML\ds\X509SKI|
87
     *   \SimpleSAML\XMLSecurity\XML\ds\X509CRL|
88
     *   \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest
89
     * )[]
90
     */
91
    public function getData(): array
92
    {
93
        return $this->data;
94
    }
95
96
97
    /**
98
     * Convert XML into a X509Data
99
     *
100
     * @param \DOMElement $xml The XML element we should load
101
     * @return static
102
     *
103
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
104
     *   If the qualified name of the supplied element is wrong
105
     */
106
    public static function fromXML(DOMElement $xml): static
107
    {
108
        Assert::same($xml->localName, 'X509Data', InvalidDOMElementException::class);
109
        Assert::same($xml->namespaceURI, X509Data::NS, InvalidDOMElementException::class);
110
111
        $x509Certificate = X509Certificate::getChildrenOfClass($xml);
112
        $x509IssuerSerial = X509IssuerSerial::getChildrenOfClass($xml);
113
        $x509SubjectName = X509SubjectName::getChildrenOfClass($xml);
114
        $x509SKI = X509SKI::getChildrenOfClass($xml);
115
        $x509CRL = X509CRL::getChildrenOfClass($xml);
116
        $x509Digest = X509Digest::getChildrenOfClass($xml);
117
118
        $data = array_merge($x509Certificate, $x509IssuerSerial, $x509SubjectName, $x509SKI, $x509CRL, $x509Digest);
119
        $children = self::getChildElementsFromXML($xml);
120
121
        return new static($data, $children);
122
    }
123
124
125
    /**
126
     * Convert this X509Data element to XML.
127
     *
128
     * @param \DOMElement|null $parent The element we should append this X509Data element to.
129
     * @return \DOMElement
130
     */
131
    public function toXML(?DOMElement $parent = null): DOMElement
132
    {
133
        $e = $this->instantiateParentElement($parent);
134
135
        foreach ($this->getData() as $d) {
136
            $d->toXML($e);
137
        }
138
139
        foreach ($this->getElements() as $c) {
140
            $c->toXML($e);
141
        }
142
143
        return $e;
144
    }
145
}
146