Passed
Pull Request — master (#55)
by Tim
01:57
created

DateTimeValue   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 5
c 1
b 0
f 0
dl 0
loc 37
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitizeValue() 0 3 1
A validateValue() 0 3 1
A fromDateTime() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML\Type;
6
7
use DateTimeInterface;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\Exception\SchemaViolationException;
10
11
/**
12
 * @package simplesaml/xml-common
13
 */
14
class DateTimeValue extends AbstractValueType
15
{
16
    public const DATETIME_FORMAT = 'Y-m-d\\TH:i:sP';
17
18
19
    /**
20
     * Sanitize the value.
21
     *
22
     * @param string $value  The unsanitized value
23
     * @return string
24
     */
25
    protected function sanitizeValue(string $value): string
26
    {
27
        return static::collapseWhitespace(static::normalizeWhitespace($value));
28
    }
29
30
31
    /**
32
     * Validate the value.
33
     *
34
     * @param string $value
35
     * @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
36
     * @return void
37
     */
38
    protected function validateValue(string $value): void
39
    {
40
        Assert::validDateTime($this->sanitizeValue($value), SchemaViolationException::class);
41
    }
42
43
44
    /**
45
     * @param \DateTimeInterface $value
46
     * @return static
47
     */
48
    public static function fromDateTime(DateTimeInterface $value): static
49
    {
50
        return new static($value->format(static::DATETIME_FORMAT));
51
    }
52
}
53