Completed
Push — master ( e47fb6...06657c )
by Andreas
01:49
created

TimeInterval   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDateTimeInterval() 0 3 1
A __construct() 0 7 1
A fromDateTimeInterval() 0 15 4
A format() 0 5 1
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
59
}
60