/core/Period.php

https://github.com/ntulip/piwik · PHP · 240 lines · 159 code · 23 blank · 58 comment · 14 complexity · f736dfe7c9004078b7f8d20175ce3e39 MD5 · raw file

  1. <?php
  2. /**
  3. * Piwik - Open source web analytics
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
  7. * @version $Id: Period.php 1420 2009-08-22 13:23:16Z vipsoft $
  8. *
  9. * @category Piwik
  10. * @package Piwik
  11. */
  12. /**
  13. * Creating a new Piwik_Period subclass:
  14. *
  15. * Every overloaded method must start with the code
  16. * if(!$this->subperiodsProcessed)
  17. * {
  18. * $this->generate();
  19. * }
  20. * that checks whether the subperiods have already been computed.
  21. * This is for performance improvements, computing the subperiods is done a per demand basis.
  22. *
  23. * @package Piwik
  24. * @subpackage Piwik_Period
  25. */
  26. abstract class Piwik_Period
  27. {
  28. protected $subperiods = array();
  29. protected $subperiodsProcessed = false;
  30. protected $label = null;
  31. protected $date = null;
  32. protected static $unknowPeriodException = "The period '%s' is not supported. Try 'day' or 'week' or 'month' or 'year'";
  33. public function __construct( $date )
  34. {
  35. $this->checkInputDate( $date );
  36. $this->date = clone $date;
  37. }
  38. /**
  39. * @param $strPeriod "day", "week", "month", "year"
  40. * @param $date Piwik_Date object
  41. * @return Piwik_Period
  42. */
  43. static public function factory($strPeriod, $date)
  44. {
  45. switch ($strPeriod) {
  46. case 'day':
  47. return new Piwik_Period_Day($date);
  48. break;
  49. case 'week':
  50. return new Piwik_Period_Week($date);
  51. break;
  52. case 'month':
  53. return new Piwik_Period_Month($date);
  54. break;
  55. case 'year':
  56. return new Piwik_Period_Year($date);
  57. break;
  58. default:
  59. throw new Exception(sprintf(self::$unknowPeriodException, $strPeriod));
  60. break;
  61. }
  62. }
  63. /**
  64. * Returns the first day of the period
  65. *
  66. * @return Piwik_Date First day of the period
  67. */
  68. public function getDateStart()
  69. {
  70. if(!$this->subperiodsProcessed)
  71. {
  72. $this->generate();
  73. }
  74. if(count($this->subperiods) == 0)
  75. {
  76. return $this->getDate();
  77. }
  78. $periods = $this->getSubperiods();
  79. $currentPeriod = $periods[0];
  80. while( $currentPeriod->getNumberOfSubperiods() > 0 )
  81. {
  82. $periods = $currentPeriod->getSubperiods();
  83. $currentPeriod = $periods[0];
  84. }
  85. return $currentPeriod->getDate();
  86. }
  87. /**
  88. * Returns the last day of the period ; can be a date in the future
  89. *
  90. * @return Piwik_Date Last day of the period
  91. */
  92. public function getDateEnd()
  93. {
  94. if(!$this->subperiodsProcessed)
  95. {
  96. $this->generate();
  97. }
  98. if(count($this->subperiods) == 0)
  99. {
  100. return $this->getDate();
  101. }
  102. $periods = $this->getSubperiods();
  103. $currentPeriod = $periods[count($periods)-1];
  104. while( $currentPeriod->getNumberOfSubperiods() > 0 )
  105. {
  106. $periods = $currentPeriod->getSubperiods();
  107. $currentPeriod = $periods[count($periods)-1];
  108. }
  109. return $currentPeriod->getDate();
  110. }
  111. public function getId()
  112. {
  113. return Piwik::$idPeriods[$this->getLabel()];
  114. }
  115. public function getLabel()
  116. {
  117. return $this->label;
  118. }
  119. /**
  120. * @return Piwik_Date
  121. */
  122. protected function getDate()
  123. {
  124. return $this->date;
  125. }
  126. protected function checkInputDate($date)
  127. {
  128. if( !($date instanceof Piwik_Date))
  129. {
  130. throw new Exception("The date must be a Piwik_Date object. " . var_export($date,true));
  131. }
  132. }
  133. protected function generate()
  134. {
  135. $this->subperiodsProcessed = true;
  136. }
  137. public function getNumberOfSubperiods()
  138. {
  139. if(!$this->subperiodsProcessed)
  140. {
  141. $this->generate();
  142. }
  143. return count($this->subperiods);
  144. }
  145. /**
  146. * Returns Period_Day for a period made of days (week, month),
  147. * Period_Month for a period made of months (year)
  148. *
  149. * @return array Piwik_Period
  150. */
  151. public function getSubperiods()
  152. {
  153. if(!$this->subperiodsProcessed)
  154. {
  155. $this->generate();
  156. }
  157. return $this->subperiods;
  158. }
  159. /**
  160. * Add a date to the period.
  161. *
  162. * Protected because it not yet supported to add periods after the initialization
  163. *
  164. * @param Piwik_Date Valid Piwik_Date object
  165. */
  166. protected function addSubperiod( $date )
  167. {
  168. $this->subperiods[] = $date;
  169. }
  170. /**
  171. * A period is finished if all the subperiods are finished
  172. */
  173. public function isFinished()
  174. {
  175. if(!$this->subperiodsProcessed)
  176. {
  177. $this->generate();
  178. }
  179. foreach($this->subperiods as $period)
  180. {
  181. if(!$period->isFinished())
  182. {
  183. return false;
  184. }
  185. }
  186. return true;
  187. }
  188. public function toString()
  189. {
  190. if(!$this->subperiodsProcessed)
  191. {
  192. $this->generate();
  193. }
  194. $dateString = array();
  195. foreach($this->subperiods as $period)
  196. {
  197. $dateString[] = $period->toString();
  198. }
  199. return $dateString;
  200. }
  201. public function __toString()
  202. {
  203. $elements = $this->toString();
  204. return implode(",", $elements);
  205. }
  206. public function get( $part= null )
  207. {
  208. if(!$this->subperiodsProcessed)
  209. {
  210. $this->generate();
  211. }
  212. return $this->date->get($part);
  213. }
  214. abstract public function getPrettyString();
  215. abstract public function getLocalizedShortString();
  216. abstract public function getLocalizedLongString();
  217. }