Passed
Pull Request — master (#71)
by ignace nyamagana
02:49
created

Duration::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
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 const FILTER_VALIDATE_INT;
19
20
/**
21
 * League Period Duration.
22
 *
23
 * @package League.period
24
 * @author  Ignace Nyamagana Butera <[email protected]>
25
 * @since   4.2.0
26
 */
27
final class Duration extends DateInterval
28
{
29
    /**
30
     * Returns a continuous portion of time between two datepoints expressed as a DateInterval object.
31
     *
32
     * The duration can be
33
     * <ul>
34
     * <li>an Period object</li>
35
     * <li>a DateInterval object</li>
36
     * <li>an integer interpreted as the duration expressed in seconds.</li>
37
     * <li>a string parsable by DateInterval::createFromDateString</li>
38
     * </ul>
39
     *
40
     * @param mixed $duration a continuous portion of time
41
     */
42 201
    public static function create($duration): DateInterval
43
    {
44 201
        if ($duration instanceof Period) {
45 3
            return $duration->getDateInterval();
46
        }
47
48 198
        if ($duration instanceof DateInterval) {
49 75
            return $duration;
50
        }
51
52 123
        if (false !== ($second = filter_var($duration, FILTER_VALIDATE_INT))) {
53 39
            return new self('PT'.$second.'S');
54
        }
55
56 84
        return self::createFromDateString($duration);
57
    }
58
}
59