|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* League.Period (https://period.thephpleague.com). |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ignace Nyamagana Butera <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace League\Period; |
|
15
|
|
|
|
|
16
|
|
|
use DateInterval; |
|
17
|
|
|
use TypeError; |
|
18
|
|
|
use function filter_var; |
|
19
|
|
|
use function get_class; |
|
20
|
|
|
use function gettype; |
|
21
|
|
|
use function is_object; |
|
22
|
|
|
use function is_string; |
|
23
|
|
|
use function sprintf; |
|
24
|
|
|
use const FILTER_VALIDATE_INT; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* League Period Duration. |
|
28
|
|
|
* |
|
29
|
|
|
* @package League.period |
|
30
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
31
|
|
|
* @since 4.2.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
final class Duration |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @codeCoverageIgnore |
|
37
|
|
|
*/ |
|
38
|
|
|
private function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns a continuous portion of time between two datepoints expressed as a DateInterval object. |
|
44
|
|
|
* |
|
45
|
|
|
* The duration can be |
|
46
|
|
|
* <ul> |
|
47
|
|
|
* <li>an Period object</li> |
|
48
|
|
|
* <li>a DateInterval object</li> |
|
49
|
|
|
* <li>an integer interpreted as the duration expressed in seconds.</li> |
|
50
|
|
|
* <li>a string parsable by DateInterval::createFromDateString</li> |
|
51
|
|
|
* </ul> |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $duration a continuous portion of time |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function create($duration): DateInterval |
|
56
|
|
|
{ |
|
57
|
|
|
if ($duration instanceof Period) { |
|
58
|
|
|
return $duration->getDateInterval(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($duration instanceof DateInterval) { |
|
62
|
|
|
return $duration; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (false !== ($second = filter_var($duration, FILTER_VALIDATE_INT))) { |
|
66
|
|
|
return new DateInterval('PT'.$second.'S'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (is_string($duration)) { |
|
70
|
|
|
return DateInterval::createFromDateString($duration); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
throw new TypeError(sprintf( |
|
74
|
|
|
'The duration must be expressed using an integer, a string, a DateInterval or a Period object %s given', |
|
75
|
|
|
is_object($duration) ? get_class($duration) : gettype($duration) |
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|