Newer
Older
mattpiwik
a validé
<?php
/**
* Piwik - free/libre analytics platform
mattpiwik
a validé
* @link http://piwik.org
mattpiwik
a validé
*/
use Piwik\Period\Factory as PeriodFactory;
mattab
a validé
use Piwik\Period\Range;
mattpiwik
a validé
/**
* Piwik allows users to view aggregated statistics for single days and for date
* ranges consisting of several days. When requesting data, a **date** string and
* a **period** string must be used to specify the date range that the data regards.
* This is the class Piwik uses to represent and manipulate those date ranges.
* There are five types of periods in Piwik: day, week, month, year and range,
* where **range** is any date range. The reason the other periods exist instead
* of just **range** is that Piwik will pre-archive reports for days, weeks, months
mattab
a validé
* and years, while every custom date range is archived on-demand.
*
mattpiwik
a validé
*/
mattpiwik
a validé
{
protected $subperiodsProcessed = false;
* @var Date
*/
protected $date = null;
* @param Date $date
public function __construct(Date $date)
{
$this->date = clone $date;
}
* @deprecated Use Factory::build instead
* @param $period
* @param $date
* @return Period
*/
public static function factory($period, $date)
{
return PeriodFactory::build($period, $date);
/**
* Returns true if `$dateString` and `$period` represent multiple periods.
* Will return true for date/period combinations where date references multiple
* dates and period is not `'range'`. For example, will return true for:
* - **date** = `2012-01-01,2012-02-01` and **period** = `'day'`
* - **date** = `2012-01-01,2012-02-01` and **period** = `'week'`
* - **date** = `last7` and **period** = `'month'`
* @static
mattab
a validé
* @param $dateString string The **date** query parameter value.
* @param $period string The **period** query parameter value.
* @return boolean
*/
public static function isMultiplePeriod($dateString, $period)
{
&& (preg_match('/^(last|previous){1}([0-9]*)$/D', $dateString, $regs)
|| Range::parseDateRange($dateString))
&& $period != 'range';
}
* Returns the first day of the period.
*/
public function getDateStart()
{
if (count($this->subperiods) == 0) {
return $this->getDate();
}
$periods = $this->getSubperiods();
mattab
a validé
/** @var $currentPeriod Period */
$currentPeriod = $periods[0];
while ($currentPeriod->getNumberOfSubperiods() > 0) {
$periods = $currentPeriod->getSubperiods();
$currentPeriod = $periods[0];
}
return $currentPeriod->getDate();
}
* Returns the last day of the period.
*/
public function getDateEnd()
{
if (count($this->subperiods) == 0) {
return $this->getDate();
}
$periods = $this->getSubperiods();
mattab
a validé
/** @var $currentPeriod Period */
$currentPeriod = $periods[count($periods) - 1];
while ($currentPeriod->getNumberOfSubperiods() > 0) {
$periods = $currentPeriod->getSubperiods();
$currentPeriod = $periods[count($periods) - 1];
}
return $currentPeriod->getDate();
}
mattpiwik
a validé
/**
* Returns the period ID.
* @return int A unique integer for this type of period.
public function getId()
{
return Piwik::$idPeriods[$this->getLabel()];
}
mattpiwik
a validé
* Returns the label for the current period.
* @return string `"day"`, `"week"`, `"month"`, `"year"`, `"range"`
{
return $this->label;
}
/**
* @return Date
*/
protected function getDate()
{
return $this->date;
}
protected function generate()
{
$this->subperiodsProcessed = true;
}
* Returns the number of available subperiods.
* @return int
*/
public function getNumberOfSubperiods()
return count($this->subperiods);
}
mattpiwik
a validé
* Returns the set of Period instances that together make up this period. For a year,
* this would be 12 months. For a month this would be 28-31 days. Etc.
* @return Period[]
*/
public function getSubperiods()
{
return $this->subperiods;
}
/**
* Add a date to the period.
*
* Protected because adding periods after initialization is not supported.
* @param \Piwik\Period $period Valid Period object
*/
protected function addSubperiod($period)
{
$this->subperiods[] = $period;
}
* Returns a list of strings representing the current period.
* @param string $format The format of each individual day.
* @return array An array of string dates that this period consists of.
$dateString = array();
foreach ($this->subperiods as $period) {
$dateString[] = $period->toString($format);
}
return $dateString;
}
public function __toString()
{
return implode(",", $this->toString());
}
/**
* Returns a pretty string describing this period.
abstract public function getPrettyString();
/**
* Returns a short string description of this period that is localized with the currently used
* language.
abstract public function getLocalizedShortString();
/**
* Returns a long string description of this period that is localized with the currently used
* language.
abstract public function getLocalizedLongString();
/**
* Returns a succinct string describing this period.
* @return string eg, `'2012-01-01,2012-01-31'`.
*/
Benaka Moorthi
a validé
public function getRangeString()
{
return $this->getDateStart()->toString("Y-m-d") . "," . $this->getDateEnd()->toString("Y-m-d");
Benaka Moorthi
a validé
}