DateInterval   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getDateTimeInterval() 0 3 1
A fromDateTimeInterval() 0 19 5
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 DateInterval
17
{
18
    private $interval;
19
20
    public function __construct(string $interval)
21
    {
22
        $this->interval = new DateTimeInterval($interval);
23
        $this->interval->h = 0;
24
        $this->interval->i = 0;
25
        $this->interval->s = 0;
26
        $this->interval->f = 0;
27
    }
28
29
    public function format(string $format) : string
30
    {
31
        $format = preg_replace('/(?<!%)(%H|%h|%I|%i|%S|%s|%F|%f)/', '%$1', $format);
32
33
        return $this->interval->format($format);
34
    }
35
36
    public function getDateTimeInterval() : DateTimeInterval
37
    {
38
        return $this->interval;
39
    }
40
41
    public static function fromDateTimeInterval(DateTimeInterval $interval) : DateInterval
42
    {
43
        $string = 'P';
44
45
        if ($interval->days) {
46
            $string .= $interval->days . 'D';
47
        } else {
48
            if ($interval->y) {
49
                $string .= $interval->y . 'Y';
50
            }
51
            if ($interval->m) {
52
                $string .= $interval->m . 'M';
53
            }
54
            if ($interval->d) {
55
                $string .= $interval->d . 'D';
56
            }
57
        }
58
59
        return new self($string);
60
    }
61
}
62