Action::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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