AbstractFreshnessType::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 4
nop 1
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\ExtendableAttributesTrait;
10
use SimpleSAML\XML\TypedTextContentTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Type\BooleanValue;
13
use SimpleSAML\XMLSchema\Type\UnsignedIntValue;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
16
use function var_export;
17
18
/**
19
 * Class defining the FreshnessType element
20
 *
21
 * @package simplesamlphp/ws-security
22
 */
23
abstract class AbstractFreshnessType extends AbstractFedElement
24
{
25
    use TypedTextContentTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TypedTextContentTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...d\AbstractFreshnessType: $localName, $namespaceURI
Loading history...
26
    use ExtendableAttributesTrait;
27
28
29
    /** @var string */
30
    public const TEXTCONTENT_TYPE = UnsignedIntValue::class;
31
32
    /** The namespace-attribute for the xs:anyAttribute element */
33
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
34
35
36
    /**
37
     * AbstractFreshnessType constructor
38
     *
39
     * @param \SimpleSAML\XMLSchema\Type\UnsignedIntValue $content
40
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $AllowCache
41
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
42
     */
43
    final public function __construct(
44
        UnsignedIntValue $content,
45
        protected ?BooleanValue $AllowCache = null,
46
        array $namespacedAttributes = [],
47
    ) {
48
        $this->setContent($content);
49
        $this->setAttributesNS($namespacedAttributes);
50
    }
51
52
53
    /**
54
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
55
     */
56
    public function getAllowCache(): ?BooleanValue
57
    {
58
        return $this->AllowCache;
59
    }
60
61
62
    /**
63
     * Create an instance of this object from its XML representation.
64
     *
65
     * @param \DOMElement $xml
66
     * @return static
67
     *
68
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
69
     *   if the qualified name of the supplied element is wrong
70
     */
71
    public static function fromXML(DOMElement $xml): static
72
    {
73
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
74
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
75
76
        return new static(
77
            UnsignedIntValue::fromString($xml->textContent),
78
            self::getOptionalAttribute($xml, 'AllowCache', BooleanValue::class, null),
79
            self::getAttributesNSFromXML($xml),
80
        );
81
    }
82
83
84
    /**
85
     * Add this IssuerNameType to an XML element.
86
     *
87
     * @param \DOMElement|null $parent The element we should append this issuer name to.
88
     * @return \DOMElement
89
     */
90
    public function toXML(?DOMElement $parent = null): DOMElement
91
    {
92
        $e = parent::instantiateParentElement($parent);
93
        $e->textContent = $this->getContent()->getValue();
94
95
        if ($this->getAllowCache() !== null) {
96
            $e->setAttribute('AllowCache', var_export($this->getAllowCache()->toBoolean(), true));
97
        }
98
99
        foreach ($this->getAttributesNS() as $attr) {
100
            $attr->toXML($e);
101
        }
102
103
        return $e;
104
    }
105
}
106