Passed
Branch feature/php8.3 (4d3b0a)
by Tim
17:15
created

DateTimeValue   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\Type;
6
7
use DateTimeImmutable;
8
use DateTimeInterface;
9
use Psr\Clock\ClockInterface;
10
use SimpleSAML\XML\Assert\Assert;
11
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12
use SimpleSAML\XMLSchema\Type\Interface\AbstractAnySimpleType;
13
14
/**
15
 * @package simplesaml/xml-common
16
 */
17
class DateTimeValue extends AbstractAnySimpleType
18
{
19
    public const string SCHEMA_TYPE = 'dateTime';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 19 at column 24
Loading history...
20
21
    public const string DATETIME_FORMAT = 'Y-m-d\\TH:i:sP';
22
23
24
    /**
25
     * Sanitize the value.
26
     *
27
     * @param string $value  The unsanitized value
28
     */
29
    protected function sanitizeValue(string $value): string
30
    {
31
        return static::collapseWhitespace(static::normalizeWhitespace($value));
32
    }
33
34
35
    /**
36
     * Validate the value.
37
     *
38
     * @param string $value
39
     * @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
40
     */
41
    protected function validateValue(string $value): void
42
    {
43
        Assert::validDateTime($this->sanitizeValue($value), SchemaViolationException::class);
44
    }
45
46
47
    /**
48
     */
49
    public static function now(ClockInterface $clock): static
50
    {
51
        return static::fromDateTime($clock->now());
52
    }
53
54
55
    /**
56
     * @param \DateTimeInterface $value
57
     */
58
    public static function fromDateTime(DateTimeInterface $value): static
59
    {
60
        return new static($value->format(static::DATETIME_FORMAT));
61
    }
62
63
64
    /**
65
     */
66
    public function toDateTime(): DateTimeImmutable
67
    {
68
        return new DateTimeImmutable($this->getValue());
69
    }
70
}
71