Passed
Push — master ( f802e2...a1b288 )
by Sebastian
02:28
created

ConvertHelper_DateInterval   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 133
rs 10
wmc 13

12 Methods

Rating   Name   Duplication   Size   Complexity  
A fromInterval() 0 3 1
A __construct() 0 20 2
A getYears() 0 3 1
A getHours() 0 3 1
A getDays() 0 3 1
A getToken() 0 3 1
A getMonths() 0 3 1
A getTotalSeconds() 0 3 1
A getSeconds() 0 3 1
A getMinutes() 0 3 1
A getInterval() 0 3 1
A fromSeconds() 0 3 1
1
<?php
2
/**
3
 * File containing the {@see \AppUtils\ConvertHelper_DateInterval} class.
4
 * 
5
 * @package Application Utils
6
 * @subpackage ConvertHelper
7
 * @see \AppUtils\ConvertHelper_DateInterval
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * DateInterval wrapper, that makes it much easier to
16
 * work with intervals. The methods are typed, so no
17
 * conversions are necessary. A number of utility methods
18
 * also help.
19
 * 
20
 * Automatically fixes the issue of date interval properties
21
 * not being populated entirely when it is created using
22
 * a format string.
23
 * 
24
 * @package Application Utils
25
 * @subpackage ConvertHelper
26
 * @author Sebastian Mordziol <[email protected]>
27
 * @see \AppUtils\parseInterval()
28
 */
29
class ConvertHelper_DateInterval
30
{
31
    const ERROR_CANNOT_GET_DATE_DIFF = 43601;
32
    
33
    const TOKEN_SECONDS = 's';
34
    const TOKEN_MINUTES = 'i';
35
    const TOKEN_HOURS = 'h';
36
    const TOKEN_DAYS = 'd';
37
    const TOKEN_MONTHS = 'm';
38
    const TOKEN_YEARS = 'y';
39
    
40
   /**
41
    * @var \DateInterval
42
    */
43
    protected $interval;
44
    
45
   /**
46
    * @var int
47
    */
48
    protected $seconds;
49
    
50
    protected function __construct(int $seconds)
51
    {
52
        $this->seconds = $seconds;
53
        
54
        $d1 = new \DateTime();
55
        $d2 = new \DateTime();
56
        $d2->add(new \DateInterval('PT'.$this->seconds.'S'));
57
        
58
        $interval = $d2->diff($d1);
59
        
60
        if($interval === false) 
61
        {
62
            throw new ConvertHelper_Exception(
63
                'Cannot create interval',
64
                sprintf('Getting the date diff failed to retrieve the interval for [%s] seconds.', $this->seconds),
65
                self::ERROR_CANNOT_GET_DATE_DIFF
66
            );
67
        }
68
        
69
        $this->interval = $interval;
70
    }
71
    
72
   /**
73
    * Creates the interval from a specific amount of seconds.
74
    * 
75
    * @param int $seconds
76
    * @return \AppUtils\ConvertHelper_DateInterval
77
    */
78
    public static function fromSeconds(int $seconds)
79
    {
80
        return new ConvertHelper_DateInterval($seconds);
81
    }
82
    
83
   /**
84
    * Creates the interval from an existing regular interval instance.
85
    * 
86
    * @param \DateInterval $interval
87
    * @return \AppUtils\ConvertHelper_DateInterval
88
    */
89
    public static function fromInterval(\DateInterval $interval)
90
    {
91
        return self::fromSeconds(ConvertHelper::interval2seconds($interval));
92
    }
93
    
94
   /**
95
    * Retrieves the PHP native date interval.
96
    * 
97
    * @return \DateInterval
98
    */
99
    public function getInterval() : \DateInterval
100
    {
101
        return $this->interval;
102
    }
103
    
104
    public function getSeconds() : int
105
    {
106
        return $this->getToken(self::TOKEN_SECONDS);
107
    }
108
    
109
    public function getHours() : int
110
    {
111
        return $this->getToken(self::TOKEN_HOURS);
112
    }
113
    
114
    public function getMinutes() : int
115
    {
116
        return $this->getToken(self::TOKEN_MINUTES);
117
    }
118
    
119
    public function getDays() : int
120
    {
121
        return $this->getToken(self::TOKEN_DAYS);
122
    }
123
    
124
    public function getMonths() : int
125
    {
126
        return $this->getToken(self::TOKEN_MONTHS);
127
    }
128
    
129
    public function getYears() : int
130
    {
131
        return $this->getToken(self::TOKEN_YEARS);
132
    }
133
    
134
   /**
135
    * Retrieves a specific time token, e.g. "h" (for hours).
136
    * Using the constants to specifiy the tokens is recommended.
137
    * 
138
    * @param string $token
139
    * @return int
140
    * 
141
    * @see ConvertHelper_DateInterval::TOKEN_SECONDS
142
    * @see ConvertHelper_DateInterval::TOKEN_MINUTES
143
    * @see ConvertHelper_DateInterval::TOKEN_HOURS
144
    * @see ConvertHelper_DateInterval::TOKEN_DAYS
145
    * @see ConvertHelper_DateInterval::TOKEN_MONTHS
146
    * @see ConvertHelper_DateInterval::TOKEN_YEARS
147
    */
148
    public function getToken(string $token) : int
149
    {
150
        return (int)$this->interval->$token;
151
    }
152
    
153
   /**
154
    * The total amount of seconds in the interval (including
155
    * everything, from seconds to days, months, years...).
156
    * 
157
    * @return int
158
    */
159
    public function getTotalSeconds() : int
160
    {
161
        return $this->seconds;
162
    }
163
}
164