Passed
Push — master ( f1bbc8...8d94c2 )
by Gabor
06:08
created

DateTime::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 2
crap 3.0175
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi;
15
16
use DateTime as PHPDateTime;
17
use DateTimeZone;
18
19
/**
20
 * Class DateTime.
21
 *
22
 * Under php5-fpm the default DateTime object will be instantiated as TimeCopDateTime which makes issues in the
23
 * dependency injection adapter, and also fails unit tests. So the idea is to create this empty extension and
24
 * use this class instead of the default one or the TimeCopDateTime.
25
 */
26
class DateTime extends PHPDateTime
27
{
28
    /**
29
     * TODO take translation data from I18n.
30
     */
31
32
    /** @var string */
33
    private $defaultTimezone;
34
    /** @var array */
35
    private $timezoneToLanguage = [
36
        'Europe/Budapest' => 'hu',
37
        'Europe/London' => 'en'
38
    ];
39
    /** @var array */
40
    private $formatLanguage = [
41
        'Y2MD' => [
42
            'default' => '%y-%m-%d',
43
            'en' => '%m-%d-%y',
44
            'hu' => '%y-%m-%d',
45
        ],
46
        'Y4MD' => [
47
            'default' => '%Y-%m-%d',
48
            'en' => '%m-%d-%Y',
49
            'hu' => '%Y-%m-%d'
50
        ],
51
        'Y4M' => [
52
            'default' => '%Y-%m',
53
            'en' => '%m-%d',
54
            'hu' => '%Y-%m'
55
        ],
56
        'Y4B' => [
57
            'default' => '%Y. %B',
58
            'en' => '%B, %Y',
59
            'hu' => '%Y. %B'
60
        ],
61
        'MD' => [
62
            'default' => '%m-%d',
63
            'en' => '%m-%d',
64
            'hu' => '%m-%d'
65
        ],
66
        'Y4BD' => [
67
            'default' => '%m. %B %Y',
68
            'en' => '%m. %B %Y',
69
            'hu' => '%Y. %B %d.'
70
        ],
71
        'BD' => [
72
            'default' => '%B %d.',
73
            'en' => '%B %d',
74
            'hu' => '%B %d.'
75
        ],
76
        'T' => [
77
            'default' => '%H:%M',
78
            'en' => '%H:%M',
79
            'hu' => '%H:%M',
80
        ],
81
        'TS' => [
82
            'default' => '%H:%M:%S',
83
            'en' => '%H:%M:%S',
84
            'hu' => '%H:%M:%S',
85
        ],
86
        'MDT' => [
87
            'default' => '%m %d., %H:%M',
88
            'en' => '%m %d, %H:%M',
89
            'hu' => '%m %d., %H:%M'
90
        ],
91
        'BDT' => [
92
            'default' => '%B %d., %H:%M',
93
            'en' => '%B %d, %H:%M',
94
            'hu' => '%B %d., %H:%M'
95
        ],
96
        'Y4BDT' => [
97
            'default' => '%m. %B %Y, %H:%M',
98
            'en' => '%m. %B %Y, %H:%M',
99
            'hu' => '%Y. %B %d.,  %H:%M'
100
        ],
101
        'Y4BDTS' => [
102
            'default' => '%m. %B %Y, %H:%M:%S',
103
            'en' => '%m. %B %Y, %H:%M:%S',
104
            'hu' => '%Y. %B %d.,  %H:%M:%S'
105
        ],
106
        'Y4MDTS' => [
107
            'default' => '%Y-%m-%d %H:%M:%S',
108
            'en' => '%m-%d-%Y %H:%M:%S',
109
            'hu' => '%Y-%m-%d %H:%M:%S'
110
        ],
111
    ];
112
113
    /**
114
     * DateTime constructor.
115
     *
116
     * @param string $time
117
     * @param DateTimeZone|null $timezone
118
     */
119 29
    public function __construct($time = 'now', DateTimeZone $timezone = null)
120
    {
121 29
        if (is_numeric($time)) {
122
            $time = date('Y-m-d H:i:s', $time);
123
        }
124
125 29
        $this->defaultTimezone = date_default_timezone_get();
126
127 29
        if (empty($timezone)) {
128 29
            $timezone = new DateTimeZone($this->defaultTimezone);
129
        }
130
131 29
        parent::__construct($time, $timezone);
132 29
    }
133
134
    /**
135
     * Returns date formatted according to given format.
136
     * @param string $format
137
     * @return string
138
     * @link http://php.net/manual/en/datetime.format.php
139
     */
140 19
    public function format($format)
141
    {
142 19
        if (isset($this->formatLanguage[$format])) {
143
            $timestamp = $this->getTimestamp();
144
            $language = $this->timezoneToLanguage[$this->defaultTimezone] ?? 'default';
145
            $dateString = strftime($this->formatLanguage[$format][$language], $timestamp);
146
        } else {
147 19
            $dateString = parent::format($format);
148
        }
149
150 19
        return $dateString;
151
    }
152
153
    /**
154
     * Checks if stored timestamp belong to current day.
155
     *
156
     * @return bool
157
     */
158
    public function isToday() : bool
159
    {
160
        return date('Ymd', $this->getTimestamp()) == date('Ymd');
161
    }
162
163
    /**
164
     * Checks if stored timestamp belong to current month.
165
     *
166
     * @return bool
167
     */
168
    public function isCurrentMonth() : bool
169
    {
170
        return date('Ym', $this->getTimestamp()) == date('Ym');
171
    }
172
173
    /**
174
     * Checks if stored timestamp belong to current year.
175
     *
176
     * @return bool
177
     */
178
    public function isCurrentYear() : bool
179
    {
180
        return date('Y', $this->getTimestamp()) == date('Y');
181
    }
182
183
    /**
184
     * Returns the date with the default format.
185
     *
186
     * @return string
187
     */
188
    public function __toString() : string
189
    {
190
        return $this->format('Y4MDTs');
191
    }
192
}
193