Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Metric extends Model
/**
* The attributes that aren't mass assignable.
*
* @var array
* @var array<string>
*/
protected $guarded = [];

Expand All @@ -37,6 +37,8 @@ protected function casts(): array

/**
* Create a new Eloquent query builder for the model.
*
* @return MetricBuilder<static>
*/
public function newEloquentBuilder($query): MetricBuilder
{
Expand Down
81 changes: 62 additions & 19 deletions src/MetricBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Builder;

/**
* @template TMetric of Metric
*
* @extends Builder<TMetric>
*/
class MetricBuilder extends Builder
{
/**
* Get metrics for today.
*
* @return $this
*/
public function today(): self
public function today(): static
{
return $this->onDate(today());
}

/**
* Get metrics for yesterday.
*
* @return $this
*/
public function yesterday(): self
public function yesterday(): static
{
return $this->onDate(
today()->subDay()
Expand All @@ -27,24 +36,30 @@ public function yesterday(): self

/**
* Get metrics for this hour.
*
* @return $this
*/
public function thisHour(): self
public function thisHour(): static
{
return $this->onDateTime(now());
}

/**
* Get metrics for last hour.
*
* @return $this
*/
public function lastHour(): self
public function lastHour(): static
{
return $this->onDateTime(now()->subHour());
}

/**
* Get metrics for this week.
*
* @return $this
*/
public function thisWeek(): self
public function thisWeek(): static
{
return $this->betweenDates(
today()->startOfWeek(),
Expand All @@ -54,8 +69,10 @@ public function thisWeek(): self

/**
* Get metrics for last week.
*
* @return $this
*/
public function lastWeek(): self
public function lastWeek(): static
{
return $this->betweenDates(
today()->subWeek()->startOfWeek(),
Expand All @@ -65,8 +82,10 @@ public function lastWeek(): self

/**
* Get metrics for this month.
*
* @return $this
*/
public function thisMonth(): self
public function thisMonth(): static
{
return $this->betweenDates(
today()->startOfMonth(),
Expand All @@ -76,8 +95,10 @@ public function thisMonth(): self

/**
* Get metrics for last month.
*
* @return $this
*/
public function lastMonth(): self
public function lastMonth(): static
{
return $this->betweenDates(
today()->subMonth()->startOfMonth(),
Expand All @@ -87,8 +108,10 @@ public function lastMonth(): self

/**
* Get metrics for last month without overflow.
*
* @return $this
*/
public function lastMonthNoOverflow(): self
public function lastMonthNoOverflow(): static
{
return $this->betweenDates(
today()->subMonthNoOverflow()->startOfMonth(),
Expand All @@ -98,8 +121,10 @@ public function lastMonthNoOverflow(): self

/**
* Get metrics for this quarter.
*
* @return $this
*/
public function thisQuarter(): self
public function thisQuarter(): static
{
return $this->betweenDates(
today()->startOfQuarter(),
Expand All @@ -109,8 +134,10 @@ public function thisQuarter(): self

/**
* Get metrics for last quarter.
*
* @return $this
*/
public function lastQuarter(): self
public function lastQuarter(): static
{
return $this->betweenDates(
today()->subQuarter()->startOfQuarter(),
Expand All @@ -120,8 +147,10 @@ public function lastQuarter(): self

/**
* Get metrics for last quarter without overflow.
*
* @return $this
*/
public function lastQuarterNoOverflow(): self
public function lastQuarterNoOverflow(): static
{
return $this->betweenDates(
today()->subQuarterNoOverflow()->startOfQuarter(),
Expand All @@ -131,8 +160,10 @@ public function lastQuarterNoOverflow(): self

/**
* Get metrics for this year.
*
* @return $this
*/
public function thisYear(): self
public function thisYear(): static
{
return $this->betweenDates(
today()->startOfYear(),
Expand All @@ -142,8 +173,10 @@ public function thisYear(): self

/**
* Get metrics for last year.
*
* @return $this
*/
public function lastYear(): self
public function lastYear(): static
{
return $this->betweenDates(
today()->subYear()->startOfYear(),
Expand All @@ -153,8 +186,10 @@ public function lastYear(): self

/**
* Get metrics for last year without overflow.
*
* @return $this
*/
public function lastYearNoOverflow(): self
public function lastYearNoOverflow(): static
{
return $this->betweenDates(
today()->subYearNoOverflow()->startOfYear(),
Expand All @@ -164,8 +199,10 @@ public function lastYearNoOverflow(): self

/**
* Get metrics between two dates.
*
* @return $this
*/
public function betweenDates(CarbonInterface $start, CarbonInterface $end): self
public function betweenDates(CarbonInterface $start, CarbonInterface $end): static
{
return $this->whereRaw(
'(year, month, day) >= (?, ?, ?) AND (year, month, day) <= (?, ?, ?)',
Expand All @@ -178,8 +215,10 @@ public function betweenDates(CarbonInterface $start, CarbonInterface $end): self

/**
* Get metrics between two datetimes (including hours).
*
* @return $this
*/
public function betweenDateTimes(CarbonInterface $start, CarbonInterface $end): self
public function betweenDateTimes(CarbonInterface $start, CarbonInterface $end): static
{
return $this->whereRaw(
'(year, month, day, hour) >= (?, ?, ?, ?) AND (year, month, day, hour) <= (?, ?, ?, ?)',
Expand All @@ -192,8 +231,10 @@ public function betweenDateTimes(CarbonInterface $start, CarbonInterface $end):

/**
* Get metrics on a specific date.
*
* @return $this
*/
public function onDate(CarbonInterface $date): self
public function onDate(CarbonInterface $date): static
{
return $this->where(function (Builder $query) use ($date) {
$query
Expand All @@ -205,8 +246,10 @@ public function onDate(CarbonInterface $date): self

/**
* Get metrics on a specific date and hour.
*
* @return $this
*/
public function onDateTime(CarbonInterface $hour): self
public function onDateTime(CarbonInterface $hour): static
{
return $this->onDate($hour)->where('hour', $hour->hour);
}
Expand Down
3 changes: 3 additions & 0 deletions src/MetricFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Metric>
*/
class MetricFactory extends Factory
{
/**
Expand Down