| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class PeriodRange |
| 4 |
* |
| 5 |
* @package LearnPress/Classes/Statistics |
| 6 |
* @since 4.4.2 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace LearnPress\Statistics; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit(); |
| 12 |
|
| 13 |
/** |
| 14 |
* Value object describing one resolved statistics window. |
| 15 |
* |
| 16 |
* Carries both the concrete [ start … end ] datetimes and the legacy-compatible |
| 17 |
* { filter_type, time } pair consumed by LP_Statistics_DB / DashboardStatisticsDB, |
| 18 |
* so existing query methods keep working unchanged. `granularity` is the explicit |
| 19 |
* chart resolution — the DB group-by never re-guesses it. |
| 20 |
* |
| 21 |
* Instances are only built by PeriodResolver. |
| 22 |
* |
| 23 |
* @since 4.4.2 |
| 24 |
*/ |
| 25 |
class PeriodRange { |
| 26 |
/** |
| 27 |
* @var string Requested preset id, e.g. 'month', 'last_quarter', 'custom'. |
| 28 |
*/ |
| 29 |
public $preset; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string Window start, 'Y-m-d H:i:s'. |
| 33 |
*/ |
| 34 |
public $start; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string Window end, 'Y-m-d H:i:s'. |
| 38 |
*/ |
| 39 |
public $end; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var string Chart resolution: PeriodResolver::GRAN_HOUR|GRAN_DAY|GRAN_MONTH. |
| 43 |
*/ |
| 44 |
public $granularity; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var string Human label, e.g. 'Month to date (Jul 1 – 14)'. |
| 48 |
*/ |
| 49 |
public $label; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var string Legacy DB filter type paired with $time: date|custom. |
| 53 |
*/ |
| 54 |
public $filter_type; |
| 55 |
|
| 56 |
/** |
| 57 |
* @var string|int Legacy DB filter value paired with $filter_type. |
| 58 |
*/ |
| 59 |
public $time; |
| 60 |
|
| 61 |
/** |
| 62 |
* @param string $preset Preset id as requested. |
| 63 |
* @param string $start 'Y-m-d H:i:s'. |
| 64 |
* @param string $end 'Y-m-d H:i:s'. |
| 65 |
* @param string $granularity hour|day|month. |
| 66 |
* @param string $label Display label. |
| 67 |
* @param string $filter_type Legacy DB filter type. |
| 68 |
* @param string|int $time Legacy DB filter value. |
| 69 |
*/ |
| 70 |
public function __construct( string $preset, string $start, string $end, string $granularity, string $label, string $filter_type, $time ) { |
| 71 |
$this->preset = $preset; |
| 72 |
$this->start = $start; |
| 73 |
$this->end = $end; |
| 74 |
$this->granularity = $granularity; |
| 75 |
$this->label = $label; |
| 76 |
$this->filter_type = $filter_type; |
| 77 |
$this->time = $time; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* The { filter_type, time } pair every legacy DB method understands. |
| 82 |
* |
| 83 |
* @return array [ 'filter_type' => string, 'time' => string|int ] |
| 84 |
*/ |
| 85 |
public function legacy_pair(): array { |
| 86 |
return array( |
| 87 |
'filter_type' => $this->filter_type, |
| 88 |
'time' => $this->time, |
| 89 |
); |
| 90 |
} |
| 91 |
} |
| 92 |
|