diff options
author | mattab <matthieu.aubry@gmail.com> | 2013-03-28 03:42:39 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2013-03-28 03:42:40 +0400 |
commit | ae4b03163792f0b6e933933e5d37df87dc3fd566 (patch) | |
tree | d1d7510a9728f587d3d63ebd03e4ecf3d904838b /core/Period.php | |
parent | 158c2150f5f2e13ece459b8d131244c11b763997 (diff) |
Mass conversion of all files to the newly agreed coding standard: PSR 1/2
Converting Piwik core source files, PHP, JS, TPL, CSS
More info: http://piwik.org/participate/coding-standards/
Diffstat (limited to 'core/Period.php')
-rw-r--r-- | core/Period.php | 410 |
1 files changed, 200 insertions, 210 deletions
diff --git a/core/Period.php b/core/Period.php index 81ab037539..6e6d4cf4d6 100644 --- a/core/Period.php +++ b/core/Period.php @@ -1,24 +1,24 @@ <?php /** * Piwik - Open source web analytics - * + * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * + * * @category Piwik * @package Piwik */ /** - * Creating a new Piwik_Period subclass: - * + * Creating a new Piwik_Period subclass: + * * Every overloaded method must start with the code - * if(!$this->subperiodsProcessed) - * { - * $this->generate(); - * } - * that checks whether the subperiods have already been computed. - * This is for performance improvements, computing the subperiods is done a per demand basis. + * if(!$this->subperiodsProcessed) + * { + * $this->generate(); + * } + * that checks whether the subperiods have already been computed. + * This is for performance improvements, computing the subperiods is done a per demand basis. * * @package Piwik * @subpackage Piwik_Period @@ -30,151 +30,145 @@ abstract class Piwik_Period * @var Piwik_Period[] */ protected $subperiods = array(); - protected $subperiodsProcessed = false; + protected $subperiodsProcessed = false; /** * @var string */ protected $label = null; - /** - * @var Piwik_Date - */ - protected $date = null; - static protected $errorAvailablePeriods = 'day, week, month, year, range'; + /** + * @var Piwik_Date + */ + protected $date = null; + static protected $errorAvailablePeriods = 'day, week, month, year, range'; /** * Constructor * @param Piwik_Date $date */ - public function __construct( $date ) - { - $this->checkInputDate( $date ); - $this->date = clone $date; - } + public function __construct($date) + { + $this->checkInputDate($date); + $this->date = clone $date; + } + + /** + * @param string $strPeriod "day", "week", "month", "year" + * @param Piwik_Date $date Piwik_Date object + * @throws Exception + * @return Piwik_Period + */ + static public function factory($strPeriod, Piwik_Date $date) + { + switch ($strPeriod) { + case 'day': + return new Piwik_Period_Day($date); + break; + + case 'week': + return new Piwik_Period_Week($date); + break; + + case 'month': + return new Piwik_Period_Month($date); + break; + + case 'year': + return new Piwik_Period_Year($date); + break; + + default: + throw new Exception(Piwik_TranslateException('General_ExceptionInvalidPeriod', array($strPeriod, self::$errorAvailablePeriods))); + break; + } + } + + /** + * The advanced factory method is easier to use from the API than the factory + * method above. It doesn't require an instance of Piwik_Date and works for + * period=range. Generally speaking, anything that can be passed as period + * and range to the API methods can directly be forwarded to this factory + * method in order to get a suitable instance of Piwik_Period. + * + * @param string $strPeriod "day", "week", "month", "year", "range" + * @param string $strDate + * @return Piwik_Period + */ + static public function advancedFactory($strPeriod, $strDate) + { + if (Piwik_Archive::isMultiplePeriod($strDate, $strPeriod) || $strPeriod == 'range') { + return new Piwik_Period_Range($strPeriod, $strDate); + } + return self::factory($strPeriod, Piwik_Date::factory($strDate)); + } + + + /** + * Returns the first day of the period + * + * @return Piwik_Date First day of the period + */ + public function getDateStart() + { + if (!$this->subperiodsProcessed) { + $this->generate(); + } + if (count($this->subperiods) == 0) { + return $this->getDate(); + } + $periods = $this->getSubperiods(); + $currentPeriod = $periods[0]; + while ($currentPeriod->getNumberOfSubperiods() > 0) { + $periods = $currentPeriod->getSubperiods(); + $currentPeriod = $periods[0]; + } + return $currentPeriod->getDate(); + } - /** - * @param string $strPeriod "day", "week", "month", "year" - * @param Piwik_Date $date Piwik_Date object - * @throws Exception - * @return Piwik_Period - */ - static public function factory($strPeriod, Piwik_Date $date) - { - switch ($strPeriod) { - case 'day': - return new Piwik_Period_Day($date); - break; - - case 'week': - return new Piwik_Period_Week($date); - break; - - case 'month': - return new Piwik_Period_Month($date); - break; - - case 'year': - return new Piwik_Period_Year($date); - break; - - default: - throw new Exception(Piwik_TranslateException('General_ExceptionInvalidPeriod', array($strPeriod, self::$errorAvailablePeriods))); - break; - } - } - - /** - * The advanced factory method is easier to use from the API than the factory - * method above. It doesn't require an instance of Piwik_Date and works for - * period=range. Generally speaking, anything that can be passed as period - * and range to the API methods can directly be forwarded to this factory - * method in order to get a suitable instance of Piwik_Period. - * - * @param string $strPeriod "day", "week", "month", "year", "range" - * @param string $strDate - * @return Piwik_Period - */ - static public function advancedFactory($strPeriod, $strDate) { - if (Piwik_Archive::isMultiplePeriod($strDate, $strPeriod) || $strPeriod == 'range') - { - return new Piwik_Period_Range($strPeriod, $strDate); - } - return self::factory($strPeriod, Piwik_Date::factory($strDate)); - } + /** + * Returns the last day of the period ; can be a date in the future + * + * @return Piwik_Date Last day of the period + */ + public function getDateEnd() + { + if (!$this->subperiodsProcessed) { + $this->generate(); + } + if (count($this->subperiods) == 0) { + return $this->getDate(); + } + $periods = $this->getSubperiods(); + $currentPeriod = $periods[count($periods) - 1]; + while ($currentPeriod->getNumberOfSubperiods() > 0) { + $periods = $currentPeriod->getSubperiods(); + $currentPeriod = $periods[count($periods) - 1]; + } + return $currentPeriod->getDate(); + } - - /** - * Returns the first day of the period - * - * @return Piwik_Date First day of the period - */ - public function getDateStart() - { - if(!$this->subperiodsProcessed) - { - $this->generate(); - } - if(count($this->subperiods) == 0) - { - return $this->getDate(); - } - $periods = $this->getSubperiods(); - $currentPeriod = $periods[0]; - while( $currentPeriod->getNumberOfSubperiods() > 0 ) - { - $periods = $currentPeriod->getSubperiods(); - $currentPeriod = $periods[0]; - } - return $currentPeriod->getDate(); - } - - /** - * Returns the last day of the period ; can be a date in the future - * - * @return Piwik_Date Last day of the period - */ - public function getDateEnd() - { - if(!$this->subperiodsProcessed) - { - $this->generate(); - } - if(count($this->subperiods) == 0) - { - return $this->getDate(); - } - $periods = $this->getSubperiods(); - $currentPeriod = $periods[count($periods)-1]; - while( $currentPeriod->getNumberOfSubperiods() > 0 ) - { - $periods = $currentPeriod->getSubperiods(); - $currentPeriod = $periods[count($periods)-1]; - } - return $currentPeriod->getDate(); - } - - public function getId() - { - return Piwik::$idPeriods[$this->getLabel()]; - } + public function getId() + { + return Piwik::$idPeriods[$this->getLabel()]; + } /** * Returns the label for the current period * @return string */ public function getLabel() - { - return $this->label; - } - - /** - * @return Piwik_Date - */ - protected function getDate() - { - return $this->date; - } + { + return $this->label; + } + + /** + * @return Piwik_Date + */ + protected function getDate() + { + return $this->date; + } /** * Checks if the given date is an instance of Piwik_Date @@ -184,57 +178,54 @@ abstract class Piwik_Period * @throws Exception */ protected function checkInputDate($date) - { - if( !($date instanceof Piwik_Date)) - { - throw new Exception("The date must be a Piwik_Date object. " . var_export($date,true)); - } - } - - protected function generate() - { - $this->subperiodsProcessed = true; - } + { + if (!($date instanceof Piwik_Date)) { + throw new Exception("The date must be a Piwik_Date object. " . var_export($date, true)); + } + } + + protected function generate() + { + $this->subperiodsProcessed = true; + } /** * Returns the number of available subperiods * @return int */ public function getNumberOfSubperiods() - { - if(!$this->subperiodsProcessed) - { - $this->generate(); - } - return count($this->subperiods); - } - - /** - * Returns Period_Day for a period made of days (week, month), - * Period_Month for a period made of months (year) - * - * @return array Piwik_Period - */ - public function getSubperiods() - { - if(!$this->subperiodsProcessed) - { - $this->generate(); - } - return $this->subperiods; - } + { + if (!$this->subperiodsProcessed) { + $this->generate(); + } + return count($this->subperiods); + } - /** - * Add a date to the period. - * - * Protected because it not yet supported to add periods after the initialization - * - * @param Piwik_Period $period Valid Piwik_Period object - */ - protected function addSubperiod( $period ) - { - $this->subperiods[] = $period; - } + /** + * Returns Period_Day for a period made of days (week, month), + * Period_Month for a period made of months (year) + * + * @return array Piwik_Period + */ + public function getSubperiods() + { + if (!$this->subperiodsProcessed) { + $this->generate(); + } + return $this->subperiods; + } + + /** + * Add a date to the period. + * + * Protected because it not yet supported to add periods after the initialization + * + * @param Piwik_Period $period Valid Piwik_Period object + */ + protected function addSubperiod($period) + { + $this->subperiods[] = $period; + } /** * Returns a string representing the current period @@ -245,34 +236,33 @@ abstract class Piwik_Period * @return array */ public function toString($format = "Y-m-d") - { - if(!$this->subperiodsProcessed) - { - $this->generate(); - } - $dateString = array(); - foreach($this->subperiods as $period) - { - $dateString[] = $period->toString($format); - } - return $dateString; - } - - public function __toString() - { - return implode(",", $this->toString()); - } - - public function get( $part= null ) - { - if(!$this->subperiodsProcessed) - { - $this->generate(); - } - return $this->date->toString($part); - } - - abstract public function getPrettyString(); - abstract public function getLocalizedShortString(); - abstract public function getLocalizedLongString(); + { + if (!$this->subperiodsProcessed) { + $this->generate(); + } + $dateString = array(); + foreach ($this->subperiods as $period) { + $dateString[] = $period->toString($format); + } + return $dateString; + } + + public function __toString() + { + return implode(",", $this->toString()); + } + + public function get($part = null) + { + if (!$this->subperiodsProcessed) { + $this->generate(); + } + return $this->date->toString($part); + } + + abstract public function getPrettyString(); + + abstract public function getLocalizedShortString(); + + abstract public function getLocalizedLongString(); } |