DateTimeTrait::validDateTime()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML\Assert;
6
7
use InvalidArgumentException;
8
9
/**
10
 * @package simplesamlphp/xml-common
11
 */
12
trait DateTimeTrait
13
{
14
    /**
15
     * The ·lexical space· of dateTime consists of finite-length sequences of characters of the form:
16
     * '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?, where
17
     *
18
     * * '-'? yyyy is a four-or-more digit optionally negative-signed numeral that represents the year;
19
     *   if more than four digits, leading zeros are prohibited, and '0000' is prohibited (see the Note above (§3.2.7);
20
     *   also note that a plus sign is not permitted);
21
     * * the remaining '-'s are separators between parts of the date portion;
22
     * * the first mm is a two-digit numeral that represents the month;
23
     * * dd is a two-digit numeral that represents the day;
24
     * * 'T' is a separator indicating that time-of-day follows;
25
     * * hh is a two-digit numeral that represents the hour; '24' is permitted if the minutes and seconds represented
26
     *   are zero, and the dateTime value so represented is the first instant of the following day
27
     *   (the hour property of a dateTime object in the ·value space· cannot have a value greater than 23);
28
     * * ':' is a separator between parts of the time-of-day portion;
29
     * * the second mm is a two-digit numeral that represents the minute;
30
     * * ss is a two-integer-digit numeral that represents the whole seconds;
31
     * * '.' s+ (if present) represents the fractional seconds;
32
     * * zzzzzz (if present) represents the timezone (as described below).
33
     */
34
    private static string $datetime_regex = '/^
35
        -?
36
        ([1-9][0-9]*|[0-9]{4})
37
        -
38
        (
39
            (
40
                (0(1|3|5|7|8)|1(0|2))
41
                -
42
                (0[1-9]|(1|2)[0-9]|3[0-1])
43
            )|
44
            (
45
                (0(4|6|9)|11)
46
                -
47
                (0[1-9]|(1|2)[0-9]|30)
48
            )|
49
                (02-(0[1-9]|(1|2)[0-9]))
50
        )
51
        T
52
        ([0-1][0-9]|2[0-4])
53
        :(0[0-9]|[1-5][0-9])
54
        :(0[0-9]|[1-5][0-9])
55
        (\.[0-9]{0,6})?
56
        (
57
            [+-]([0-1][0-9]|2[0-4])
58
            :(0[0-9]|[1-5][0-9])
59
            |Z
60
        )?
61
        $/Dx';
62
63
64
    /**
65
     * @param string $value
66
     * @param string $message
67
     */
68
    protected static function validDateTime(string $value, string $message = ''): void
69
    {
70
        parent::regex(
71
            $value,
72
            self::$datetime_regex,
73
            $message ?: '%s is not a valid xs:dateTime',
74
            InvalidArgumentException::class,
75
        );
76
    }
77
}
78