Passed
Pull Request — master (#55)
by Tim
02:07
created

DateTimeValue::sanitizeValue()   A

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 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
    /** @var string */
17
    public const SCHEMA_TYPE = 'xs:dateTime';
18
19
    /** @var string */
20
    public const DATETIME_FORMAT = 'Y-m-d\\TH:i:sP';
21
22
23
    /**
24
     * Sanitize the value.
25
     *
26
     * @param string $value  The unsanitized value
27
     * @return string
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\XML\Exception\SchemaViolationException on failure
40
     * @return void
41
     */
42
    protected function validateValue(string $value): void
43
    {
44
        Assert::validDateTime($this->sanitizeValue($value), SchemaViolationException::class);
45
    }
46
47
48
    /**
49
     * @param \DateTimeInterface $value
50
     * @return static
51
     */
52
    public static function fromDateTime(DateTimeInterface $value): static
53
    {
54
        return new static($value->format(static::DATETIME_FORMAT));
55
    }
56
}
57