AbstractAttributedDateTime::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsu;
6
7
use DateTimeImmutable;
8
use DOMElement;
9
use SimpleSAML\WSSecurity\Assert\Assert;
10
use SimpleSAML\WSSecurity\Constants as C;
11
use SimpleSAML\WSSecurity\Exception\ProtocolViolationException;
12
use SimpleSAML\XML\Attribute as XMLAttribute;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\ExtendableAttributesTrait;
15
use SimpleSAML\XML\XsNamespace as NS;
16
17
/**
18
 * Abstract class defining the AttributedDateTime type
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
abstract class AbstractAttributedDateTime extends AbstractWsuElement
23
{
24
    use ExtendableAttributesTrait;
25
26
    /** The namespace-attribute for the xs:anyAttribute element */
27
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
28
29
30
    /**
31
     * AbstractAttributedDateTime constructor
32
     *
33
     * @param \DateTimeImmutable $dateTime
34
     * @param string|null $Id
35
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
36
     */
37
    final public function __construct(
38
        protected DateTimeImmutable $dateTime,
39
        protected ?string $Id = null,
40
        array $namespacedAttributes = [],
41
    ) {
42
        Assert::nullOrValidNCName($Id);
43
        $this->setAttributesNS($namespacedAttributes);
44
    }
45
46
47
    /**
48
     * @return string|null
49
     */
50
    public function getId(): ?string
51
    {
52
        return $this->Id;
53
    }
54
55
56
    /**
57
     * Collect the value of the dateTime property
58
     *
59
     * @return \DateTimeImmutable
60
     */
61
    public function getDateTime(): DateTimeImmutable
62
    {
63
        return $this->dateTime;
64
    }
65
66
67
    /**
68
     * Create an instance of this object from its XML representation.
69
     *
70
     * @param \DOMElement $xml
71
     * @return static
72
     *
73
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
74
     *   if the qualified name of the supplied element is wrong
75
     */
76
    public static function fromXML(DOMElement $xml): static
77
    {
78
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
79
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
80
81
        // Time values MUST be expressed in the UTC timezone using the 'Z' timezone identifier
82
        // Strip sub-seconds
83
        $xml->textContent = preg_replace('/([.][0-9]+)/', '', $xml->textContent, 1);
84
        Assert::validDateTime($xml->textContent, ProtocolViolationException::class);
85
86
        $Id = null;
87
        if ($xml->hasAttributeNS(static::NS, 'Id')) {
88
            $Id = $xml->getAttributeNS(static::NS, 'Id');
89
        }
90
91
        return new static(new DateTimeImmutable($xml->textContent), $Id, self::getAttributesNSFromXML($xml));
92
    }
93
94
95
    /**
96
     * @param \DOMElement|null $parent
97
     * @return \DOMElement
98
     */
99
    final public function toXML(?DOMElement $parent = null): DOMElement
100
    {
101
        $e = $this->instantiateParentElement($parent);
102
        $e->textContent = $this->getDateTime()->format(C::DATETIME_FORMAT);
103
104
        $attributes = $this->getAttributesNS();
105
        if ($this->getId() !== null) {
106
            $attributes[] = new XMLAttribute(static::NS, 'wsu', 'Id', $this->getId());
107
        }
108
109
        foreach ($attributes as $attr) {
110
            $attr->toXML($e);
111
        }
112
113
        return $e;
114
    }
115
}
116