DateTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A validDate() 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 DateTrait
13
{
14
    /**
15
     * The ·lexical space· of date consists of finite-length sequences of characters of the form:
16
     * '-'? yyyy '-' mm '-' dd zzzzzz?
17
     *
18
     * where the date and optional timezone are represented exactly the same way as they are for dateTime.
19
     * The first moment of the interval is that represented by:
20
     * '-' yyyy '-' mm '-' dd 'T00:00:00' zzzzzz?
21
     *
22
     * and the least upper bound of the interval is the timeline point represented (noncanonically) by:
23
     * '-' yyyy '-' mm '-' dd 'T24:00:00' zzzzzz?.
24
     */
25
    private static string $date_regex  = '/^
26
        -?
27
        ([1-9][0-9]*|[0-9]{4})
28
        -
29
        (
30
            ((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))
31
            |((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))
32
            |(02-(0[1-9]|(1|2)[0-9]))
33
        )
34
        (
35
            ([+-]
36
                ([0-1][0-9]|2[0-4])
37
                :
38
                (0[0-9]|[1-5][0-9])
39
            )|Z
40
        )?$/Dx';
41
42
43
    /**
44
     * @param string $value
45
     * @param string $message
46
     */
47
    protected static function validDate(string $value, string $message = ''): void
48
    {
49
        parent::regex(
50
            $value,
51
            self::$date_regex,
52
            $message ?: '%s is not a valid xs:date',
53
            InvalidArgumentException::class,
54
        );
55
    }
56
}
57