TimePeriods::tomorrow()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sfneal\Helpers\Time;
4
5
class TimePeriods
6
{
7
    /**
8
     * This Month.
9
     *
10
     * @param $format string Date format
11
     * @return array
12
     */
13
    public static function thisMonth(string $format = 'Y-m-d'): array
14
    {
15
        return self::getPeriod(
16
            date($format, strtotime('first day of this month')).' 00:00:00',
17
            date($format, strtotime('last day of this month')).' 23:59:59'
18
        );
19
    }
20
21
    /**
22
     * Last Month.
23
     *
24
     * @param $format string Date format
25
     * @return array
26
     */
27
    public static function lastMonth(string $format = 'Y-m-d'): array
28
    {
29
        return self::getPeriod(
30
            date($format, strtotime('first day of last month')).' 00:00:00',
31
            date($format, strtotime('last day of last month')).' 23:59:59'
32
        );
33
    }
34
35
    /**
36
     * This Week.
37
     *
38
     * @param $format string Date format
39
     * @return array
40
     */
41
    public static function thisWeek(string $format = 'Y-m-d'): array
42
    {
43
        return self::getPeriod(
44
            date($format, strtotime('Monday this week')).' 00:00:00',
45
            date($format, strtotime('Sunday this week')).' 23:59:59'
46
        );
47
    }
48
49
    /**
50
     * Today.
51
     *
52
     * @param $format string Date format
53
     * @return array
54
     */
55
    public static function today(string $format = 'Y-m-d'): array
56
    {
57
        return self::getPeriod(
58
            date($format, strtotime('Today')).' 00:00:00',
59
            date($format, strtotime('Today')).' 23:59:59'
60
        );
61
    }
62
63
    /**
64
     * Yesterday.
65
     *
66
     * @param $format string Date format
67
     * @return array
68
     */
69
    public static function yesterday(string $format = 'Y-m-d'): array
70
    {
71
        return self::getPeriod(
72
            date($format, strtotime('Yesterday')).' 00:00:00',
73
            date($format, strtotime('Yesterday')).' 23:59:59'
74
        );
75
    }
76
77
    /**
78
     * Tomorrow.
79
     *
80
     * @param $format string Date format
81
     * @return array
82
     */
83
    public static function tomorrow(string $format = 'Y-m-d'): array
84
    {
85
        return self::getPeriod(
86
            date($format, strtotime('Tomorrow')).' 00:00:00',
87
            date($format, strtotime('Tomorrow')).' 23:59:59'
88
        );
89
    }
90
91
    /**
92
     * Names of helper methods that don't return time periods.
93
     */
94
    private const HELPER_METHODS = ['all', 'get', 'getPeriod', 'mapMethods', 'timePeriod'];
95
96
    /**
97
     * Retrieve an array time period names and time period ranges.
98
     *
99
     * @param array $exclusions
100
     * @return array
101
     */
102
    public static function all(array $exclusions = []): array
103
    {
104
        // Get all the methods in this class except 'all' & 'getPeriod'
105
        $methods = array_filter(get_class_methods(new self()), function ($method) use ($exclusions) {
106
            return ! in_array($method, array_unique(array_merge(self::HELPER_METHODS, $exclusions)));
107
        });
108
109
        // Method name key, with method results values
110
        return self::mapMethods($methods);
111
    }
112
113
    /**
114
     * Retrieve an array time period names and time period ranges.
115
     *
116
     * @param array $inclusions
117
     * @return array
118
     */
119
    public static function get(...$inclusions): array
120
    {
121
        // Get all the methods in this class except 'all' & 'getPeriod'
122
        $methods = array_filter(get_class_methods(new self()), function ($method) use ($inclusions) {
123
            return in_array($method, $inclusions);
124
        });
125
126
        // Method name key, with method results values
127
        return self::mapMethods($methods);
128
    }
129
130
    /**
131
     * Retrieve a single period range.
132
     *
133
     * @param $period
134
     * @return mixed
135
     */
136
    public static function timePeriod($period)
137
    {
138
        return self::{$period}();
139
    }
140
141
    /**
142
     * Return an array of time periods with 'start' and 'end' keys.
143
     *
144
     * @param string $start
145
     * @param string $end
146
     * @return array
147
     */
148
    private static function getPeriod(string $start, string $end): array
149
    {
150
        return [$start, $end];
151
    }
152
153
    /**
154
     * Return method name key, with method results values.
155
     *
156
     * @param array $methods
157
     * @return array
158
     */
159
    private static function mapMethods(array $methods): array
160
    {
161
        $arr = [];
162
        foreach ($methods as $method) {
163
            $arr[$method] = self::{$method}();
164
        }
165
166
        return $arr;
167
    }
168
}
169