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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function str_replace;
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
    /** The namespace-attribute for the xs:anyAttribute element */
28
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
29
30
31
    /**
32
     * Initialize a wsp:PolicyReference
33
     *
34
     * @param string $URI
35
     * @param string|null $Digest
36
     * @param string $DigestAlgorithm
37
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
38
     */
39
    public function __construct(
40
        protected string $URI,
41
        protected ?string $Digest = null,
42
        protected ?string $DigestAlgorithm = 'http://schemas.xmlsoap.org/ws/2004/09/policy/Sha1Exc',
43
        array $namespacedAttributes = [],
44
    ) {
45
        Assert::validURI($URI, SchemaViolationException::class);
46
        Assert::nullOrValidBase64($Digest, SchemaViolationException::class);
47
        Assert::nullOrValidURI($DigestAlgorithm, SchemaViolationException::class);
48
49
        $this->setAttributesNS($namespacedAttributes);
50
    }
51
52
53
    /**
54
     * @return string
55
     */
56
    public function getURI(): string
57
    {
58
        return $this->URI;
59
    }
60
61
62
    /**
63
     * @return string|null
64
     */
65
    public function getDigest(): ?string
66
    {
67
        return $this->Digest ? str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $this->Digest) : null;
68
    }
69
70
71
    /**
72
     * @return string|null
73
     */
74
    public function getDigestAlgorithm(): ?string
75
    {
76
        return $this->DigestAlgorithm;
77
    }
78
79
80
    /*
81
     * Convert XML into an wsp:PolicyReference element
82
     *
83
     * @param \DOMElement $xml The XML element we should load
84
     * @return static
85
     *
86
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
87
     *   If the qualified name of the supplied element is wrong
88
     */
89
    public static function fromXML(DOMElement $xml): static
90
    {
91
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
92
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
93
94
        $namespacedAttributes = self::getAttributesNSFromXML($xml);
95
        foreach ($namespacedAttributes as $i => $attr) {
96
            if ($attr->getNamespaceURI() === null) {
97
                if ($attr->getAttrName() === 'URI') {
98
                    unset($namespacedAttributes[$i]);
99
                    continue;
100
                } elseif ($attr->getAttrName() === 'Digest') {
101
                    unset($namespacedAttributes[$i]);
102
                    continue;
103
                } elseif ($attr->getAttrName() === 'DigestAlgorithm') {
104
                    unset($namespacedAttributes[$i]);
105
                    continue;
106
                }
107
            }
108
        }
109
110
        return new static(
111
            self::getAttribute($xml, 'URI'),
112
            self::getOptionalAttribute($xml, 'Digest', null),
113
            self::getOptionalAttribute($xml, 'DigestAlgorithm', null),
114
            $namespacedAttributes,
115
        );
116
    }
117
118
119
    /**
120
     * Convert this wsp:PolicyReference to XML.
121
     *
122
     * @param \DOMElement|null $parent The element we should add this wsp:Policy to
123
     * @return \DOMElement This wsp:PolicyReference element.
124
     */
125
    public function toXML(?DOMElement $parent = null): DOMElement
126
    {
127
        $e = $this->instantiateParentElement($parent);
128
        $e->setAttribute('URI', $this->getURI());
129
130
        if ($this->getDigest() !== null) {
131
            $e->setAttribute('Digest', $this->getDigest());
132
        }
133
134
        if ($this->getDigestAlgorithm() !== null) {
135
            $e->setAttribute('DigestAlgorithm', $this->getDigestAlgorithm());
136
        }
137
138
        foreach ($this->getAttributesNS() as $attr) {
139
            $attr->toXML($e);
140
        }
141
142
        return $e;
143
    }
144
}
145