diff --git a/src/SimpleStat.php b/src/SimpleStat.php index 4622041..b3d1023 100755 --- a/src/SimpleStat.php +++ b/src/SimpleStat.php @@ -14,6 +14,10 @@ class SimpleStat protected ?string $label; + protected ?string $description; + + protected bool $overWriteDescription = false; + public string $dateColumn = 'created_at'; public function __construct(private readonly string $model) @@ -33,6 +37,14 @@ public function label(string $label): self return $this; } + public function description(string $description): self + { + $this->description = $description; + $this->overWriteDescription = true; + + return $this; + } + public function dateColumn(string $dateColumn): self { $this->trend->dateColumn($dateColumn); @@ -58,6 +70,10 @@ public function lastDays(int $days): self end: now(), ); + if (!$this->overWriteDescription) { + $this->description = __('Last :days days', ['days' => $days]); + } + return $this; } @@ -118,7 +134,8 @@ private function buildAverageStat(Collection $trendData): Stat private function buildStat(string $faceValue, Collection $chartValues, AggregateType $aggregateType): Stat { return Stat::make($this->buildLabel($aggregateType), $faceValue) - ->chart($chartValues->map(fn (TrendValue $trend) => $trend->aggregate)->toArray()); + ->chart($chartValues->map(fn (TrendValue $trend) => $trend->aggregate)->toArray()) + ->description($this->description); } private function buildLabel(AggregateType $aggregateType): string diff --git a/tests/DescriptionTest.php b/tests/DescriptionTest.php new file mode 100644 index 0000000..738988f --- /dev/null +++ b/tests/DescriptionTest.php @@ -0,0 +1,27 @@ +count(rand(0, 2))->create(['created_at' => $date]); + } +}); + +it('constructs a description based on the interval', function () { + $simpleStat = SimpleStat::make(ExampleEvent::class)->last7Days()->dailyAverage(); + expect($simpleStat->getDescription())->toBe('Last 7 days'); + + $simpleStat = SimpleStat::make(ExampleEvent::class)->last30Days()->dailyAverage(); + expect($simpleStat->getDescription())->toBe('Last 30 days'); + + $simpleStat = SimpleStat::make(ExampleEvent::class)->lastDays(14)->dailyAverage(); + expect($simpleStat->getDescription())->toBe('Last 14 days'); +});