Action::toXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
10
use SimpleSAML\SAML2\XML\StringElementTrait;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
13
/**
14
 * Class representing SAML2 Action
15
 *
16
 * @package simplesamlphp/saml2
17
 */
18
final class Action extends AbstractSamlElement
19
{
20
    use StringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\SAML2\XML\StringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\saml\Action: $localName, $namespaceURI
Loading history...
21
22
23
    /**
24
     * Initialize an Action.
25
     *
26
     * @param string $namespace  This attribute was marked REQUIRED in the 2012 SAML errata (E36)
27
     * @param string $content
28
     */
29
    public function __construct(
30
        protected string $namespace,
31
        string $content,
32
    ) {
33
        SAMLAssert::validURI($namespace);
34
35
        $this->setContent($content);
36
    }
37
38
39
    /**
40
     * Collect the value of the namespace-property
41
     *
42
     * @return string
43
     */
44
    public function getNamespace(): string
45
    {
46
        return $this->namespace;
47
    }
48
49
50
    /**
51
     * Convert XML into a Action
52
     *
53
     * @param \DOMElement $xml The XML element we should load
54
     * @return static
55
     *
56
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
57
     *   if the qualified name of the supplied element is wrong
58
     */
59
    public static function fromXML(DOMElement $xml): static
60
    {
61
        Assert::same($xml->localName, 'Action', InvalidDOMElementException::class);
62
        Assert::same($xml->namespaceURI, Action::NS, InvalidDOMElementException::class);
63
64
        return new self(
65
            self::getAttribute($xml, 'Namespace'),
66
            $xml->textContent,
67
        );
68
    }
69
70
71
    /**
72
     * Convert this Action to XML.
73
     *
74
     * @param \DOMElement|null $parent The element we should append this Action to.
75
     * @return \DOMElement
76
     */
77
    public function toXML(?DOMElement $parent = null): DOMElement
78
    {
79
        $e = $this->instantiateParentElement($parent);
80
81
        $e->setAttribute('Namespace', $this->getNamespace());
82
        $e->textContent = $this->getContent();
83
84
        return $e;
85
    }
86
}
87