Completed
Push — master ( 674066...0b0354 )
by WEBEWEB
03:44
created

DateTimeHelper::getWeekNumberToApply()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.3554
c 0
b 0
f 0
cc 5
nc 3
nop 4
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 greater than date/time B
33
     *  1: if the date/time A is lesser than date/time B
34
     *  0: if the date/time are equals.
35
     */
36
    public static function compare(DateTime $a, DateTime $b) {
37
        $timestampA = $a->getTimestamp();
38
        $timestampB = $b->getTimestamp();
39
        if ($timestampA < $timestampB) {
40
            return -1;
41
        }
42
        if ($timestampB < $timestampA) {
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 a month number.
71
     *
72
     * @param DateTime $dateTime The date/time.
73
     * @return integer Returns the month number.
74
     */
75
    public static function getMonthNumber(DateTime $dateTime) {
76
        return intval($dateTime->format("m"));
77
    }
78
79
    /**
80
     * Get a week number.
81
     *
82
     * @param DateTime $dateTime The date/time.
83
     * @return integer Returns the week number.
84
     */
85
    public static function getWeekNumber(DateTime $dateTime) {
86
        return intval($dateTime->format("W"));
87
    }
88
89
    /**
90
     * Get a week number to apply with a schedule.
91
     *
92
     * <p>
93
     * For example:
94
     * We have a schedule etablished over 5 weeks.
95
     *
96
     * We start the schedule with the week number 1.
97
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 1
98
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 2
99
     * etc.
100
     *
101
     * We start the schedule with the week number 3.
102
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 3
103
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 4
104
     * etc.
105
     * </p>
106
     *
107
     * @param DateTime $date The date.
108
     * @param DateTime $startDate The start date.
109
     * @param integer $weekCount The week count.
110
     * @param integer $weekOffset The week offset.
111
     * @return integer Returns the week number to apply between 1 and $weekCount.
112
     */
113
    public static function getWeekNumberToApply(DateTime $date, DateTime $startDate, $weekCount, $weekOffset = 1) {
114
115
        // Check the week arguments.
116
        if ($weekCount <= 0 || $weekOffset <= 0 || $weekCount < $weekOffset) {
117
            return -1;
118
        }
119
120
        // Calculate.
121
        $result = intval($date->diff($startDate)->d / 7);
122
        $result %= $weekCount;
123
        $result += $weekOffset;
124
        if ($weekCount < $result) {
125
            $result -= $weekCount;
126
        }
127
128
        // Return.
129
        return $result;
130
    }
131
132
    /**
133
     * Get a year number.
134
     *
135
     * @param DateTime $dateTime The date/time.
136
     * @return integer Returns the year number.
137
     */
138
    public static function getYearNumber(DateTime $dateTime) {
139
        return intval($dateTime->format("Y"));
140
    }
141
142
    /**
143
     * Determines if a value is a date.
144
     *
145
     * @param mixed $value The value.
146
     * @throws DateArgumentException Throws a Date argument exception if the value is not of expected type.
147
     */
148
    public static function isDate($value) {
149
        if (false === strtotime($value)) {
150
            throw new DateArgumentException($value);
151
        }
152
    }
153
154
    /**
155
     * Translate the weekday part.
156
     *
157
     * @param string $date The date.
158
     * @param string $locale The locale.
159
     * @return string Returns the weekday part translated.
160
     */
161
    public static function translateWeekday($date, $locale = "en") {
162
163
        // Initialize.
164
        $template = __DIR__ . "/../../Resources/translations/messages.%locale%.yml";
165
        $filename = str_replace("%locale%", $locale, $template);
166
167
        // Check if the filename exists.
168
        if (false === file_exists($filename)) {
169
            $filename = str_replace("%locale%", "en", $template);
170
        }
171
172
        // Parse the translations.
173
        $translations = Yaml::parse(file_get_contents($filename));
174
175
        // Return the weekday part translated.
176
        return str_ireplace(array_keys($translations["weekdays"]), array_values($translations["weekdays"]), $date);
177
    }
178
179
}
180