Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1675 lars 1
<?php
2
 
3
namespace LaracraftTech\LaravelDateScopes;
4
 
5
use Illuminate\Database\Eloquent\Builder;
6
 
7
/**
8
 * @method static Builder ofLastUnit(string $dateUnit, int $value, DateRange $customRange = null)
9
 *
10
 * @method static Builder ofJustNow()
11
 * @method static Builder ofLastSecond()
12
 * @method static Builder ofLast15Seconds(DateRange $customRange = null)
13
 * @method static Builder ofLast30Seconds(DateRange $customRange = null)
14
 * @method static Builder ofLast45Seconds(DateRange $customRange = null)
15
 * @method static Builder ofLast60Seconds(DateRange $customRange = null)
16
 * @method static Builder ofLastSeconds(int $seconds, DateRange $customRange = null)
17
 *
18
 * @method static Builder ofLastMinute()
19
 * @method static Builder ofLast15Minutes(DateRange $customRange = null)
20
 * @method static Builder ofLast30Minutes(DateRange $customRange = null)
21
 * @method static Builder ofLast45Minutes(DateRange $customRange = null)
22
 * @method static Builder ofLast60Minutes(DateRange $customRange = null)
23
 * @method static Builder ofLastMinutes(int $minutes, DateRange $customRange = null)
24
 *
25
 * @method static Builder ofLastHour()
26
 * @method static Builder ofLast6Hours(DateRange $customRange = null)
27
 * @method static Builder ofLast12Hours(DateRange $customRange = null)
28
 * @method static Builder ofLast18Hours(DateRange $customRange = null)
29
 * @method static Builder ofLast24Hours(DateRange $customRange = null)
30
 * @method static Builder ofLastHours(int $hours, DateRange $customRange = null)
31
 *
32
 * @method static Builder ofToday()
33
 * @method static Builder ofYesterday()
34
 * @method static Builder ofLast7Days(DateRange $customRange = null)
35
 * @method static Builder ofLast14Days(DateRange $customRange = null)
36
 * @method static Builder ofLast21Days(DateRange $customRange = null)
37
 * @method static Builder ofLast30Days(DateRange $customRange = null)
38
 * @method static Builder ofLastDays(int $days, DateRange $customRange = null)
39
 *
40
 * @method static Builder ofLastWeek()
41
 * @method static Builder ofLast2Weeks(DateRange $customRange = null)
42
 * @method static Builder ofLast3Weeks(DateRange $customRange = null)
43
 * @method static Builder ofLast4Weeks(DateRange $customRange = null)
44
 * @method static Builder ofLastWeeks(int $weeks, DateRange $customRange = null)
45
 *
46
 * @method static Builder ofLastMonth()
47
 * @method static Builder ofLast3Months(DateRange $customRange = null)
48
 * @method static Builder ofLast6Months(DateRange $customRange = null)
49
 * @method static Builder ofLast9Months(DateRange $customRange = null)
50
 * @method static Builder ofLast12Months(DateRange $customRange = null)
51
 * @method static Builder ofLastMonths(int $months, DateRange $customRange = null)
52
 *
53
 * @method static Builder ofLastQuarter()
54
 * @method static Builder ofLast2Quarters(DateRange $customRange = null)
55
 * @method static Builder ofLast3Quarters(DateRange $customRange = null)
56
 * @method static Builder ofLast4Quarters(DateRange $customRange = null)
57
 * @method static Builder ofLastQuarters(int $quarters, DateRange $customRange = null)
58
 *
59
 * @method static Builder ofLastYear()
60
 * @method static Builder ofLastYears(int $years, DateRange $customRange = null)
61
 *
62
 * @method static Builder ofLastDecade()
63
 * @method static Builder ofLastDecades(int $decades, DateRange $customRange = null)
64
 *
65
 * @method static Builder ofLastCentury()
66
 * @method static Builder ofLastCenturies(int $decades, DateRange $customRange = null)
67
 *
68
 * @method static Builder ofLastMillennium()
69
 * @method static Builder ofLastMillenniums(int $millennium, DateRange $customRange = null)
70
 *
71
 * @method static Builder secondToNow()
72
 * @method static Builder minuteToNow()
73
 * @method static Builder hourToNow()
74
 * @method static Builder dayToNow()
75
 * @method static Builder weekToDate()
76
 * @method static Builder monthToDate()
77
 * @method static Builder quarterToDate()
78
 * @method static Builder yearToDate()
79
 * @method static Builder decadeToDate()
80
 * @method static Builder centuryToDate()
81
 * @method static Builder millenniumToDate()
82
 */
83
trait DateScopes
84
{
85
    /**
86
     * For example, if you subtract one month from March 31, you would get February 31, which is not a valid date.
87
     * The subMonthsNoOverflow method for instance would instead return February 28 (or February 29 in a leap year),
88
     * as it adjusts the day to the last day of the month when the resulting date would be invalid.
89
     *
90
     * In Carbon, the date units that can have this "overflow" behavior are months, years, decades, etc. because their lengths can vary.
91
     * Days, hours, minutes, and seconds have fixed lengths, so they do not have this issue.
92
     *
93
     * @var array|string[]
94
     */
95
    private array $fixedLengthDateUnits = [
96
        'second',
97
        'minute',
98
        'hour',
99
        'day',
100
        'week'
101
    ];
102
 
103
    /**
104
     * @param  Builder  $query Eloquent Builder
105
     * @param  string  $dateUnit A valid date unit, such as hour, day, month, year etc...
106
     * @param  int  $value How many hours, days etc. you want to get.
107
     */
108
    public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, DateRange $customRange = null): Builder
109
    {
110
        if ($value <= 0) {
111
            throw DateException::invalidValue();
112
        }
113
 
114
        $dateRange = $customRange ?? DateRange::from(config('date-scopes.default_range'));
115
 
116
        $startFunc = 'startOf'.ucfirst($dateUnit);
117
        $endFunc = 'endOf'.ucfirst($dateUnit);
118
 
119
        $applyNoOverflow = (! in_array($dateUnit, $this->fixedLengthDateUnits)) ? 'NoOverflow' : '' ;
120
        $subFunc = 'sub'.ucfirst($dateUnit).'s'.$applyNoOverflow;
121
        $sub = 1;
122
 
123
        if ($dateRange === DateRange::EXCLUSIVE) {
124
            $range = [
125
                'from' => now()->$subFunc($value)->$startFunc(),
126
                'to' => now()->$subFunc($sub)->$endFunc(),
127
            ];
128
        } else {
129
            $range = [
130
                'from' => now()->$subFunc($value - $sub)->$startFunc(),
131
                'to' => now()->$endFunc(),
132
            ];
133
        }
134
 
135
//        if (defined('DATE_SCOPE_DEBUG'))
136
//            dd(collect($range)->transform(fn ($item) => $item->format('Y-m-d H:i:s'))->toArray());
137
 
138
        $createdColumnName = (self::CREATED_AT != 'created_at') ? self::CREATED_AT : config('date-scopes.created_column');
139
 
140
        return $query->whereBetween($createdColumnName, $range);
141
    }
142
 
143
    // START SECONDS
144
    public function scopeOfJustNow(Builder $query): Builder {return $query->ofLastSeconds(1, DateRange::INCLUSIVE);}
145
    public function scopeOfLastSecond(Builder $query): Builder {return $query->ofLastSeconds(1, DateRange::EXCLUSIVE);}
146
    public function scopeOfLast15Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(15, $customRange);}
147
    public function scopeOfLast30Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(30, $customRange);}
148
    public function scopeOfLast45Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(45, $customRange);}
149
    public function scopeOfLast60Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(60, $customRange);}
150
 
151
    public function scopeOfLastSeconds(Builder $query, int $seconds, DateRange $customRange = null): Builder
152
    {
153
        return $query->ofLastUnit('second', $seconds, $customRange);
154
    }
155
 
156
    // START MINUTES
157
    public function scopeOfLastMinute(Builder $query): Builder {return $query->ofLastMinutes(1, DateRange::EXCLUSIVE);}
158
    public function scopeOfLast15Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(15, $customRange);}
159
    public function scopeOfLast30Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(30, $customRange);}
160
    public function scopeOfLast45Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(45, $customRange);}
161
    public function scopeOfLast60Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(60, $customRange);}
162
 
163
    public function scopeOfLastMinutes(Builder $query, int $minutes, DateRange $customRange = null): Builder
164
    {
165
        return $query->ofLastUnit('minute', $minutes, $customRange);
166
    }
167
 
168
    // START HOURS
169
    public function scopeOfLastHour(Builder $query): Builder {return $query->ofLastHours(1, DateRange::EXCLUSIVE);}
170
    public function scopeOfLast6Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(6, $customRange);}
171
    public function scopeOfLast12Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(12, $customRange);}
172
    public function scopeOfLast18Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(18, $customRange);}
173
    public function scopeOfLast24Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(24, $customRange);}
174
 
175
    public function scopeOfLastHours(Builder $query, int $hours, DateRange $customRange = null): Builder
176
    {
177
        return $query->ofLastUnit('hour', $hours, $customRange);
178
    }
179
 
180
    // START DAYS
181
    public function scopeOfToday(Builder $query): Builder {return $query->ofLastDays(1, DateRange::INCLUSIVE);}
182
    public function scopeOfYesterday(Builder $query): Builder {return $query->ofLastDays(1, DateRange::EXCLUSIVE);}
183
    public function scopeOfLast7Days(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastDays(7, $customRange);}
184
    public function scopeOfLast14Days(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastDays(14, $customRange);}
185
    public function scopeOfLast21Days(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastDays(21, $customRange);}
186
    public function scopeOfLast30Days(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastDays(30, $customRange);}
187
 
188
    public function scopeOfLastDays(Builder $query, int $days, DateRange $customRange = null): Builder
189
    {
190
        return $query->ofLastUnit('day', $days, $customRange);
191
    }
192
 
193
    // START WEEKS
194
    public function scopeOfLastWeek(Builder $query): Builder {return $query->ofLastWeeks(1, DateRange::EXCLUSIVE);}
195
    public function scopeOfLast2Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(2, $customRange);}
196
    public function scopeOfLast3Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(3, $customRange);}
197
    public function scopeOfLast4Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(4, $customRange);}
198
 
199
    public function scopeOfLastWeeks(Builder $query, int $weeks, DateRange $customRange = null): Builder
200
    {
201
        return $query->ofLastUnit('week', $weeks, $customRange);
202
    }
203
 
204
    // START MONTHS
205
    public function scopeOfLastMonth(Builder $query): Builder {return $query->ofLastMonths(1, DateRange::EXCLUSIVE);}
206
    public function scopeOfLast3Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(3, $customRange);}
207
    public function scopeOfLast6Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(6, $customRange);}
208
    public function scopeOfLast9Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(9, $customRange);}
209
    public function scopeOfLast12Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(12, $customRange);}
210
 
211
    public function scopeOfLastMonths(Builder $query, int $months, DateRange $customRange = null): Builder
212
    {
213
        return $query->ofLastUnit('month', $months, $customRange);
214
    }
215
 
216
    // START QUARTER
217
    public function scopeOfLastQuarter(Builder $query): Builder {return $query->ofLastQuarters(1, DateRange::EXCLUSIVE);}
218
    public function scopeOfLast2Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(2, $customRange);}
219
    public function scopeOfLast3Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(3, $customRange);}
220
    public function scopeOfLast4Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(4, $customRange);}
221
 
222
    public function scopeOfLastQuarters(Builder $query, int $quarters, DateRange $customRange = null): Builder
223
    {
224
        return $query->ofLastUnit('quarter', $quarters, $customRange);
225
    }
226
 
227
    // START YEARS
228
    public function scopeOfLastYear(Builder $query): Builder {return $query->ofLastYears(1, DateRange::EXCLUSIVE);}
229
 
230
    public function scopeOfLastYears(Builder $query, int $years, DateRange $customRange = null): Builder
231
    {
232
        return $query->ofLastUnit('year', $years, $customRange);
233
    }
234
 
235
    // START DECADE
236
    public function scopeOfLastDecade(Builder $query): Builder {return $query->ofLastDecades(1, DateRange::EXCLUSIVE);}
237
 
238
    public function scopeOfLastDecades(Builder $query, int $decades, DateRange $customRange = null): Builder
239
    {
240
        return $query->ofLastUnit('decade', $decades, $customRange);
241
    }
242
 
243
    // START CENTURIES
244
    public function scopeOfLastCentury(Builder $query): Builder {return $query->ofLastCenturies(1, DateRange::EXCLUSIVE);}
245
 
246
    public function scopeOfLastCenturies(Builder $query, int $centuries, DateRange $customRange = null): Builder
247
    {
248
        return $query->ofLastUnit('century', $centuries, $customRange);
249
    }
250
 
251
    // START MILLENNIUM
252
    public function scopeOfLastMillennium(Builder $query): Builder {return $query->ofLastMillenniums(1, DateRange::EXCLUSIVE);}
253
 
254
    public function scopeOfLastMillenniums(Builder $query, int $millennium, DateRange $customRange = null): Builder
255
    {
256
        return $query->ofLastUnit('millennium', $millennium, $customRange);
257
    }
258
 
259
    // START CURRENT TO NOW
260
 
261
    public function scopeSecondToNow(Builder $query): Builder {return $query->ofLastSeconds(1, DateRange::INCLUSIVE);}
262
    public function scopeMinuteToNow(Builder $query): Builder {return $query->ofLastMinutes(1, DateRange::INCLUSIVE);}
263
    public function scopeHourToNow(Builder $query): Builder {return $query->ofLastHours(1, DateRange::INCLUSIVE);}
264
    public function scopeDayToNow(Builder $query): Builder {return $query->ofLastDays(1, DateRange::INCLUSIVE);}
265
    public function scopeWeekToDate(Builder $query): Builder {return $query->ofLastWeeks(1, DateRange::INCLUSIVE);}
266
    public function scopeMonthToDate(Builder $query): Builder {return $query->ofLastMonths(1, DateRange::INCLUSIVE);}
267
    public function scopeQuarterToDate(Builder $query): Builder {return $query->ofLastQuarters(1, DateRange::INCLUSIVE);}
268
    public function scopeYearToDate(Builder $query): Builder {return $query->ofLastYears(1, DateRange::INCLUSIVE);}
269
    public function scopeDecadeToDate(Builder $query): Builder {return $query->ofLastDecades(1, DateRange::INCLUSIVE);}
270
    public function scopeCenturyToDate(Builder $query): Builder {return $query->ofLastCenturies(1, DateRange::INCLUSIVE);}
271
    public function scopeMillenniumToDate(Builder $query): Builder {return $query->ofLastMillenniums(1, DateRange::INCLUSIVE);}
272
}