Completed
Push — master ( 92461c...7eeb67 )
by WEBEWEB
01:33
created

DateTimeHelper::compareZone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
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
namespace WBW\Library\Core\Helper\Argument;
13
14
use DateTime;
15
use Symfony\Component\Yaml\Yaml;
16
use WBW\Library\Core\Exception\Argument\DateArgumentException;
17
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
18
19
/**
20
 * Date/time helper.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Library\Core\Helper\Argument
24
 */
25
class DateTimeHelper {
26
27
    /**
28
     * Compare two date/time.
29
     *
30
     * @param DateTime $a The date/time A.
31
     * @param DateTime $b The date/time B.
32
     * @return int Returns
33
     *  -1: if the date/time A is lesser than date/time B
34
     *   0: if the date/time are equals.
35
     *   1: if the date/time A is greater than date/time B
36
     */
37
    public static function compare(DateTime $a, DateTime $b) {
38
        if (true === self::isLessThan($a, $b)) {
39
            return -1;
40
        }
41
        if (true === self::isGreaterThan($a, $b)) {
42
            return 1;
43
        }
44
        return 0;
45
    }
46
47
    /**
48
     * Compare tow date/time zones.
49
     *
50
     * @param DateTime $a The date/time A.
51
     * @param DateTime $b The date/time B.
52
     * @throws IllegalArgumentException Throws an illegal argument exception if the two date/time does not have the same time zone.
53
     */
54
    protected static function compareZone(DateTime $a, DateTime $b) {
55
        if (false === DateTimeZoneHelper::equals($a->getTimezone(), $b->getTimezone())) {
56
            throw new IllegalArgumentException("The two date/times does not have the same time zone");
57
        }
58
    }
59
60
    /**
61
     * Determines if two date/time are equals.
62
     *
63
     * @param DateTime $a The date/time A.
64
     * @param DateTime $b The date/time B.
65
     * @return boolean Returns true in case o success, false otherwise.
66
     */
67
    public static function equals(DateTime $a, DateTime $b) {
68
        return 0 === self::compare($a, $b);
69
    }
70
71
    /**
72
     * Get a day number.
73
     *
74
     * @param DateTime $dateTime The date/time.
75
     * @return integer Returns the day number between 1 and 7 with monday equals to 1.
76
     */
77
    public static function getDayNumber(DateTime $dateTime) {
78
        return intval($dateTime->format("N"));
79
    }
80
81
    /**
82
     * Get the greater date/time.
83
     *
84
     * @param DateTime $a The date/time A.
85
     * @param DateTime $b The date/time B.
86
     * @return DateTime Returns the greater date/time.
87
     */
88
    public static function getGreater(DateTime $a, DateTime $b) {
89
        return 0 <= self::compare($a, $b) ? $a : $b;
90
    }
91
92
    /**
93
     * Get a month number.
94
     *
95
     * @param DateTime $dateTime The date/time.
96
     * @return integer Returns the month number.
97
     */
98
    public static function getMonthNumber(DateTime $dateTime) {
99
        return intval($dateTime->format("m"));
100
    }
101
102
    /**
103
     * Get the smaller date/time.
104
     *
105
     * @param DateTime $a The date/time A.
106
     * @param DateTime $b The date/time B.
107
     * @return DateTime Returns the smaller date/time.
108
     */
109
    public static function getSmaller(DateTime $a, DateTime $b) {
110
        return 0 <= self::compare($a, $b) ? $b : $a;
111
    }
112
113
    /**
114
     * Get a week number.
115
     *
116
     * @param DateTime $dateTime The date/time.
117
     * @return integer Returns the week number.
118
     */
119
    public static function getWeekNumber(DateTime $dateTime) {
120
        return intval($dateTime->format("W"));
121
    }
122
123
    /**
124
     * Get a week number to apply with a schedule.
125
     *
126
     * <p>
127
     * For example:
128
     * We have a schedule etablished over 5 weeks.
129
     *
130
     * We start the schedule with the week number 1.
131
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 1
132
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 2
133
     * etc.
134
     *
135
     * We start the schedule with the week number 3.
136
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 3
137
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 4
138
     * etc.
139
     * </p>
140
     *
141
     * @param DateTime $date The date.
142
     * @param DateTime $startDate The start date.
143
     * @param integer $weekCount The week count.
144
     * @param integer $weekOffset The week offset.
145
     * @return integer Returns the week number to apply between 1 and $weekCount.
146
     */
147
    public static function getWeekNumberToApply(DateTime $date, DateTime $startDate, $weekCount, $weekOffset = 1) {
148
149
        // Check the week arguments.
150
        if ($weekCount <= 0 || $weekOffset <= 0 || $weekCount < $weekOffset) {
151
            return -1;
152
        }
153
154
        // Calculate.
155
        $result = intval($date->diff($startDate)->d / 7);
156
        $result %= $weekCount;
157
        $result += $weekOffset;
158
        if ($weekCount < $result) {
159
            $result -= $weekCount;
160
        }
161
162
        // Return.
163
        return $result;
164
    }
165
166
    /**
167
     * Get a year number.
168
     *
169
     * @param DateTime $dateTime The date/time.
170
     * @return integer Returns the year number.
171
     */
172
    public static function getYearNumber(DateTime $dateTime) {
173
        return intval($dateTime->format("Y"));
174
    }
175
176
    /**
177
     * Determines if a date/time is between date/time A and date/time B.
178
     *
179
     * @param DateTime $dateTime The date/time.
180
     * @param DateTime $a The date/time A.
181
     * @param DateTime $b The date/time B.
182
     * @return bool Returns true in case of success, false otherwise.
183
     */
184
    public static function isBetween(DateTime $dateTime, DateTime $a, DateTime $b) {
185
        self::compareZone($a, $b);
186
        $c1 = $a->getTimestamp() <= $dateTime->getTimestamp();
187
        $c2 = $dateTime->getTimestamp() <= $b->getTimestamp();
188
        return $c1 && $c2;
189
    }
190
191
    /**
192
     * Determines if a value is a date.
193
     *
194
     * @param mixed $value The value.
195
     * @throws DateArgumentException Throws a Date argument exception if the value is not of expected type.
196
     */
197
    public static function isDate($value) {
198
        if (false === strtotime($value)) {
199
            throw new DateArgumentException($value);
200
        }
201
    }
202
203
    /**
204
     * Detremines if date/time A is greater than date/time B.
205
     *
206
     * @param DateTime $a The date/time A.
207
     * @param DateTime $b The date/time B.
208
     * @return bool Returns true in case of success, false otherwise.
209
     */
210
    public static function isGreaterThan(DateTime $a, DateTime $b) {
211
        self::compareZone($a, $b);
212
        return $a->getTimestamp() > $b->getTimestamp();
213
    }
214
215
    /**
216
     * Detremines if date/time A is less than date/time B.
217
     *
218
     * @param DateTime $a The date/time A.
219
     * @param DateTime $b The date/time B.
220
     * @return bool Returns true in case of success, false otherwise.
221
     */
222
    public static function isLessThan(DateTime $a, DateTime $b) {
223
        self::compareZone($a, $b);
224
        return $a->getTimestamp() < $b->getTimestamp();
225
    }
226
227
    /**
228
     * Translate the weekday part.
229
     *
230
     * @param string $date The date.
231
     * @param string $locale The locale.
232
     * @return string Returns the weekday part translated.
233
     */
234
    public static function translateWeekday($date, $locale = "en") {
235
236
        // Initialize.
237
        $template = __DIR__ . "/../../Resources/translations/messages.%locale%.yml";
238
        $filename = str_replace("%locale%", $locale, $template);
239
240
        // Check if the filename exists.
241
        if (false === file_exists($filename)) {
242
            $filename = str_replace("%locale%", "en", $template);
243
        }
244
245
        // Parse the translations.
246
        $translations = Yaml::parse(file_get_contents($filename));
247
248
        // Return the weekday part translated.
249
        return str_ireplace(array_keys($translations["weekdays"]), array_values($translations["weekdays"]), $date);
250
    }
251
252
}
253