TimeInterval::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * Copyright (c) DateTime-Contributors
7
 *
8
 * Licensed under the MIT License. See LICENSE.md file in the project root
9
 * for full license information.
10
 */
11
12
namespace DateTime;
13
14
use DateInterval as DateTimeInterval;
15
16
class TimeInterval
17
{
18
    private $interval;
19
20
    public function __construct(string $interval)
21
    {
22
        $this->interval = new DateTimeInterval($interval);
23
        $this->interval->d = 0;
24
        $this->interval->m = 0;
25
        $this->interval->y = 0;
26
        $this->interval->days = 0;
27
    }
28
29
    public function getDateTimeInterval() : DateTimeInterval
30
    {
31
        return $this->interval;
32
    }
33
34
    public static function fromDateTimeInterval(DateTimeInterval $interval) : TimeInterval
35
    {
36
        $string = 'PT';
37
38
        if ($interval->h) {
39
            $string .= $interval->h . 'H';
40
        }
41
        if ($interval->i) {
42
            $string .= $interval->i . 'M';
43
        }
44
        if ($interval->s) {
45
            $string .= $interval->s . 'S';
46
        }
47
48
        return new self($string);
49
    }
50
51
    public function format(string $format) : string
52
    {
53
        $format = preg_replace('/(?<!%)(%Y|%y|%M|%m|%D|%d|%a)/', '%$1', $format);
54
55
        return $this->interval->format($format);
56
    }
57
}
58