Introduce a scalable statistics architecture including `statistics` class, interface, and related implementations. Added support for tracking new bookings with routes and data handling through `bookings_s` and `statistic_t`. Integrated the feature into the application using autoloaders and response mechanisms.
102 lines
2.7 KiB
PHP
102 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace traits;
|
|
|
|
use statistics\bookings_s;
|
|
|
|
trait statistic_t
|
|
{
|
|
public array $data_sets = [];
|
|
public array $labels = [];
|
|
/**
|
|
* The start date of the date range
|
|
* This is set by the set_date_range method, and is used to filter the data.
|
|
* All dates are in the format 'YYYY-MM-DD'
|
|
* If the start date is not set, the default is fetched from self::get_default_start_date()
|
|
* @var string
|
|
*/
|
|
public string $start;
|
|
|
|
/**
|
|
* The end date of the date range
|
|
* This is set by the set_date_range method, and is used to filter the data.
|
|
* All dates are in the format 'YYYY-MM-DD'
|
|
* If the end date is not set, the default is fetched from self::get_default_end_date()
|
|
* @var string
|
|
*/
|
|
public string $end;
|
|
|
|
/**
|
|
* Set the date range for the statistics
|
|
*/
|
|
public function set_date_range(string $start, string $end): self
|
|
{
|
|
$this->start = $start;
|
|
$this->end = $end;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get the default start date for the statistics
|
|
*/
|
|
public function get_default_start_date(): string
|
|
{
|
|
return date('Y-m-d', strtotime('-1 month'));
|
|
}
|
|
|
|
/**
|
|
* Get the default end date for the statistics
|
|
*/
|
|
public function get_default_end_date(): string
|
|
{
|
|
return date('Y-m-d');
|
|
}
|
|
|
|
/**
|
|
* Add a label to the statistics
|
|
* @param string $label The label to add to the statistics (e.g. 'January')
|
|
* @return bookings_s|statistic_t
|
|
*/
|
|
public function add_label(string $label): self
|
|
{
|
|
$this->labels[] = $label;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Add a data set to the statistics
|
|
* @param string $label The label for the data set (e.g. 'January')
|
|
* @param array $data_set The data set to add to the statistics (e.g. [1, 2, 3, 4])
|
|
* @param array $options The options for the data set (e.g. ['backgroundColor' => '#FF0000'])
|
|
* @return bookings_s|statistic_t
|
|
*/
|
|
public function add_data_set(string $label, array $data_set, array $options = []): self
|
|
{
|
|
// Apply the default options
|
|
$options = array_merge([
|
|
'label' => 'Dataset ' . (count($this->data_sets) + 1),
|
|
'backgroundColor' => '#FF0000',
|
|
], $options);
|
|
// Add the data set
|
|
$this->data_sets[] = [
|
|
'label' => $label,
|
|
'backgroundColor' => $options['backgroundColor'],
|
|
'data' => $data_set
|
|
];
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get the response for the statistics
|
|
* @return array
|
|
*/
|
|
public function get_response(): array
|
|
{
|
|
return [
|
|
'labels' => $this->labels,
|
|
'datasets' => $this->data_sets
|
|
];
|
|
}
|
|
|
|
|
|
} |