AbstractAttributeExtensibleString::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\XML\StringElementTrait;
12
use SimpleSAML\XML\XsNamespace as NS;
13
14
/**
15
 * An AbstractAttributeExtensibleString element
16
 *
17
 * @package simplesamlphp/ws-security
18
 */
19
abstract class AbstractAttributeExtensibleString extends AbstractFedElement
20
{
21
    use ExtendableAttributesTrait;
22
    use StringElementTrait;
23
24
    /** The namespace-attribute for the xs:anyAttribute element */
25
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
26
27
28
    /**
29
     * @param string $content
30
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
31
     */
32
    final public function __construct(string $content, array $namespacedAttributes = [])
33
    {
34
        $this->setContent($content);
35
        $this->setAttributesNS($namespacedAttributes);
36
    }
37
38
39
    /**
40
     * Create a class from XML
41
     *
42
     * @param \DOMElement $xml
43
     * @return static
44
     */
45
    public static function fromXML(DOMElement $xml): static
46
    {
47
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
48
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
49
50
        return new static(
51
            $xml->textContent,
52
            self::getAttributesNSFromXML($xml),
53
        );
54
    }
55
56
57
    /**
58
     * Create XML from this class
59
     *
60
     * @param \DOMElement|null $parent
61
     * @return \DOMElement
62
     */
63
    public function toXML(?DOMElement $parent = null): DOMElement
64
    {
65
        $e = $this->instantiateParentElement($parent);
66
        $e->textContent = $this->getContent();
67
68
        foreach ($this->getAttributesNS() as $attr) {
69
            $attr->toXML($e);
70
        }
71
72
        return $e;
73
    }
74
}
75