DurationTrait::validDuration()   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 DurationTrait
13
{
14
    /** @var string */
15
    private static string $duration_regex = '/^
16
        ([-+]?)
17
        P
18
        (?!$)
19
        (?:(?<years>\d+(?:[\.\,]\d+)?)Y)?
20
        (?:(?<months>\d+(?:[\.\,]\d+)?)M)?
21
        (?:(?<weeks>\d+(?:[\.\,]\d+)?)W)?
22
        (?:(?<days>\d+(?:[\.\,]\d+)?)D)?
23
        (T(?=\d)(?:(?<hours>\d+(?:[\.\,]\d+)?)H)?
24
        (?:(?<minutes>\d+(?:[\.\,]\d+)?)M)?
25
        (?:(?<seconds>\d+(?:[\.\,]\d+)?)S)?)?
26
        $/Dx';
27
28
29
    /**
30
     * @param string $value
31
     * @param string $message
32
     */
33
    protected static function validDuration(string $value, string $message = ''): void
34
    {
35
        parent::regex(
36
            $value,
37
            self::$duration_regex,
38
            $message ?: '%s is not a valid xs:duration',
39
            InvalidArgumentException::class,
40
        );
41
    }
42
}
43