Completed
Push — master ( dd589b...afca27 )
by WEBEWEB
05:55
created

DateUtility::getWeekNumberToApply()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
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\Utility\Argument;
13
14
use DateTime;
15
use WBW\Library\Core\Utility\Argument\StringUtility;
16
17
/**
18
 * Date utility.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Utility\Argument
22
 * @final
23
 */
24
final class DateUtility implements DateUtilityInterface {
25
26
    /**
27
     * Get a day number.
28
     *
29
     * @param DateTime $date The date.
30
     * @return integer Returns the day number between 1 and 7 with monday equals to 1.
31
     */
32
    public static function getDayNumber(DateTime $date) {
33
        return intval($date->format("N"));
34
    }
35
36
    /**
37
     * Get a week number.
38
     *
39
     * @param DateTime $date The date.
40
     * @return integer Returns the week number.
41
     */
42
    public static function getWeekNumber(DateTime $date) {
43
        return intval($date->format("W"));
44
    }
45
46
    /**
47
     * Get a week number to apply with a schedule.
48
     *
49
     * <p>
50
     * For example:
51
     * We have a schedule etablished over 5 weeks.
52
     *
53
     * We start the schedule with the week number 1.
54
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 1
55
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 2
56
     * etc.
57
     *
58
     * We start the schedule with the week number 3.
59
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 3
60
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 4
61
     * etc.
62
     * </p>
63
     *
64
     * @param DateTime $date The date.
65
     * @param DateTime $startDate The start  date.
66
     * @param integer $weekCount The week count.
67
     * @param integer $weekNumber The week number.
68
     * @return integer Returns the week number to apply between 1 and $weekCount.
69
     */
70
    public static function getWeekNumberToApply(DateTime $date, DateTime $startDate, $weekCount, $weekNumber) {
71
        if ($weekCount <= 0 || $weekNumber <= 0 || $weekCount < $weekNumber) {
72
            return -1;
73
        }
74
        $result = self::getWeekNumber($date) - self::getWeekNumber($startDate) + $weekNumber;
75
        if ($weekCount < $result) {
76
            $result -= $weekCount;
77
        }
78
        return abs($result);
79
    }
80
81
    /**
82
     * Get the weekday into FR.
83
     *
84
     * @return array Returns the weekday into FR.
85
     */
86
    public static function getWeekdayFR() {
87
        return [
88
            self::EN_WEEKDAY_SUNDAY    => self::FR_WEEKDAY_SUNDAY,
89
            self::EN_WEEKDAY_MONDAY    => self::FR_WEEKDAY_MONDAY,
90
            self::EN_WEEKDAY_TUESDAY   => self::FR_WEEKDAY_TUESDAY,
91
            self::EN_WEEKDAY_WEDNESDAY => self::FR_WEEKDAY_WEDNESDAY,
92
            self::EN_WEEKDAY_THURSDAY  => self::FR_WEEKDAY_THURSDAY,
93
            self::EN_WEEKDAY_FRIDAY    => self::FR_WEEKDAY_FRIDAY,
94
            self::EN_WEEKDAY_SATURDAY  => self::FR_WEEKDAY_SATURDAY,
95
        ];
96
    }
97
98
    /**
99
     * Translate the weekday part.
100
     *
101
     * @param string $date The date.
102
     * @param array $translations The translations: day => translation.
103
     * @return string Returns the weekday part translated.
104
     */
105
    public static function translateWeekday($date, array $translations = []) {
106
        return StringUtility::replace($date, array_keys($translations), array_values($translations));
107
    }
108
109
}
110