Accumulates counters (e.g. "sales", "signups") across multiple years, bucketed by calendar period (day/week/month/trimester/semester/year), for year-over-year comparison charts. getCount is CUMULATIVE: it sums every included period from the finest granularity up to (and including) the one asked for -- e.g.
class MMO\core\helpers\PeriodStatistic
Namespace
MMO\core\helpers
asking for Month returns day+week+month combined, not the month bucket alone.
$stat = new PeriodStatistic(2); // track 2 years, default periods (day/week/month/year)
$stat->addCategory('sales');
$stat->add('sales', 5, cDatePeriod::Day, 0); // year index 0
$stat->add('sales', 3, cDatePeriod::Week, 0);
$stat->getCount('sales', cDatePeriod::Week, 0); // 8 (day + week)
Convenience driver: for each of the setYearCount tracked years (going backward one year at a time from $date), queries $collection for entities matching $filters within that year's largest tra…
Register a counter title, pre-filled with zeros for every tracked year/period. Not strictly required before add (which lazily creates the entry), but ensures the title appears (with zeros) in
Convenience driver: for each of the setYearCount tracked years (going backward one year at a time from $date), queries $collection for entities matching $filters within that year's largest tracked period window, and yields each one tagged with which period it falls into -- while also updating the internal "current" period/year ($period / $yearIndex) so a plain add(title, count) call inside the loop body (with no explicit period/year) lands in the right bucket.
periodName => iEntity, one at a time, across every tracked year
foreach ($stat->loopCollection($orders, new DateTime(), [], 'ctime') as $period => $order) {
$stat->add('revenue', $order['amount']); // period/year are inferred from the current loop position
}