AuthenticationDate::toXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\CAS\XML\cas;
6
7
use DateTimeImmutable;
8
use DOMElement;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\CAS\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
13
/**
14
 * Class for CAS authenticationDate
15
 *
16
 * @package simplesamlphp/cas
17
 */
18
final class AuthenticationDate extends AbstractCasElement
19
{
20
    /** @var string */
21
    final public const LOCALNAME = 'authenticationDate';
22
23
24
    /**
25
     * @param \DateTimeImmutable $timestamp
26
     */
27
    final public function __construct(
28
        protected DateTimeImmutable $timestamp,
29
    ) {
30
    }
31
32
33
    /**
34
     * Retrieve the issue timestamp of this message.
35
     *
36
     * @return \DateTimeImmutable The issue timestamp of this message, as an UNIX timestamp
37
     */
38
    public function getTimestamp(): DateTimeImmutable
39
    {
40
        return $this->timestamp;
41
    }
42
43
44
    /**
45
     * Convert this element into an XML document.
46
     *
47
     * @return \DOMElement The root element of the DOM tree
48
     */
49
    public function toXML(?DOMElement $parent = null): DOMElement
50
    {
51
        $root = $this->instantiateParentElement($parent);
52
        $root->textContent = $this->getTimestamp()->format(C::DATETIME_FORMAT);
53
54
        return $root;
55
    }
56
57
58
    /**
59
     * Convert XML into a cas:authenticationDate
60
     *
61
     * @param \DOMElement $xml The XML element we should load
62
     * @return static
63
     *
64
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
65
     *   If the qualified name of the supplied element is wrong
66
     */
67
    public static function fromXML(DOMElement $xml): static
68
    {
69
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
70
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
71
72
        return new static(new DateTimeImmutable($xml->textContent));
73
    }
74
}
75