PolicyReference::fromXML()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 26
rs 9.0111
cc 6
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsp;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Type\AnyURIValue;
14
use SimpleSAML\XMLSchema\Type\Base64BinaryValue;
15
use SimpleSAML\XMLSchema\XML\Constants\NS;
16
17
/**
18
 * Class defining the PolicyReference element
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
final class PolicyReference extends AbstractWspElement implements SchemaValidatableElementInterface
23
{
24
    use ExtendableAttributesTrait;
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\wsp\PolicyReference: $message, $line
Loading history...
26
27
28
    /** The namespace-attribute for the xs:anyAttribute element */
29
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
30
31
32
    /**
33
     * Initialize a wsp:PolicyReference
34
     *
35
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $URI
36
     * @param \SimpleSAML\XMLSchema\Type\Base64BinaryValue|null $Digest
37
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $DigestAlgorithm
38
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
39
     */
40
    public function __construct(
41
        protected AnyURIValue $URI,
42
        protected ?Base64BinaryValue $Digest = null,
43
        protected ?AnyURIValue $DigestAlgorithm = null,
44
        array $namespacedAttributes = [],
45
    ) {
46
        $this->setAttributesNS($namespacedAttributes);
47
    }
48
49
50
    /**
51
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
52
     */
53
    public function getURI(): AnyURIValue
54
    {
55
        return $this->URI;
56
    }
57
58
59
    /**
60
     * @return \SimpleSAML\XMLSchema\Type\Base64BinaryValue|null
61
     */
62
    public function getDigest(): ?Base64BinaryValue
63
    {
64
        return $this->Digest;
65
    }
66
67
68
    /**
69
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
70
     */
71
    public function getDigestAlgorithm(): ?AnyURIValue
72
    {
73
        return $this->DigestAlgorithm;
74
    }
75
76
77
    /*
78
     * Convert XML into an wsp:PolicyReference element
79
     *
80
     * @param \DOMElement $xml The XML element we should load
81
     * @return static
82
     *
83
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
84
     *   If the qualified name of the supplied element is wrong
85
     */
86
    public static function fromXML(DOMElement $xml): static
87
    {
88
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
89
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
90
91
        $namespacedAttributes = self::getAttributesNSFromXML($xml);
92
        foreach ($namespacedAttributes as $i => $attr) {
93
            if ($attr->getNamespaceURI() === null) {
94
                if ($attr->getAttrName() === 'URI') {
95
                    unset($namespacedAttributes[$i]);
96
                    continue;
97
                } elseif ($attr->getAttrName() === 'Digest') {
98
                    unset($namespacedAttributes[$i]);
99
                    continue;
100
                } elseif ($attr->getAttrName() === 'DigestAlgorithm') {
101
                    unset($namespacedAttributes[$i]);
102
                    continue;
103
                }
104
            }
105
        }
106
107
        return new static(
108
            self::getAttribute($xml, 'URI', AnyURIValue::class),
109
            self::getOptionalAttribute($xml, 'Digest', Base64BinaryValue::class, null),
110
            self::getOptionalAttribute($xml, 'DigestAlgorithm', AnyURIValue::class, null),
111
            $namespacedAttributes,
112
        );
113
    }
114
115
116
    /**
117
     * Convert this wsp:PolicyReference to XML.
118
     *
119
     * @param \DOMElement|null $parent The element we should add this wsp:Policy to
120
     * @return \DOMElement This wsp:PolicyReference element.
121
     */
122
    public function toXML(?DOMElement $parent = null): DOMElement
123
    {
124
        $e = $this->instantiateParentElement($parent);
125
        $e->setAttribute('URI', $this->getURI()->getValue());
126
127
        if ($this->getDigest() !== null) {
128
            $e->setAttribute('Digest', $this->getDigest()->getValue());
129
        }
130
131
        if ($this->getDigestAlgorithm() !== null) {
132
            $e->setAttribute('DigestAlgorithm', $this->getDigestAlgorithm()->getValue());
133
        }
134
135
        foreach ($this->getAttributesNS() as $attr) {
136
            $attr->toXML($e);
137
        }
138
139
        return $e;
140
    }
141
}
142