Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Period.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use Piwik\Period\Factory as PeriodFactory;
  11. use Piwik\Period\Range;
  12. /**
  13. * Date range representation.
  14. *
  15. * Piwik allows users to view aggregated statistics for single days and for date
  16. * ranges consisting of several days. When requesting data, a **date** string and
  17. * a **period** string must be used to specify the date range that the data regards.
  18. * This is the class Piwik uses to represent and manipulate those date ranges.
  19. *
  20. * There are five types of periods in Piwik: day, week, month, year and range,
  21. * where **range** is any date range. The reason the other periods exist instead
  22. * of just **range** is that Piwik will pre-archive reports for days, weeks, months
  23. * and years, while every custom date range is archived on-demand.
  24. *
  25. * @api
  26. */
  27. abstract class Period
  28. {
  29. /**
  30. * Array of subperiods
  31. * @var \Piwik\Period[]
  32. */
  33. protected $subperiods = array();
  34. protected $subperiodsProcessed = false;
  35. /**
  36. * @var string
  37. */
  38. protected $label = null;
  39. /**
  40. * @var Date
  41. */
  42. protected $date = null;
  43. /**
  44. * Constructor.
  45. *
  46. * @param Date $date
  47. * @ignore
  48. */
  49. public function __construct(Date $date)
  50. {
  51. $this->date = clone $date;
  52. }
  53. /**
  54. * @deprecated Use Factory::build instead
  55. * @param $period
  56. * @param $date
  57. * @return Period
  58. */
  59. public static function factory($period, $date)
  60. {
  61. return PeriodFactory::build($period, $date);
  62. }
  63. /**
  64. * Returns true if `$dateString` and `$period` represent multiple periods.
  65. *
  66. * Will return true for date/period combinations where date references multiple
  67. * dates and period is not `'range'`. For example, will return true for:
  68. *
  69. * - **date** = `2012-01-01,2012-02-01` and **period** = `'day'`
  70. * - **date** = `2012-01-01,2012-02-01` and **period** = `'week'`
  71. * - **date** = `last7` and **period** = `'month'`
  72. *
  73. * etc.
  74. *
  75. * @static
  76. * @param $dateString string The **date** query parameter value.
  77. * @param $period string The **period** query parameter value.
  78. * @return boolean
  79. */
  80. public static function isMultiplePeriod($dateString, $period)
  81. {
  82. return is_string($dateString)
  83. && (preg_match('/^(last|previous){1}([0-9]*)$/D', $dateString, $regs)
  84. || Range::parseDateRange($dateString))
  85. && $period != 'range';
  86. }
  87. /**
  88. * Returns the first day of the period.
  89. *
  90. * @return Date
  91. */
  92. public function getDateStart()
  93. {
  94. $this->generate();
  95. if (count($this->subperiods) == 0) {
  96. return $this->getDate();
  97. }
  98. $periods = $this->getSubperiods();
  99. /** @var $currentPeriod Period */
  100. $currentPeriod = $periods[0];
  101. while ($currentPeriod->getNumberOfSubperiods() > 0) {
  102. $periods = $currentPeriod->getSubperiods();
  103. $currentPeriod = $periods[0];
  104. }
  105. return $currentPeriod->getDate();
  106. }
  107. /**
  108. * Returns the last day of the period.
  109. *
  110. * @return Date
  111. */
  112. public function getDateEnd()
  113. {
  114. $this->generate();
  115. if (count($this->subperiods) == 0) {
  116. return $this->getDate();
  117. }
  118. $periods = $this->getSubperiods();
  119. /** @var $currentPeriod Period */
  120. $currentPeriod = $periods[count($periods) - 1];
  121. while ($currentPeriod->getNumberOfSubperiods() > 0) {
  122. $periods = $currentPeriod->getSubperiods();
  123. $currentPeriod = $periods[count($periods) - 1];
  124. }
  125. return $currentPeriod->getDate();
  126. }
  127. /**
  128. * Returns the period ID.
  129. *
  130. * @return int A unique integer for this type of period.
  131. */
  132. public function getId()
  133. {
  134. return Piwik::$idPeriods[$this->getLabel()];
  135. }
  136. /**
  137. * Returns the label for the current period.
  138. *
  139. * @return string `"day"`, `"week"`, `"month"`, `"year"`, `"range"`
  140. */
  141. public function getLabel()
  142. {
  143. return $this->label;
  144. }
  145. /**
  146. * @return Date
  147. */
  148. protected function getDate()
  149. {
  150. return $this->date;
  151. }
  152. protected function generate()
  153. {
  154. $this->subperiodsProcessed = true;
  155. }
  156. /**
  157. * Returns the number of available subperiods.
  158. *
  159. * @return int
  160. */
  161. public function getNumberOfSubperiods()
  162. {
  163. $this->generate();
  164. return count($this->subperiods);
  165. }
  166. /**
  167. * Returns the set of Period instances that together make up this period. For a year,
  168. * this would be 12 months. For a month this would be 28-31 days. Etc.
  169. *
  170. * @return Period[]
  171. */
  172. public function getSubperiods()
  173. {
  174. $this->generate();
  175. return $this->subperiods;
  176. }
  177. /**
  178. * Add a date to the period.
  179. *
  180. * Protected because adding periods after initialization is not supported.
  181. *
  182. * @param \Piwik\Period $period Valid Period object
  183. * @ignore
  184. */
  185. protected function addSubperiod($period)
  186. {
  187. $this->subperiods[] = $period;
  188. }
  189. /**
  190. * Returns a list of strings representing the current period.
  191. *
  192. * @param string $format The format of each individual day.
  193. * @return array An array of string dates that this period consists of.
  194. */
  195. public function toString($format = "Y-m-d")
  196. {
  197. $this->generate();
  198. $dateString = array();
  199. foreach ($this->subperiods as $period) {
  200. $dateString[] = $period->toString($format);
  201. }
  202. return $dateString;
  203. }
  204. /**
  205. * See {@link toString()}.
  206. *
  207. * @return string
  208. */
  209. public function __toString()
  210. {
  211. return implode(",", $this->toString());
  212. }
  213. /**
  214. * Returns a pretty string describing this period.
  215. *
  216. * @return string
  217. */
  218. abstract public function getPrettyString();
  219. /**
  220. * Returns a short string description of this period that is localized with the currently used
  221. * language.
  222. *
  223. * @return string
  224. */
  225. abstract public function getLocalizedShortString();
  226. /**
  227. * Returns a long string description of this period that is localized with the currently used
  228. * language.
  229. *
  230. * @return string
  231. */
  232. abstract public function getLocalizedLongString();
  233. /**
  234. * Returns a succinct string describing this period.
  235. *
  236. * @return string eg, `'2012-01-01,2012-01-31'`.
  237. */
  238. public function getRangeString()
  239. {
  240. return $this->getDateStart()->toString("Y-m-d") . "," . $this->getDateEnd()->toString("Y-m-d");
  241. }
  242. }