CoolCMS API

StatisticsSeriesTrait

Shared time-series mechanics for the eshop ChartJS aggregations: x-axis buckets, their labels and typed scalar reads from DBAL rows.

Used by OrderStatisticsService and ProductStatisticsService.

IMPORTANT: the bucket key is computed in two places, in SQL (bucketExpr(), MySQL DATE_FORMAT) and in PHP (bucketLabels()), and both must match. Otherwise a series misses the axis and the chart is empty without any error. ISO week %x-%v equals PHP o-W.

Table of Contents

Constants

BUCKETS  : array<string|int, mixed> = ['day' => '%Y-%m-%d', 'week' => '%x-%v', 'month...
Allowed granularities mapped to MySQL `DATE_FORMAT` masks.

Methods

bucketExpr()  : string
Returns the MySQL `DATE_FORMAT` expression for the granularity; it must produce the same key as {@see bucketLabels()}.
bucketLabels()  : array<int|string, string>
Builds the `bucketKey => label` map for the whole `from..to` range, including days and months without a single order (the x axis must have no gaps).
dayEnd()  : string
dayStart()  : string
pivot()  : array{labels: list, datasets: list}>}
Pivots flat SQL rows (bucket x dimension) into the ChartJS structure: one dataset per dimension value (status/shipper/payment/product set), padded with zero buckets for days without data (otherwise the chart would have gaps).
toFloat()  : float
toInt()  : int
toStr()  : string

Constants

BUCKETS

Allowed granularities mapped to MySQL `DATE_FORMAT` masks.

private array<string|int, mixed> BUCKETS = ['day' => '%Y-%m-%d', 'week' => '%x-%v', 'month' => '%Y-%m', 'year' => '%Y']

Methods

bucketExpr()

Returns the MySQL `DATE_FORMAT` expression for the granularity; it must produce the same key as {@see bucketLabels()}.

private bucketExpr(string $column, string $granularity) : string
Parameters
$column : string
$granularity : string
Return values
string

bucketLabels()

Builds the `bucketKey => label` map for the whole `from..to` range, including days and months without a single order (the x axis must have no gaps).

private bucketLabels(DateTimeInterface $from, DateTimeInterface $to, string $granularity) : array<int|string, string>

The key is always a string (DATE_FORMAT returns a string), but PHP casts purely numeric string keys (such as year "2025") to int, hence array<int|string, string>.

Parameters
$from : DateTimeInterface
$to : DateTimeInterface
$granularity : string
Return values
array<int|string, string>

dayEnd()

private dayEnd(DateTimeInterface $date) : string
Parameters
$date : DateTimeInterface
Return values
string

dayStart()

private dayStart(DateTimeInterface $date) : string
Parameters
$date : DateTimeInterface
Return values
string

pivot()

Pivots flat SQL rows (bucket x dimension) into the ChartJS structure: one dataset per dimension value (status/shipper/payment/product set), padded with zero buckets for days without data (otherwise the chart would have gaps).

private pivot(array<int, array<string, mixed>> $rows, DateTimeInterface $from, DateTimeInterface $to, string $granularity, string $idField, string $keyField, string $nameField, string $valueField[, bool $isSum = false ]) : array{labels: list, datasets: list}>}
Parameters
$rows : array<int, array<string, mixed>>
$from : DateTimeInterface
$to : DateTimeInterface
$granularity : string
$idField : string
$keyField : string
$nameField : string
$valueField : string
$isSum : bool = false
Return values
array{labels: list, datasets: list}>}

toFloat()

private toFloat(mixed $value) : float
Parameters
$value : mixed
Return values
float

toStr()

private toStr(mixed $value[, string $default = '' ]) : string
Parameters
$value : mixed
$default : string = ''
Return values
string
On this page

Search results