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

Datepoint::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 15
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 DateTime;
17
use DateTimeImmutable;
18
use DateTimeInterface;
19
use function filter_var;
20
use const FILTER_VALIDATE_INT;
21
22
/**
23
 * League Period Datepoint.
24
 *
25
 * @package League.period
26
 * @author  Ignace Nyamagana Butera <[email protected]>
27
 * @since   4.2.0
28
 */
29
final class Datepoint extends DateTimeImmutable
30
{
31
    /**
32
     * Returns a position in time expressed as a DateTimeImmutable object.
33
     *
34
     * A datepoint can be
35
     * <ul>
36
     * <li>a DateTimeInterface object
37
     * <li>a integer interpreted as a timestamp
38
     * <li>a string parsable by DateTime::__construct
39
     * </ul>
40
     *
41
     * @param mixed $datepoint a position in time
42
     */
43
    public static function create($datepoint): DateTimeImmutable
44
    {
45
        if ($datepoint instanceof DateTimeImmutable) {
46
            return $datepoint;
47
        }
48
49
        if ($datepoint instanceof DateTime) {
50
            return self::createFromMutable($datepoint);
51
        }
52
53
        if (false !== ($timestamp = filter_var($datepoint, FILTER_VALIDATE_INT))) {
54
            return new self('@'.$timestamp);
55
        }
56
57
        return new self($datepoint);
58
    }
59
}
60