Completed
Push — master ( b93e82...a1a5c6 )
by WEBEWEB
01:50
created

DateTimeHelper::equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
18
/**
19
 * Date/time helper.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Library\Core\Helper\Argument
23
 */
24
class DateTimeHelper {
25
26
    /**
27
     * Compare two date/time.
28
     *
29
     * @param DateTime $a The date/time.
30
     * @param DateTime $b The date/time.
31
     * @return int Returns
32
     *  -1: if the date/time A is lesser than date/time B
33
     *   0: if the date/time are equals.
34
     *   1: if the date/time A is greater than date/time B
35
     */
36
    public static function compare(DateTime $a, DateTime $b) {
37
        $tsA = $a->getTimestamp();
0 ignored issues
show
Unused Code introduced by
$tsA is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
        $tsB = $b->getTimestamp();
0 ignored issues
show
Unused Code introduced by
$tsB is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
        if (true === self::isLessThan($a, $b)) {
40
            return -1;
41
        }
42
        if (true === self::isGreaterThan($a, $b)) {
43
            return 1;
44
        }
45
        return 0;
46
    }
47
48
    /**
49
     * Determines if two date/time are equals.
50
     *
51
     * @param DateTime $a The date/time.
52
     * @param DateTime $b The date/time.
53
     * @return boolean Returns true in case o success, false otherwise.
54
     */
55
    public static function equals(DateTime $a, DateTime $b) {
56
        return 0 === self::compare($a, $b);
57
    }
58
59
    /**
60
     * Get a day number.
61
     *
62
     * @param DateTime $dateTime The date/time.
63
     * @return integer Returns the day number between 1 and 7 with monday equals to 1.
64
     */
65
    public static function getDayNumber(DateTime $dateTime) {
66
        return intval($dateTime->format("N"));
67
    }
68
69
    /**
70
     * Get the greater date/time.
71
     *
72
     * @param DateTime $a The date/time A.
73
     * @param DateTime $b The date/time B.
74
     * @return DateTime Returns the greater date/time.
75
     */
76
    public static function getGreater(DateTime $a, DateTime $b) {
77
        return 0 <= self::compare($a, $b) ? $a : $b;
78
    }
79
80
    /**
81
     * Get a month number.
82
     *
83
     * @param DateTime $dateTime The date/time.
84
     * @return integer Returns the month number.
85
     */
86
    public static function getMonthNumber(DateTime $dateTime) {
87
        return intval($dateTime->format("m"));
88
    }
89
90
    /**
91
     * Get the smaller date/time.
92
     *
93
     * @param DateTime $a The date/time A.
94
     * @param DateTime $b The date/time B.
95
     * @return DateTime Returns the smaller date/time.
96
     */
97
    public static function getSmaller(DateTime $a, DateTime $b) {
98
        return 0 <= self::compare($a, $b) ? $b : $a;
99
    }
100
101
    /**
102
     * Get a week number.
103
     *
104
     * @param DateTime $dateTime The date/time.
105
     * @return integer Returns the week number.
106
     */
107
    public static function getWeekNumber(DateTime $dateTime) {
108
        return intval($dateTime->format("W"));
109
    }
110
111
    /**
112
     * Get a week number to apply with a schedule.
113
     *
114
     * <p>
115
     * For example:
116
     * We have a schedule etablished over 5 weeks.
117
     *
118
     * We start the schedule with the week number 1.
119
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 1
120
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 2
121
     * etc.
122
     *
123
     * We start the schedule with the week number 3.
124
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 3
125
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 4
126
     * etc.
127
     * </p>
128
     *
129
     * @param DateTime $date The date.
130
     * @param DateTime $startDate The start date.
131
     * @param integer $weekCount The week count.
132
     * @param integer $weekOffset The week offset.
133
     * @return integer Returns the week number to apply between 1 and $weekCount.
134
     */
135
    public static function getWeekNumberToApply(DateTime $date, DateTime $startDate, $weekCount, $weekOffset = 1) {
136
137
        // Check the week arguments.
138
        if ($weekCount <= 0 || $weekOffset <= 0 || $weekCount < $weekOffset) {
139
            return -1;
140
        }
141
142
        // Calculate.
143
        $result = intval($date->diff($startDate)->d / 7);
144
        $result %= $weekCount;
145
        $result += $weekOffset;
146
        if ($weekCount < $result) {
147
            $result -= $weekCount;
148
        }
149
150
        // Return.
151
        return $result;
152
    }
153
154
    /**
155
     * Get a year number.
156
     *
157
     * @param DateTime $dateTime The date/time.
158
     * @return integer Returns the year number.
159
     */
160
    public static function getYearNumber(DateTime $dateTime) {
161
        return intval($dateTime->format("Y"));
162
    }
163
164
    /**
165
     * Determines if a date/time is between date/time A and date/time B.
166
     *
167
     * @param DateTime $dateTime The date/time.
168
     * @param DateTime $a The date/time A.
169
     * @param DateTime $b The date/time B.
170
     * @return bool Returns true in case of success, false otherwise.
171
     */
172
    public static function isBetween(DateTime $dateTime, DateTime $a, DateTime $b) {
173
        $c1 = $a->getTimestamp() <= $dateTime->getTimestamp();
174
        $c2 = $dateTime->getTimestamp() <= $b->getTimestamp();
175
        return $c1 && $c2;
176
    }
177
178
    /**
179
     * Determines if a value is a date.
180
     *
181
     * @param mixed $value The value.
182
     * @throws DateArgumentException Throws a Date argument exception if the value is not of expected type.
183
     */
184
    public static function isDate($value) {
185
        if (false === strtotime($value)) {
186
            throw new DateArgumentException($value);
187
        }
188
    }
189
190
    /**
191
     * Detremines if date/time A is greater than date/time B.
192
     *
193
     * @param DateTime $a The date/time A.
194
     * @param DateTime $b The date/time B.
195
     * @return bool Returns true in case of success, false otherwise.
196
     */
197
    public static function isGreaterThan(DateTime $a, DateTime $b) {
198
        return $a->getTimestamp() > $b->getTimestamp();
199
    }
200
201
    /**
202
     * Detremines if date/time A is less than date/time B.
203
     *
204
     * @param DateTime $a The date/time A.
205
     * @param DateTime $b The date/time B.
206
     * @return bool Returns true in case of success, false otherwise.
207
     */
208
    public static function isLessThan(DateTime $a, DateTime $b) {
209
        return $a->getTimestamp() < $b->getTimestamp();
210
    }
211
212
    /**
213
     * Translate the weekday part.
214
     *
215
     * @param string $date The date.
216
     * @param string $locale The locale.
217
     * @return string Returns the weekday part translated.
218
     */
219
    public static function translateWeekday($date, $locale = "en") {
220
221
        // Initialize.
222
        $template = __DIR__ . "/../../Resources/translations/messages.%locale%.yml";
223
        $filename = str_replace("%locale%", $locale, $template);
224
225
        // Check if the filename exists.
226
        if (false === file_exists($filename)) {
227
            $filename = str_replace("%locale%", "en", $template);
228
        }
229
230
        // Parse the translations.
231
        $translations = Yaml::parse(file_get_contents($filename));
232
233
        // Return the weekday part translated.
234
        return str_ireplace(array_keys($translations["weekdays"]), array_values($translations["weekdays"]), $date);
235
    }
236
237
}
238