Issues (81)

src/XML/ds/KeyValue.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\ExtendableElementTrait;
11
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
12
use SimpleSAML\XML\SerializableElementInterface;
13
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException, TooManyElementsException};
14
use SimpleSAML\XMLSchema\XML\Enumeration\NamespaceEnum;
15
use SimpleSAML\XMLSecurity\Constants as C;
16
use SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue;
17
18
use function array_merge;
19
use function array_pop;
20
21
/**
22
 * Class representing a ds:KeyValue element.
23
 *
24
 * @package simplesamlphp/xml-security
25
 */
26
final class KeyValue extends AbstractDsElement implements SchemaValidatableElementInterface
27
{
28
    // We use our own getter instead of the trait's one, so we prevent their use by marking them private
29
    use ExtendableElementTrait {
30
        getElements as private;
31
        setElements as private;
32
    }
33
    use SchemaValidatableElementTrait;
0 ignored issues
show
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\KeyValue: $message, $line
Loading history...
34
35
36
    /** The namespace-attribute for the xs:any element */
37
    public const XS_ANY_ELT_NAMESPACE = NamespaceEnum::Other;
38
39
40
    /**
41
     * Initialize an KeyValue.
42
     *
43
     * @param \SimpleSAML\XML\SerializableElementInterface $keyValue
44
     */
45
    final public function __construct(
46
        protected RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface $keyValue,
47
    ) {
48
        /** @var \SimpleSAML\XML\AbstractElement|\SimpleSAML\XML\Chunk $keyValue */
49
        if (
50
            !($keyValue instanceof RSAKeyValue
51
            || $keyValue instanceof DSAKeyValue
52
            || $keyValue instanceof ECKeyValue)
53
        ) {
54
            Assert::true(
55
                (($keyValue instanceof Chunk) ? $keyValue->getNamespaceURI() : $keyValue::getNameSpaceURI())
56
                !== C::NS_XDSIG,
57
                'A <ds:KeyValue> requires either a RSAKeyValue, DSAKeyValue, ECKeyValue '
58
                . 'or an element in namespace ##other',
59
                SchemaViolationException::class,
60
            );
61
        }
62
    }
63
64
65
    /**
66
     * Collect the value of the RSAKeyValue-property
67
     *
68
     * @return (\SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue| at position 3 could not be parsed: the token is null at position 3.
Loading history...
69
     *         \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue|
70
     *         \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue|
71
     *         \SimpleSAML\XML\SerializableElementInterface)
72
     */
73
    public function getKeyValue(): RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface
74
    {
75
        return $this->keyValue;
76
    }
77
78
79
    /**
80
     * Convert XML into a KeyValue
81
     *
82
     * @param \DOMElement $xml The XML element we should load
83
     * @return static
84
     *
85
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
86
     *   If the qualified name of the supplied element is wrong
87
     */
88
    public static function fromXML(DOMElement $xml): static
89
    {
90
        Assert::same($xml->localName, 'KeyValue', InvalidDOMElementException::class);
91
        Assert::same($xml->namespaceURI, KeyValue::NS, InvalidDOMElementException::class);
92
93
        $keyValue = array_merge(
94
            RSAKeyValue::getChildrenOfClass($xml),
95
            DSAKeyValue::getChildrenOfClass($xml),
96
            self::getChildElementsFromXML($xml),
97
        );
98
99
        Assert::count(
100
            $keyValue,
101
            1,
102
            'A <ds:KeyValue> must contain exactly one child element',
103
            TooManyElementsException::class,
104
        );
105
106
        return new static(array_pop($keyValue));
107
    }
108
109
110
    /**
111
     * Convert this KeyValue element to XML.
112
     *
113
     * @param \DOMElement|null $parent The element we should append this KeyValue element to.
114
     * @return \DOMElement
115
     */
116
    public function toXML(?DOMElement $parent = null): DOMElement
117
    {
118
        $e = $this->instantiateParentElement($parent);
119
120
        $this->getKeyValue()->toXML($e);
121
122
        return $e;
123
    }
124
}
125