AbstractTimestamp   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
eloc 43
dl 0
loc 144
rs 10
c 3
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A isEmptyElement() 0 7 5
A getCreated() 0 3 1
A toXML() 0 22 4
A getExpires() 0 3 1
A fromXML() 0 20 2
A __construct() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsu;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Attribute as XMLAttribute;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function array_pop;
16
17
/**
18
 * Abstract class defining the Timestamp type
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
abstract class AbstractTimestamp extends AbstractWsuElement
23
{
24
    use ExtendableAttributesTrait;
25
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\wsu\AbstractTimestamp: $namespaceURI, $localName, $childNodes
Loading history...
26
27
    /** The namespace-attribute for the xs:anyAttribute element */
28
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
32
33
    /** The exclusions for the xs:any element */
34
    public const XS_ANY_ELT_EXCLUSIONS = [
35
        ['http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Created'],
36
        ['http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Expires'],
37
    ];
38
39
40
    /**
41
     * AbstractTimestamp constructor
42
     *
43
     * @param \SimpleSAML\WSSecurity\XML\wsu\Created|null $created
44
     * @param \SimpleSAML\WSSecurity\XML\wsu\Expires|null $expires
45
     * @param string|null $Id
46
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $elements
47
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
48
     */
49
    final public function __construct(
50
        protected ?Created $created = null,
51
        protected ?Expires $expires = null,
52
        protected ?string $Id = null,
53
        array $elements = [],
54
        array $namespacedAttributes = [],
55
    ) {
56
        Assert::nullOrValidNCName($Id);
57
58
        $this->setElements($elements);
59
        $this->setAttributesNS($namespacedAttributes);
60
    }
61
62
63
    /**
64
     * @return string|null
65
     */
66
    public function getId(): ?string
67
    {
68
        return $this->Id;
69
    }
70
71
72
    /**
73
     * @return \SimpleSAML\WSSecurity\XML\wsu\Created|null
74
     */
75
    public function getCreated(): ?Created
76
    {
77
        return $this->created;
78
    }
79
80
81
    /**
82
     * @return \SimpleSAML\WSSecurity\XML\wsu\Expires|null
83
     */
84
    public function getExpires(): ?Expires
85
    {
86
        return $this->expires;
87
    }
88
89
90
    /**
91
     * Test if an object, at the state it's in, would produce an empty XML-element
92
     *
93
     * @return bool
94
     */
95
    public function isEmptyElement(): bool
96
    {
97
        return empty($this->getCreated())
98
            && empty($this->getExpires())
99
            && empty($this->getId())
100
            && empty($this->getElements())
101
            && empty($this->getAttributesNS());
102
    }
103
104
105
    /**
106
     * Create an instance of this object from its XML representation.
107
     *
108
     * @param \DOMElement $xml
109
     * @return static
110
     *
111
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
112
     *   if the qualified name of the supplied element is wrong
113
     */
114
    public static function fromXML(DOMElement $xml): static
115
    {
116
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
117
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
118
119
        $created = Created::getChildrenOfClass($xml);
120
        $expires = Expires::getChildrenOfClass($xml);
121
        $children = self::getChildElementsFromXML($xml);
122
123
        $Id = null;
124
        if ($xml->hasAttributeNS(static::NS, 'Id')) {
125
            $Id = $xml->getAttributeNS(static::NS, 'Id');
126
        }
127
128
        return new static(
129
            array_pop($created),
130
            array_pop($expires),
131
            $Id,
132
            $children,
133
            self::getAttributesNSFromXML($xml),
134
        );
135
    }
136
137
138
    /**
139
     * Convert this Timestamp to XML.
140
     *
141
     * @param \DOMElement|null $parent The element we should append this class to.
142
     * @return \DOMElement The XML element after adding the data corresponding to this Timestamp.
143
     */
144
    public function toXML(?DOMElement $parent = null): DOMElement
145
    {
146
        $e = $this->instantiateParentElement($parent);
147
148
        $attributes = $this->getAttributesNS();
149
        if ($this->getId() !== null) {
150
            $attributes[] = new XMLAttribute(static::NS, 'wsu', 'Id', $this->getId());
151
        }
152
153
        foreach ($attributes as $attr) {
154
            $attr->toXML($e);
155
        }
156
157
        $this->getCreated()?->toXML($e);
158
        $this->getExpires()?->toXML($e);
159
160
        foreach ($this->getElements() as $detail) {
161
            /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $detail */
162
            $detail->toXML($e);
163
        }
164
165
        return $e;
166
    }
167
}
168