DateTime   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 3
cbo 1
dl 0
loc 139
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getDateType() 0 4 1
A setDateType() 0 10 2
A getShort() 0 4 1
A getLong() 0 4 1
A getUnixTimestamp() 0 8 2
A getGmtDateTime() 0 7 1
A isEqualTo() 0 4 1
A isBefore() 0 4 1
A isToday() 0 6 1
A isAfter() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Arbor\Moment;
6
7
use Arbor\Moment\Formatter\DateTimeFormatter;
8
9
/**
10
 * Class DateTime
11
 *
12
 * @package Arbor\Moment
13
 * @author Igor Vuckovic <[email protected]>
14
 */
15
class DateTime extends \DateTime
16
{
17
    const ISO_8601_NO_TIMEZONE = 'Y-m-d H:i:s';
18
    const ISO_8601_DATE_ONLY = 'Y-m-d';
19
20
    const FORMAT_LONG = 'd F Y';
21
    const FORMAT_LONG_DAY_OF_WEEK = 'l, d F Y';
22
    const FORMAT_SHORT = 'd M Y';
23
    const FORMAT_SHORT_DAY_OF_WEEK = 'D, d M Y';
24
    const FORMAT_TIME = 'H:i';
25
26
    const TYPE_DATETIME = 'datetime';
27
    const TYPE_DATE = 'date';
28
29
    protected $dateType;
30
31
    /** @var DateTimeFormatter */
32
    private $formatter;
33
34
    /**
35
     * @param string $time
36
     * @param \DateTimeZone $timezone
37
     * @param string $dateType
38
     */
39 45
    public function __construct(string $time = 'now', \DateTimeZone $timezone = null, string $dateType = self::TYPE_DATETIME)
40
    {
41 45
        parent::__construct($time, $timezone);
42
43 45
        $this->setDateType($dateType);
44 45
        $this->formatter = new DateTimeFormatter($this);
45 45
    }
46
47
    /**
48
     * @return string
49
     */
50 37
    public function getDateType() : string
51
    {
52 37
        return $this->dateType;
53
    }
54
55
    /**
56
     * @param $dateType
57
     * @return DateTime
58
     */
59 45
    public function setDateType($dateType) : DateTime
60
    {
61 45
        if ($dateType === self::TYPE_DATE) {
62 16
            $this->setTime(0, 0);
63
        }
64
65 45
        $this->dateType = $dateType;
66
67 45
        return $this;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 24
    public function getShort() : string
74
    {
75 24
        return $this->formatter->getShort();
76
    }
77
78
    /**
79
     * @return string
80
     */
81 35
    public function getLong() : string
82
    {
83 35
        return $this->formatter->getLong();
84
    }
85
86
    /**
87
     * @return string
88
     */
89 5
    public function getUnixTimestamp() : string
90
    {
91 5
        if ($this->dateType === self::TYPE_DATE) {
92 3
            return $this->format(self::ISO_8601_DATE_ONLY);
93
        }
94
95 2
        return $this->format(self::ISO_8601_NO_TIMEZONE);
96
    }
97
98
    /**
99
     * @return DateTime
100
     */
101 2
    public function getGmtDateTime() : DateTime
102
    {
103 2
        $gmtDate = clone $this;
104 2
        $gmtDate->setTimezone(new \DateTimeZone('Europe/London'));
105
106 2
        return $gmtDate;
107
    }
108
109
    /**
110
     * @param DateTime $dateTime
111
     * @return bool
112
     */
113 4
    public function isEqualTo(DateTime $dateTime) : bool
114
    {
115 4
        return $this->getTimestamp() === $dateTime->getTimestamp();
116
    }
117
118
    /**
119
     * @param DateTime $date
120
     * @return bool
121
     */
122 4
    public function isBefore(DateTime $date) : bool
123
    {
124 4
        return $this->getTimestamp() < $date->getTimestamp();
125
    }
126
127
    /**
128
     * @return bool
129
     */
130 2
    public function isToday() : bool
131
    {
132 2
        $today = new self('now', $this->getTimezone(), self::TYPE_DATE);
133
134 2
        return $this->getShort() === $today->getShort();
135
    }
136
137
    /**
138
     * @param DateTime $date
139
     * @return bool
140
     */
141 4
    public function isAfter(DateTime $date) : bool
142
    {
143 4
        return $this->getTimestamp() > $date->getTimestamp();
144
    }
145
146
    /**
147
     * @return string
148
     */
149 6
    public function __toString() : string
150
    {
151 6
        return $this->getLong();
152
    }
153
}
154