Passed
Push — master ( 37dcaf...03656c )
by Tim
12:27 queued 27s
created

DateTimeTrait::validDateTime()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML\Assert;
6
7
use InvalidArgumentException;
8
9
use function filter_var;
10
use function sprintf;
11
12
/**
13
 * @package simplesamlphp/xml-common
14
 */
15
trait DateTimeTrait
16
{
17
    /** @var string */
18
    private static string $datetime_regex = '/-?[0-9]{4}-(((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))|((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))|(02-(0[1-9]|(1|2)[0-9])))T([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-999])?((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?/i';
19
20
    /***********************************************************************************
21
     *  NOTE:  Custom assertions may be added below this line.                         *
22
     *         They SHOULD be marked as `protected` to ensure the call is forced       *
23
     *          through __callStatic().                                                *
24
     *         Assertions marked `public` are called directly and will                 *
25
     *          not handle any custom exception passed to it.                          *
26
     ***********************************************************************************/
27
28
29
    /**
30
     * @param string $value
31
     * @param string $message
32
     */
33
    protected static function validDateTime(string $value, string $message = ''): void
34
    {
35
        if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$datetime_regex]]) === false) {
36
            throw new InvalidArgumentException(sprintf(
37
                $message ?: '\'%s\' is not a valid xs:dateTime',
38
                $value,
39
            ));
40
        }
41
    }
42
}
43