Passed
Push — master ( 8d94c2...c3251a )
by Gabor
07:26
created

DateTime::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 2
crap 3
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
use WebHemi\I18n\TimeZoneInterface;
19
use WebHemi\StringLib;
20
21
/**
22
 * Class DateTime.
23
 *
24
 * Under php5-fpm the default DateTime object will be instantiated as TimeCopDateTime which makes issues in the
25
 * dependency injection adapter, and also fails unit tests. So the idea is to create this empty extension and
26
 * use this class instead of the default one or the TimeCopDateTime.
27
 */
28
class DateTime extends PHPDateTime implements TimeZoneInterface
29
{
30
    /** @var string */
31
    private $timeZoneDataPath;
32
    /** @var array */
33
    private $dateFormat = [];
34
35
    /**
36
     * DateTime constructor.
37
     *
38
     * @param string $time
39
     * @param DateTimeZone|null $timeZone
40
     */
41 32
    public function __construct($time = 'now', DateTimeZone $timeZone = null)
42
    {
43 32
        if (is_numeric($time)) {
44 2
            $time = date('Y-m-d H:i:s', $time);
45
        }
46
47 32
        if (empty($timeZone)) {
48 30
            $currentTimeZone = new DateTimeZone(date_default_timezone_get());
49
        } else {
50 3
            $currentTimeZone = $timeZone;
51
        }
52
53 32
        $this->timeZoneDataPath = __DIR__.'/I18n/TimeZone';
54 32
        $this->loadTimeZoneData($currentTimeZone->getName());
55
56 32
        parent::__construct($time, $currentTimeZone);
57 32
    }
58
59
    /**
60
     * Loads date format data regarding to the time zone.
61
     *
62
     * @param string $timeZone
63
     */
64 32
    public function loadTimeZoneData(string $timeZone) : void
65
    {
66 32
        $normalizedTimeZone = StringLib::convertNonAlphanumericToUnderscore($timeZone, '-');
67
68 32
        if (file_exists($this->timeZoneDataPath.'/'.$normalizedTimeZone.'.php')) {
69 31
            $this->dateFormat = include $this->timeZoneDataPath.'/'.$normalizedTimeZone.'.php';
70
        }
71 32
    }
72
73
    /**
74
     * Returns date formatted according to given format.
75
     * @param string $format
76
     * @return string
77
     * @link http://php.net/manual/en/datetime.format.php
78
     */
79 21
    public function format($format)
80
    {
81 21
        if (isset($this->dateFormat[$format])) {
82 1
            $timestamp = $this->getTimestamp();
83 1
            $dateString = strftime($this->dateFormat[$format], $timestamp);
84
        } else {
85 21
            $dateString = parent::format($format);
86
        }
87
88 21
        return $dateString;
89
    }
90
91
    /**
92
     * Checks if stored timestamp belong to current day.
93
     *
94
     * @return bool
95
     */
96 4
    public function isToday() : bool
97
    {
98 4
        return date('Ymd', $this->getTimestamp()) == date('Ymd');
99
    }
100
101
    /**
102
     * Checks if stored timestamp belong to current month.
103
     *
104
     * @return bool
105
     */
106 4
    public function isCurrentMonth() : bool
107
    {
108 4
        return date('Ym', $this->getTimestamp()) == date('Ym');
109
    }
110
111
    /**
112
     * Checks if stored timestamp belong to current year.
113
     *
114
     * @return bool
115
     */
116 4
    public function isCurrentYear() : bool
117
    {
118 4
        return date('Y', $this->getTimestamp()) == date('Y');
119
    }
120
121
    /**
122
     * Returns the date with the default format.
123
     *
124
     * @return string
125
     */
126 1
    public function __toString() : string
127
    {
128 1
        return $this->format('Y-m-d H:i:s');
129
    }
130
}
131