Completed
Push — master ( 0b15f4...4aad55 )
by WEBEWEB
04:00
created

DateUtility   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 0
loc 101
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDayNumber() 0 3 1
A getWeekNumber() 0 3 1
A getYearNumber() 0 3 1
B getWeekNumberToApply() 0 21 6
A getWeekdayFR() 0 11 1
A translateWeekday() 0 3 1
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
    public static function getYearNumber(DateTime $date) {
47
        return intval($date->format("Y"));
48
    }
49
50
    /**
51
     * Get a week number to apply with a schedule.
52
     *
53
     * <p>
54
     * For example:
55
     * We have a schedule etablished over 5 weeks.
56
     *
57
     * We start the schedule with the week number 1.
58
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 1
59
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 2
60
     * etc.
61
     *
62
     * We start the schedule with the week number 3.
63
     * If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 3
64
     * If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 4
65
     * etc.
66
     * </p>
67
     *
68
     * @param DateTime $date The date.
69
     * @param DateTime $startDate The start  date.
70
     * @param integer $weekCount The week count.
71
     * @param integer $weekNumber The week number.
72
     * @return integer Returns the week number to apply between 1 and $weekCount.
73
     */
74
    public static function getWeekNumberToApply(DateTime $date, DateTime $startDate, $weekCount, $weekNumber) {
75
76
        // Check the date arguments.
77
        if ($date < $startDate) {
78
            return -1;
79
        }
80
81
        // Check the week arguments.
82
        if ($weekCount <= 0 || $weekNumber <= 0 || $weekCount < $weekNumber) {
83
            return -1;
84
        }
85
86
        // Calculate.
87
        $result = intval($date->diff($startDate)->d / 7) + $weekNumber;
88
        if ($weekCount < $result) {
89
            $result -= $weekCount;
90
        }
91
92
        // Return.
93
        return $result;
94
    }
95
96
    /**
97
     * Get the weekday into FR.
98
     *
99
     * @return array Returns the weekday into FR.
100
     */
101
    public static function getWeekdayFR() {
102
        return [
103
            self::EN_WEEKDAY_SUNDAY    => self::FR_WEEKDAY_SUNDAY,
104
            self::EN_WEEKDAY_MONDAY    => self::FR_WEEKDAY_MONDAY,
105
            self::EN_WEEKDAY_TUESDAY   => self::FR_WEEKDAY_TUESDAY,
106
            self::EN_WEEKDAY_WEDNESDAY => self::FR_WEEKDAY_WEDNESDAY,
107
            self::EN_WEEKDAY_THURSDAY  => self::FR_WEEKDAY_THURSDAY,
108
            self::EN_WEEKDAY_FRIDAY    => self::FR_WEEKDAY_FRIDAY,
109
            self::EN_WEEKDAY_SATURDAY  => self::FR_WEEKDAY_SATURDAY,
110
        ];
111
    }
112
113
    /**
114
     * Translate the weekday part.
115
     *
116
     * @param string $date The date.
117
     * @param array $translations The translations: day => translation.
118
     * @return string Returns the weekday part translated.
119
     */
120
    public static function translateWeekday($date, array $translations = []) {
121
        return StringUtility::replace($date, array_keys($translations), array_values($translations));
122
    }
123
124
}
125