feat: add price classification enum and reliable sort option
This commit is contained in:
49
app/Enums/PriceClassification.php
Normal file
49
app/Enums/PriceClassification.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
enum PriceClassification: string
|
||||
{
|
||||
case Current = 'current';
|
||||
case Recent = 'recent';
|
||||
case Stale = 'stale';
|
||||
case Outdated = 'outdated';
|
||||
|
||||
public static function fromUpdatedAt(?Carbon $updatedAt): self
|
||||
{
|
||||
if ($updatedAt === null) {
|
||||
return self::Outdated;
|
||||
}
|
||||
|
||||
$hours = $updatedAt->diffInHours(now());
|
||||
|
||||
return match (true) {
|
||||
$hours < 24 => self::Current,
|
||||
$hours < 48 => self::Recent,
|
||||
$hours < 120 => self::Stale,
|
||||
default => self::Outdated,
|
||||
};
|
||||
}
|
||||
|
||||
public function weight(): int
|
||||
{
|
||||
return match ($this) {
|
||||
self::Current => 0,
|
||||
self::Recent => 1,
|
||||
self::Stale => 2,
|
||||
self::Outdated => 3,
|
||||
};
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Current => 'Current',
|
||||
self::Recent => 'Recent',
|
||||
self::Stale => 'Stale',
|
||||
self::Outdated => 'Outdated',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user