Issues (88)

src/XML/ds/X509Data.php (2 issues)

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\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XMLSecurity\Constants as C;
14
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
15
use SimpleSAML\XMLSecurity\XML\dsig11\X509Digest;
16
17
/**
18
 * Class representing a ds:X509Data element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
final class X509Data extends AbstractDsElement implements SchemaValidatableElementInterface
23
{
24
    use SchemaValidatableElementTrait;
0 ignored issues
show
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\X509Data: $message, $line
Loading history...
25
26
    /**
27
     * Initialize a X509Data.
28
     *
29
     * @param (\SimpleSAML\XML\Chunk|
30
     *         \SimpleSAML\XMLSecurity\XML\ds\X509Certificate|
31
     *         \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial|
32
     *         \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName|
33
     *         \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest)[] $data
34
     */
35
    public function __construct(
36
        protected array $data,
37
    ) {
38
        Assert::maxCount($data, C::UNBOUNDED_LIMIT);
39
        Assert::allIsInstanceOfAny(
40
            $data,
41
            [Chunk::class, X509Certificate::class, X509IssuerSerial::class, X509SubjectName::class, X509Digest::class],
42
            InvalidArgumentException::class,
43
        );
44
    }
45
46
47
    /**
48
     * Collect the value of the data-property
49
     *
50
     * @return (\SimpleSAML\XML\Chunk|
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\SimpleSAML\XML\Chunk| at position 3 could not be parsed: the token is null at position 3.
Loading history...
51
     *          \SimpleSAML\XMLSecurity\XML\ds\X509Certificate|
52
     *          \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial|
53
     *          \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName|
54
     *          \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest)[]
55
     */
56
    public function getData(): array
57
    {
58
        return $this->data;
59
    }
60
61
62
    /**
63
     * Convert XML into a X509Data
64
     *
65
     * @param \DOMElement $xml The XML element we should load
66
     * @return static
67
     *
68
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
69
     *   If the qualified name of the supplied element is wrong
70
     */
71
    public static function fromXML(DOMElement $xml): static
72
    {
73
        Assert::same($xml->localName, 'X509Data', InvalidDOMElementException::class);
74
        Assert::same($xml->namespaceURI, X509Data::NS, InvalidDOMElementException::class);
75
76
        $data = [];
77
78
        for ($n = $xml->firstChild; $n !== null; $n = $n->nextSibling) {
79
            if (!($n instanceof DOMElement)) {
80
                continue;
81
            } elseif ($n->namespaceURI === self::NS) {
82
                $data[] = match ($n->localName) {
83
                    'X509Certificate' => X509Certificate::fromXML($n),
84
                    'X509IssuerSerial' => X509IssuerSerial::fromXML($n),
85
                    'X509SubjectName' => X509SubjectName::fromXML($n),
86
                    default => new Chunk($n),
87
                };
88
            } elseif ($n->namespaceURI === C::NS_XDSIG11) {
89
                $data[] = match ($n->localName) {
90
                    'X509Digest' => X509Digest::fromXML($n),
91
                    default => new Chunk($n),
92
                };
93
            } else {
94
                $data[] = new Chunk($n);
95
                continue;
96
            }
97
        }
98
99
        return new static($data);
100
    }
101
102
103
    /**
104
     * Convert this X509Data element to XML.
105
     *
106
     * @param \DOMElement|null $parent The element we should append this X509Data element to.
107
     * @return \DOMElement
108
     */
109
    public function toXML(?DOMElement $parent = null): DOMElement
110
    {
111
        $e = $this->instantiateParentElement($parent);
112
113
        foreach ($this->getData() as $n) {
114
            $n->toXML($e);
115
        }
116
117
        return $e;
118
    }
119
}
120