From 4229cae603a456b377356f8b5250c97f525eb5e0 Mon Sep 17 00:00:00 2001 From: Zacharias Creutznacher Date: Thu, 12 Oct 2023 14:53:14 +0200 Subject: [PATCH] v2 - Add startFrom parameter --- README.md | 68 ++++++++++----- UPGRADING.md | 12 +++ config/date-scopes.php | 2 +- src/DateScopes.php | 178 +++++++++++++++++++++------------------ tests/DataScopesTest.php | 20 +++-- 5 files changed, 170 insertions(+), 110 deletions(-) create mode 100644 UPGRADING.md diff --git a/README.md b/README.md index 1e3c270..f4943e1 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,36 @@ [![License](https://img.shields.io/packagist/l/laracraft-tech/laravel-date-scopes.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-date-scopes) [![Total Downloads](https://img.shields.io/packagist/dt/laracraft-tech/laravel-date-scopes.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-date-scopes) -The package provides a big range of useful date scopes for your Laravel Eloquent models! +This package provides a big range of useful **date scopes** for your Laravel Eloquent models! + +Let's assume you have a `Transaction` model. +If you now give it the `DateScopes` trait, you can do something like this: + +```php +use LaracraftTech\LaravelDateScopes\DateScopes; + +class Transaction extends Model +{ + use DateScopes; +} + +Transaction::ofToday(); // query transactions created today +Transaction::ofLastWeek(); // query transactions created during the last week +Transaction::monthToDate(); // query transactions created during the start of the current month till now +Transaction::ofLastYear(startFrom: '2020-01-01') // query transactions created during the last year, starting from 2020 +// ... and much more scopes are available (see below) + +// For sure, you can chain any Builder function you want here. +// Such as these aggregations, for instance: +Transaction::ofToday()->sum('amount'); +Transaction::ofLastWeek()->avg('amount'); +``` + +## ToC + +- [`Installation`](#installation) +- [`Configuration`](#configuration) +- [`Scopes`](#scopes) ## Installation @@ -57,7 +86,7 @@ return [ * you could use the inclusive range here as a default. * Note that you can also fluently specify the range for quite every scope we offer * directly when using the scope: - * Transaction::ofLast7Days(DateRange::INCLUSIVE); (this works for all but the singular "ofLast"-scopes) + * Transaction::ofLast7Days(customRange: DateRange::INCLUSIVE); (this works for all but the singular "ofLast"-scopes) * This will do an inclusive query, even though the global default range here is set to exclusive. */ 'default_range' => env('DATE_SCOPES_DEFAULT_RANGE', DateRange::EXCLUSIVE->value), @@ -80,14 +109,14 @@ directly when using the scope: ```php // This works for all "ofLast"-scopes, expect the singulars like "ofLastHour", // because it would not make sense for those. -Transaction::ofLast7Days(DateRange::INCLUSIVE); +Transaction::ofLast7Days(customRange: DateRange::INCLUSIVE); ``` This will do an inclusive query (today-6 days), even though the global default range here was set to exclusive. ### Fluent created_at column configuration -If you only want to change the ```created_at``` field in one of your models and not globally just do: +If you only want to change the ```created_at``` field in one of your models and not globally just do: ```php use LaracraftTech\LaravelDateScopes\DateScopes; @@ -100,10 +129,19 @@ class Transaction extends Model const CREATED_AT = 'custom_created_at'; } -// also make sure to omit the defalt $table->timestamps() function in your migration +// also make sure to omit the default $table->timestamps() function in your migration // and use something like this instead: $table->timestamp('custom_created_at')->nullable(); ``` +### Custom start date + +If you want data not starting from now, but from another date, you can do this with: + +```php +// query transactions created during 2019-2020 +Transaction::ofLastYear(startFrom: '2020-01-01') +``` + ## Scopes - [`seconds`](#seconds) @@ -118,18 +156,6 @@ class Transaction extends Model - [`centuries`](#centuries) - [`millenniums`](#millenniums) - [`toNow/toDate`](#toNowtoDate) - -Let's assume you have an `Transaction` model class. -Now when you give it the `DateScopes` trait, you can use the following scopes: - -```php -use LaracraftTech\LaravelDateScopes\DateScopes; - -class Transaction extends Model -{ - use DateScopes; -} -``` ### Seconds @@ -280,7 +306,11 @@ Transaction::millenniumToDate(); // query transactions created during the start composer test ``` -## Changelog +## Upgrading + +Please see [UPGRADING](UPGRADING.md) for details. + +### Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. @@ -288,7 +318,7 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re Please see [CONTRIBUTING](CONTRIBUTING.md) for details. -## Security Vulnerabilities +## Security Please review [our security policy](../../security/policy) on how to report security vulnerabilities. diff --git a/UPGRADING.md b/UPGRADING.md new file mode 100644 index 0000000..742ce52 --- /dev/null +++ b/UPGRADING.md @@ -0,0 +1,12 @@ +# Upgrading + +## From v1 to v2 + +The `startFrom` parameter was added, so you may need to change the order of your passed arguments or use named arguments: + +```php +// Instead of doing this: +Transaction::ofLast7Days(DateRange::INCLUSIVE); +// Do this: +Transaction::ofLast7Days(customRange: DateRange::INCLUSIVE); +``` diff --git a/config/date-scopes.php b/config/date-scopes.php index 71593be..cad1b41 100644 --- a/config/date-scopes.php +++ b/config/date-scopes.php @@ -8,7 +8,7 @@ * you could use the inclusive range here as a default. * Note that you can also fluently specify it for quite every scope we offer * directly when using the scope: - * Transaction::ofLast7Days(DateRange::INCLUSIVE); (this works for all but the singular "ofLast"-scopes) + * Transaction::ofLast7Days(customRange: DateRange::INCLUSIVE); (this works for all but the singular "ofLast"-scopes) * This will do an inclusive query, even though the global default range here is set to exclusive. */ 'default_range' => env('DATE_SCOPES_DEFAULT_RANGE', DateRange::EXCLUSIVE->value), diff --git a/src/DateScopes.php b/src/DateScopes.php index 5acfebd..680b661 100755 --- a/src/DateScopes.php +++ b/src/DateScopes.php @@ -2,6 +2,7 @@ namespace LaracraftTech\LaravelDateScopes; +use Carbon\CarbonImmutable; use Illuminate\Database\Eloquent\Builder; /** @@ -101,12 +102,20 @@ trait DateScopes ]; /** - * @param Builder $query Eloquent Builder - * @param string $dateUnit A valid date unit, such as hour, day, month, year etc... - * @param int $value How many hours, days etc. you want to get. + * @param Builder $query Eloquent Builder + * @param string $dateUnit A valid date unit, such as hour, day, month, year etc... + * @param int $value How many hours, days etc. you want to get. + * @param null $startFrom Any parseable date to start from + * @param DateRange|null $customRange + * @return Builder */ - public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, DateRange $customRange = null): Builder - { + public function scopeOfLastUnit( + Builder $query, + string $dateUnit, + int $value, + $startFrom = null, + DateRange $customRange = null + ): Builder { if ($value <= 0) { throw DateException::invalidValue(); } @@ -120,15 +129,17 @@ public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, Da $subFunc = 'sub'.ucfirst($dateUnit).'s'.$applyNoOverflow; $sub = 1; + $startFrom = CarbonImmutable::parse($startFrom ?? now()); + if ($dateRange === DateRange::EXCLUSIVE) { $range = [ - 'from' => now()->$subFunc($value)->$startFunc(), - 'to' => now()->$subFunc($sub)->$endFunc(), + 'from' => $startFrom->$subFunc($value)->$startFunc(), + 'to' => $startFrom->$subFunc($sub)->$endFunc(), ]; } else { $range = [ - 'from' => now()->$subFunc($value - $sub)->$startFunc(), - 'to' => now()->$endFunc(), + 'from' => $startFrom->$subFunc($value - $sub)->$startFunc(), + 'to' => $startFrom->$endFunc(), ]; } @@ -141,132 +152,133 @@ public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, Da } // START SECONDS - public function scopeOfJustNow(Builder $query): Builder {return $query->ofLastSeconds(1, DateRange::INCLUSIVE);} - public function scopeOfLastSecond(Builder $query): Builder {return $query->ofLastSeconds(1, DateRange::EXCLUSIVE);} - public function scopeOfLast15Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(15, $customRange);} - public function scopeOfLast30Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(30, $customRange);} - public function scopeOfLast45Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(45, $customRange);} - public function scopeOfLast60Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(60, $customRange);} - - public function scopeOfLastSeconds(Builder $query, int $seconds, DateRange $customRange = null): Builder + public function scopeOfJustNow(Builder $query, $startFrom = null): Builder {return $query->ofLastSeconds(1, $startFrom, DateRange::INCLUSIVE);} + public function scopeOfLastSecond(Builder $query, $startFrom = null): Builder {return $query->ofLastSeconds(1, $startFrom, DateRange::EXCLUSIVE);} + public function scopeOfLast15Seconds(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastSeconds(15, $startFrom, $customRange);} + public function scopeOfLast30Seconds(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastSeconds(30, $startFrom, $customRange);} + public function scopeOfLast45Seconds(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastSeconds(45, $startFrom, $customRange);} + public function scopeOfLast60Seconds(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastSeconds(60, $startFrom, $customRange);} + + public function scopeOfLastSeconds(Builder $query, int $seconds, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('second', $seconds, $customRange); + return $query->ofLastUnit('second', $seconds, $startFrom, $customRange); } // START MINUTES - public function scopeOfLastMinute(Builder $query): Builder {return $query->ofLastMinutes(1, DateRange::EXCLUSIVE);} - public function scopeOfLast15Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(15, $customRange);} - public function scopeOfLast30Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(30, $customRange);} - public function scopeOfLast45Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(45, $customRange);} - public function scopeOfLast60Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(60, $customRange);} + public function scopeOfLastMinute(Builder $query, $startFrom = null): Builder {return $query->ofLastMinutes(1, $startFrom, DateRange::EXCLUSIVE);} + public function scopeOfLast15Minutes(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastMinutes(15, $startFrom, $customRange);} + public function scopeOfLast30Minutes(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastMinutes(30, $startFrom, $customRange);} + public function scopeOfLast45Minutes(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastMinutes(45, $startFrom, $customRange);} + public function scopeOfLast60Minutes(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastMinutes(60, $startFrom, $customRange);} - public function scopeOfLastMinutes(Builder $query, int $minutes, DateRange $customRange = null): Builder + public function scopeOfLastMinutes(Builder $query, int $minutes, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('minute', $minutes, $customRange); + return $query->ofLastUnit('minute', $minutes, $startFrom, $customRange); } // START HOURS - public function scopeOfLastHour(Builder $query): Builder {return $query->ofLastHours(1, DateRange::EXCLUSIVE);} - public function scopeOfLast6Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(6, $customRange);} - public function scopeOfLast12Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(12, $customRange);} - public function scopeOfLast18Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(18, $customRange);} - public function scopeOfLast24Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(24, $customRange);} + public function scopeOfLastHour(Builder $query, $startFrom = null): Builder {return $query->ofLastHours(1, $startFrom, DateRange::EXCLUSIVE);} + public function scopeOfLast6Hours(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastHours(6, $startFrom, $customRange);} + public function scopeOfLast12Hours(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastHours(12, $startFrom, $customRange);} + public function scopeOfLast18Hours(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastHours(18, $startFrom, $customRange);} + public function scopeOfLast24Hours(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastHours(24, $startFrom, $customRange);} - public function scopeOfLastHours(Builder $query, int $hours, DateRange $customRange = null): Builder + public function scopeOfLastHours(Builder $query, int $hours, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('hour', $hours, $customRange); + return $query->ofLastUnit('hour', $hours, $startFrom, $customRange); } // START DAYS - public function scopeOfToday(Builder $query): Builder {return $query->ofLastDays(1, DateRange::INCLUSIVE);} - public function scopeOfYesterday(Builder $query): Builder {return $query->ofLastDays(1, DateRange::EXCLUSIVE);} - public function scopeOfLast7Days(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastDays(7, $customRange);} - public function scopeOfLast14Days(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastDays(14, $customRange);} - public function scopeOfLast21Days(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastDays(21, $customRange);} - public function scopeOfLast30Days(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastDays(30, $customRange);} - - public function scopeOfLastDays(Builder $query, int $days, DateRange $customRange = null): Builder + public function scopeOfToday(Builder $query, $startFrom = null): Builder {return $query->ofLastDays(1, $startFrom, DateRange::INCLUSIVE);} + public function scopeOfYesterday(Builder $query, $startFrom = null): Builder {return $query->ofLastDays(1, $startFrom, DateRange::EXCLUSIVE);} + public function scopeOfLast7Days(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastDays(7, $startFrom, $customRange);} + public function scopeOfLast14Days(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastDays(14, $startFrom, $customRange);} + public function scopeOfLast21Days(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastDays(21, $startFrom, $customRange);} + public function scopeOfLast30Days(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastDays(30, $startFrom, $customRange);} + + public function scopeOfLastDays(Builder $query, int $days, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('day', $days, $customRange); + return $query->ofLastUnit('day', $days, $startFrom, $customRange); } // START WEEKS - public function scopeOfLastWeek(Builder $query): Builder {return $query->ofLastWeeks(1, DateRange::EXCLUSIVE);} - public function scopeOfLast2Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(2, $customRange);} - public function scopeOfLast3Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(3, $customRange);} - public function scopeOfLast4Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(4, $customRange);} + public function scopeOfLastWeek(Builder $query, $startFrom = null): Builder {return $query->ofLastWeeks(1, $startFrom, DateRange::EXCLUSIVE);} + public function scopeOfLast2Weeks(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastWeeks(2, $startFrom, $customRange);} + public function scopeOfLast3Weeks(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastWeeks(3, $startFrom, $customRange);} + public function scopeOfLast4Weeks(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastWeeks(4, $startFrom, $customRange);} - public function scopeOfLastWeeks(Builder $query, int $weeks, DateRange $customRange = null): Builder + public function scopeOfLastWeeks(Builder $query, int $weeks, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('week', $weeks, $customRange); + return $query->ofLastUnit('week', $weeks, $startFrom, $customRange); } // START MONTHS - public function scopeOfLastMonth(Builder $query): Builder {return $query->ofLastMonths(1, DateRange::EXCLUSIVE);} - public function scopeOfLast3Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(3, $customRange);} - public function scopeOfLast6Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(6, $customRange);} - public function scopeOfLast9Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(9, $customRange);} - public function scopeOfLast12Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(12, $customRange);} + public function scopeOfLastMonth(Builder $query, $startFrom = null): Builder {return $query->ofLastMonths(1, $startFrom, DateRange::EXCLUSIVE);} + public function scopeOfLast3Months(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastMonths(3, $startFrom, $customRange);} + public function scopeOfLast6Months(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastMonths(6, $startFrom, $customRange);} + public function scopeOfLast9Months(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastMonths(9, $startFrom, $customRange);} + public function scopeOfLast12Months(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastMonths(12, $startFrom, $customRange);} - public function scopeOfLastMonths(Builder $query, int $months, DateRange $customRange = null): Builder + public function scopeOfLastMonths(Builder $query, int $months, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('month', $months, $customRange); + return $query->ofLastUnit('month', $months, $startFrom, $customRange); } // START QUARTER - public function scopeOfLastQuarter(Builder $query): Builder {return $query->ofLastQuarters(1, DateRange::EXCLUSIVE);} - public function scopeOfLast2Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(2, $customRange);} - public function scopeOfLast3Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(3, $customRange);} - public function scopeOfLast4Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(4, $customRange);} + public function scopeOfLastQuarter(Builder $query, $startFrom = null): Builder {return $query->ofLastQuarters(1, $startFrom, DateRange::EXCLUSIVE);} + public function scopeOfLast2Quarters(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastQuarters(2, $startFrom, $customRange);} + public function scopeOfLast3Quarters(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastQuarters(3, $startFrom, $customRange);} + public function scopeOfLast4Quarters(Builder $query, $startFrom = null, DateRange $customRange = null): Builder {return $query->ofLastQuarters(4, $startFrom, $customRange);} - public function scopeOfLastQuarters(Builder $query, int $quarters, DateRange $customRange = null): Builder + public function scopeOfLastQuarters(Builder $query, int $quarters, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('quarter', $quarters, $customRange); + return $query->ofLastUnit('quarter', $quarters, $startFrom, $customRange); } // START YEARS - public function scopeOfLastYear(Builder $query): Builder {return $query->ofLastYears(1, DateRange::EXCLUSIVE);} + public function scopeOfLastYear(Builder $query, $startFrom = null): Builder {return $query->ofLastYears(1, $startFrom, DateRange::EXCLUSIVE);} - public function scopeOfLastYears(Builder $query, int $years, DateRange $customRange = null): Builder + public function scopeOfLastYears(Builder $query, int $years, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('year', $years, $customRange); + return $query->ofLastUnit('year', $years, $startFrom, $customRange); } // START DECADE - public function scopeOfLastDecade(Builder $query): Builder {return $query->ofLastDecades(1, DateRange::EXCLUSIVE);} + public function scopeOfLastDecade(Builder $query, $startFrom = null): Builder {return $query->ofLastDecades(1, $startFrom, DateRange::EXCLUSIVE);} - public function scopeOfLastDecades(Builder $query, int $decades, DateRange $customRange = null): Builder + public function scopeOfLastDecades(Builder $query, int $decades, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('decade', $decades, $customRange); + return $query->ofLastUnit('decade', $decades, $startFrom, $customRange); } // START CENTURIES - public function scopeOfLastCentury(Builder $query): Builder {return $query->ofLastCenturies(1, DateRange::EXCLUSIVE);} + public function scopeOfLastCentury(Builder $query, $startFrom = null): Builder {return $query->ofLastCenturies(1, $startFrom, DateRange::EXCLUSIVE);} - public function scopeOfLastCenturies(Builder $query, int $centuries, DateRange $customRange = null): Builder + public function scopeOfLastCenturies(Builder $query, int $centuries, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('century', $centuries, $customRange); + return $query->ofLastUnit('century', $centuries, $startFrom, $customRange); } // START MILLENNIUM - public function scopeOfLastMillennium(Builder $query): Builder {return $query->ofLastMillenniums(1, DateRange::EXCLUSIVE);} + public function scopeOfLastMillennium(Builder $query, $startFrom = null): Builder {return $query->ofLastMillenniums(1, $startFrom, DateRange::EXCLUSIVE);} - public function scopeOfLastMillenniums(Builder $query, int $millennium, DateRange $customRange = null): Builder + public function scopeOfLastMillenniums(Builder $query, int $millennium, $startFrom = null, DateRange $customRange = null): Builder { - return $query->ofLastUnit('millennium', $millennium, $customRange); + return $query->ofLastUnit('millennium', $millennium, $startFrom, $customRange); } // START CURRENT TO NOW - public function scopeSecondToNow(Builder $query): Builder {return $query->ofLastSeconds(1, DateRange::INCLUSIVE);} - public function scopeMinuteToNow(Builder $query): Builder {return $query->ofLastMinutes(1, DateRange::INCLUSIVE);} - public function scopeHourToNow(Builder $query): Builder {return $query->ofLastHours(1, DateRange::INCLUSIVE);} - public function scopeDayToNow(Builder $query): Builder {return $query->ofLastDays(1, DateRange::INCLUSIVE);} - public function scopeWeekToDate(Builder $query): Builder {return $query->ofLastWeeks(1, DateRange::INCLUSIVE);} - public function scopeMonthToDate(Builder $query): Builder {return $query->ofLastMonths(1, DateRange::INCLUSIVE);} - public function scopeQuarterToDate(Builder $query): Builder {return $query->ofLastQuarters(1, DateRange::INCLUSIVE);} - public function scopeYearToDate(Builder $query): Builder {return $query->ofLastYears(1, DateRange::INCLUSIVE);} - public function scopeDecadeToDate(Builder $query): Builder {return $query->ofLastDecades(1, DateRange::INCLUSIVE);} - public function scopeCenturyToDate(Builder $query): Builder {return $query->ofLastCenturies(1, DateRange::INCLUSIVE);} - public function scopeMillenniumToDate(Builder $query): Builder {return $query->ofLastMillenniums(1, DateRange::INCLUSIVE);} + public function scopeSecondToNow(Builder $query): Builder {return $query->ofLastSeconds(1, customRange: DateRange::INCLUSIVE);} + public function scopeMinuteToNow(Builder $query): Builder {return $query->ofLastMinutes(1, customRange: DateRange::INCLUSIVE);} + public function scopeHourToNow(Builder $query): Builder {return $query->ofLastHours(1, customRange: DateRange::INCLUSIVE);} + public function scopeDayToNow(Builder $query): Builder {return $query->ofLastDays(1, customRange: DateRange::INCLUSIVE);} + public function scopeWeekToDate(Builder $query): Builder {return $query->ofLastWeeks(1, customRange: DateRange::INCLUSIVE);} + public function scopeMonthToDate(Builder $query): Builder {return $query->ofLastMonths(1, customRange: DateRange::INCLUSIVE);} + public function scopeQuarterToDate(Builder $query): Builder {return $query->ofLastQuarters(1, customRange: DateRange::INCLUSIVE);} + public function scopeYearToDate(Builder $query): Builder {return $query->ofLastYears(1, customRange: DateRange::INCLUSIVE);} + public function scopeDecadeToDate(Builder $query): Builder {return $query->ofLastDecades(1, customRange: DateRange::INCLUSIVE);} + public function scopeCenturyToDate(Builder $query): Builder {return $query->ofLastCenturies(1, customRange: DateRange::INCLUSIVE);} + public function scopeMillenniumToDate(Builder $query): Builder {return $query->ofLastMillenniums(1, customRange: DateRange::INCLUSIVE);} } + diff --git a/tests/DataScopesTest.php b/tests/DataScopesTest.php index 79aa509..fb6f4d4 100644 --- a/tests/DataScopesTest.php +++ b/tests/DataScopesTest.php @@ -17,10 +17,10 @@ function getCreatedAtValues(): array ['created_at' => $start], ['created_at' => '2023-03-31 13:15:14'], ['created_at' => '2023-03-31 13:15:00'], - ['created_at' => '2023-03-31 13:13:15'], ['created_at' => '2023-03-31 13:14:45'], ['created_at' => '2023-03-31 13:14:30'], ['created_at' => '2023-03-31 13:14:15'], + ['created_at' => '2023-03-31 13:13:15'], ['created_at' => '2023-03-31 12:45:00'], ['created_at' => '2023-03-31 12:30:00'], ['created_at' => '2023-03-31 12:15:00'], @@ -177,12 +177,12 @@ function getCreatedAtValues(): array }); it('retrieves transactions of last x (inclusive fluent date range)', function () { - expect(Transaction::ofLast15Minutes(DateRange::INCLUSIVE)->get())->toHaveCount(7) - ->and(Transaction::ofLast12Hours(DateRange::INCLUSIVE)->get())->toHaveCount(11) - ->and(Transaction::ofLast21Days(DateRange::INCLUSIVE)->get())->toHaveCount(17) - ->and(Transaction::ofLast3Weeks(DateRange::INCLUSIVE)->get())->toHaveCount(17) - ->and(Transaction::ofLast12Months(DateRange::INCLUSIVE)->get())->toHaveCount(23) - ->and(Transaction::ofLastQuarters(8, DateRange::INCLUSIVE)->get())->toHaveCount(24); + expect(Transaction::ofLast15Minutes(customRange: DateRange::INCLUSIVE)->get())->toHaveCount(7) + ->and(Transaction::ofLast12Hours(customRange: DateRange::INCLUSIVE)->get())->toHaveCount(11) + ->and(Transaction::ofLast21Days(customRange: DateRange::INCLUSIVE)->get())->toHaveCount(17) + ->and(Transaction::ofLast3Weeks(customRange: DateRange::INCLUSIVE)->get())->toHaveCount(17) + ->and(Transaction::ofLast12Months(customRange: DateRange::INCLUSIVE)->get())->toHaveCount(23) + ->and(Transaction::ofLastQuarters(8, customRange: DateRange::INCLUSIVE)->get())->toHaveCount(24); }); it('also works with a custom created_at column name', function() { @@ -211,3 +211,9 @@ function getCreatedAtValues(): array ->and(CustomTransaction::ofLastYear()->get())->toHaveCount(1) ->and(CustomTransaction::ofLastDecade()->get())->toHaveCount(1); }); + +it('retrieves transactions of last x (with startFrom)', function () { + expect(Transaction::ofLastMinute(startFrom: '2023-03-31 13:15:00')->get())->toHaveCount(3) + ->and(Transaction::ofYesterday(startFrom: '2023-03-31 13:15:00')->get())->toHaveCount(2) + ->and(Transaction::ofLastWeek(startFrom: '2023-03-31 13:15:00')->get())->toHaveCount(1); +});