Completed
Pull Request — master (#2)
by Andreas
02:06
created

DateTime::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (c) Andreas Heigl<[email protected]
5
 *
6
 * Licensed under the MIT License. See LICENSE.md file in the project root
7
 * for full license information.
8
 */
9
10
namespace DateTime;
11
12
use Closure;
13
use DateTime\Timezone\Timezone;
14
use DateTimeImmutable;
15
use DateTimeInterface;
16
17
class DateTime
18
{
19
    private $datetime;
20
21
    public function __construct(string $datetimeString = 'now', Timezone $timezone = null)
22
    {
23
        if (null === $timezone) {
24
            $timezone = Timezone::fromString('UTC');
25
        }
26
        $timezoneThief = function (Timezone $timezone) {
27
            return $timezone->timezone;
0 ignored issues
show
Bug introduced by
The property timezone is declared protected in DateTime\Timezone\Timezone and cannot be accessed from this context.
Loading history...
28
        };
29
        $timezoneThief = Closure::bind($timezoneThief, null, $timezone);
30
31
        $timezone = $timezoneThief($timezone);
32
        $this->datetime = new DateTimeImmutable($datetimeString, $timezone);
33
        $this->datetime->setTimezone($timezone);
34
    }
35
36
    public function add(DateTimeInterval $interval) : self
37
    {
38
        $self = clone($this);
39
        $self->datetime = $self->datetime->add($interval->getDateTimeInterval());
40
41
        return $self;
42
    }
43
44
    public function modify(string $modification) : self
45
    {
46
        $self = clone($this);
47
        $self->datetime = $this->datetime->modify($modification);
48
49
        return $self;
50
    }
51
52
    public function sub(DateTimeInterval $interval) : self
53
    {
54
        $self = clone($this);
55
        $self->datetime = $this->datetime->sub($interval->getDateTimeInterval());
56
57
        return $self;
58
    }
59
60
    public function diff(DateTime $date) : DateTimeInterval
61
    {
62
        return DateTimeInterval::fromDateTimeInterval(
63
            $this->datetime->diff($date->datetime)
64
        );
65
    }
66
67
    public function format(string $format) : string
68
    {
69
        return $this->datetime->format($format);
70
    }
71
72
    public function getTimestamp() : int
73
    {
74
        return $this->datetime->getTimestamp();
75
    }
76
77
    public function getTimezone() : Timezone
78
    {
79
        return Timezone::fromString($this->datetime->getTimezone()->getName());
80
    }
81
82
    public static function createFromFormat(
83
        string $format,
84
        string $time,
85
        TimeZone $timezone = null
86
    ) : self {
87
        if (null === $timezone) {
88
            $timezone = Timezone::fromString('UTC');
89
        }
90
91
        $timezoneThief = function (Timezone $timezone) {
92
            return $timezone->timezone;
0 ignored issues
show
Bug introduced by
The property timezone is declared protected in DateTime\Timezone\Timezone and cannot be accessed from this context.
Loading history...
93
        };
94
        $timezoneThief = Closure::bind($timezoneThief, null, $timezone);
95
        $timezone      = $timezoneThief($timezone);
96
97
        $datetime = DateTimeImmutable::createFromFormat($format, $time, $timezone);
98
99
        return self::createFromPhpDateTime($datetime);
0 ignored issues
show
Bug introduced by
It seems like $datetime can also be of type false; however, parameter $datetime of DateTime\DateTime::createFromPhpDateTime() does only seem to accept DateTimeInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        return self::createFromPhpDateTime(/** @scrutinizer ignore-type */ $datetime);
Loading history...
100
    }
101
102
    public static function createFromPhpDateTime(DateTimeInterface $datetime) : self
103
    {
104
        return new self(
105
            $datetime->format('c'),
106
            Timezone::fromString($datetime->getTimezone()->getName())
107
        );
108
    }
109
}
110