DateTimeTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A validDateTime() 0 7 2
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
     * @var string
35
     */
36
    private static string $datetime_regex = '/^
37
        -?
38
        ([1-9][0-9]*|[0-9]{4})
39
        -
40
        (
41
            (
42
                (0(1|3|5|7|8)|1(0|2))
43
                -
44
                (0[1-9]|(1|2)[0-9]|3[0-1])
45
            )|
46
            (
47
                (0(4|6|9)|11)
48
                -
49
                (0[1-9]|(1|2)[0-9]|30)
50
            )|
51
                (02-(0[1-9]|(1|2)[0-9]))
52
        )
53
        T
54
        ([0-1][0-9]|2[0-4])
55
        :(0[0-9]|[1-5][0-9])
56
        :(0[0-9]|[1-5][0-9])
57
        (\.[0-9]{0,6})?
58
        (
59
            [+-]([0-1][0-9]|2[0-4])
60
            :(0[0-9]|[1-5][0-9])
61
            |Z
62
        )?
63
        $/Dx';
64
65
66
    /**
67
     * @param string $value
68
     * @param string $message
69
     */
70
    protected static function validDateTime(string $value, string $message = ''): void
71
    {
72
        parent::regex(
73
            $value,
74
            self::$datetime_regex,
75
            $message ?: '%s is not a valid xs:dateTime',
76
            InvalidArgumentException::class,
77
        );
78
    }
79
}
80