|
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 function filter_var; |
|
18
|
|
|
use function property_exists; |
|
19
|
|
|
use const FILTER_VALIDATE_INT; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* League Period Duration. |
|
23
|
|
|
* |
|
24
|
|
|
* @package League.period |
|
25
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
26
|
|
|
* @since 4.2.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
final class Duration extends DateInterval |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Returns a continuous portion of time between two datepoints expressed as a DateInterval object. |
|
32
|
|
|
* |
|
33
|
|
|
* The duration can be |
|
34
|
|
|
* <ul> |
|
35
|
|
|
* <li>an Period object</li> |
|
36
|
|
|
* <li>a DateInterval object</li> |
|
37
|
|
|
* <li>an integer interpreted as the duration expressed in seconds.</li> |
|
38
|
|
|
* <li>a string parsable by DateInterval::createFromDateString</li> |
|
39
|
|
|
* </ul> |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $duration a continuous portion of time |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function create($duration): self |
|
44
|
|
|
{ |
|
45
|
|
|
if ($duration instanceof self) { |
|
46
|
|
|
return $duration; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($duration instanceof Period) { |
|
50
|
|
|
$duration = $duration->getDateInterval(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($duration instanceof DateInterval) { |
|
54
|
|
|
$new = new self('PT0S'); |
|
55
|
|
|
foreach ($duration as $name => $value) { |
|
56
|
|
|
if (property_exists($new, $name)) { |
|
57
|
|
|
$new->$name = $value; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $new; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (false !== ($second = filter_var($duration, FILTER_VALIDATE_INT))) { |
|
65
|
|
|
return new self('PT'.$second.'S'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return self::createFromDateString($duration); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
* |
|
74
|
|
|
* @param mixed $duration a date with relative parts |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function createFromDateString($duration): self |
|
77
|
|
|
{ |
|
78
|
|
|
$duration = parent::createFromDateString($duration); |
|
79
|
|
|
$new = new self('PT0S'); |
|
80
|
|
|
foreach ($duration as $name => $value) { |
|
81
|
|
|
$new->$name = $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $new; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|