Day.php
2 years ago
Factory.php
1 year ago
Month.php
2 years ago
PeriodValidator.php
2 years ago
Range.php
1 year ago
Week.php
1 year ago
Year.php
2 years ago
Factory.php
199 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Period; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\Container\StaticContainer; |
| 13 | use Piwik\Date; |
| 14 | use Piwik\Period; |
| 15 | use Piwik\Piwik; |
| 16 | use Piwik\Plugin; |
| 17 | /** |
| 18 | * Creates Period instances using the values used for the 'period' and 'date' |
| 19 | * query parameters. |
| 20 | * |
| 21 | * ## Custom Periods |
| 22 | * |
| 23 | * Plugins can define their own period factories all plugins to define new period types, in addition |
| 24 | * to "day", "week", "month", "year" and "range". |
| 25 | * |
| 26 | * To define a new period type: |
| 27 | * |
| 28 | * 1. create a new period class that derives from {@see \Piwik\Period}. |
| 29 | * 2. extend this class in a new PeriodFactory class and put it in /path/to/piwik/plugins/MyPlugin/PeriodFactory.php |
| 30 | * |
| 31 | * Period name collisions: |
| 32 | * |
| 33 | * If two plugins try to handle the same period label, the first one encountered will |
| 34 | * be used. In other words, avoid using another plugin's period label. |
| 35 | */ |
| 36 | abstract class Factory |
| 37 | { |
| 38 | public function __construct() |
| 39 | { |
| 40 | // empty |
| 41 | } |
| 42 | /** |
| 43 | * Returns true if this factory should handle the period/date string combination. |
| 44 | * |
| 45 | * @return bool |
| 46 | */ |
| 47 | public abstract function shouldHandle($strPeriod, $strDate); |
| 48 | /** |
| 49 | * Creates a period using the value of the 'date' query parameter. |
| 50 | * |
| 51 | * @param string $strPeriod |
| 52 | * @param string|Date $date |
| 53 | * @param string $timezone |
| 54 | * @return Period |
| 55 | */ |
| 56 | public abstract function make($strPeriod, $date, $timezone); |
| 57 | /** |
| 58 | * Creates a new Period instance with a period ID and {@link Date} instance. |
| 59 | * |
| 60 | * _Note: This method cannot create {@link Period\Range} periods._ |
| 61 | * |
| 62 | * @param string $period `"day"`, `"week"`, `"month"`, `"year"`, `"range"`. |
| 63 | * @param Date|string $date A date within the period or the range of dates. |
| 64 | * @param Date|string $timezone Optional timezone that will be used only when $period is 'range' or $date is 'last|previous' |
| 65 | * @throws Exception If `$strPeriod` is invalid or $date is invalid. |
| 66 | * @return \Piwik\Period |
| 67 | */ |
| 68 | public static function build($period, $date, $timezone = 'UTC') |
| 69 | { |
| 70 | self::checkPeriodIsEnabled($period); |
| 71 | if (is_string($date)) { |
| 72 | [$period, $date] = self::convertRangeToDateIfNeeded($period, $date); |
| 73 | if (Period::isMultiplePeriod($date, $period) || $period == 'range') { |
| 74 | return new \Piwik\Period\Range($period, $date, $timezone); |
| 75 | } |
| 76 | $dateObject = Date::factory($date); |
| 77 | } elseif ($date instanceof Date) { |
| 78 | $dateObject = $date; |
| 79 | } else { |
| 80 | throw new \Exception("Invalid date supplied to Period\\Factory::build(): " . gettype($date)); |
| 81 | } |
| 82 | switch ($period) { |
| 83 | case 'day': |
| 84 | return new \Piwik\Period\Day($dateObject); |
| 85 | case 'week': |
| 86 | return new \Piwik\Period\Week($dateObject); |
| 87 | case 'month': |
| 88 | return new \Piwik\Period\Month($dateObject); |
| 89 | case 'year': |
| 90 | return new \Piwik\Period\Year($dateObject); |
| 91 | } |
| 92 | /** @var string[] $customPeriodFactories */ |
| 93 | $customPeriodFactories = Plugin\Manager::getInstance()->findComponents('PeriodFactory', self::class); |
| 94 | foreach ($customPeriodFactories as $customPeriodFactoryClass) { |
| 95 | $customPeriodFactory = StaticContainer::get($customPeriodFactoryClass); |
| 96 | if ($customPeriodFactory->shouldHandle($period, $date)) { |
| 97 | return $customPeriodFactory->make($period, $date, $timezone); |
| 98 | } |
| 99 | } |
| 100 | throw new \Exception("Don't know how to create a '{$period}' period! (date = {$date})"); |
| 101 | } |
| 102 | public static function checkPeriodIsEnabled($period) |
| 103 | { |
| 104 | if (!self::isPeriodEnabledForAPI($period)) { |
| 105 | self::throwExceptionInvalidPeriod($period); |
| 106 | } |
| 107 | } |
| 108 | /** |
| 109 | * @param $strPeriod |
| 110 | * @throws \Exception |
| 111 | */ |
| 112 | private static function throwExceptionInvalidPeriod($strPeriod) |
| 113 | { |
| 114 | $periods = self::getPeriodsEnabledForAPI(); |
| 115 | $periods = implode(", ", $periods); |
| 116 | $message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, $periods)); |
| 117 | throw new Exception($message); |
| 118 | } |
| 119 | private static function convertRangeToDateIfNeeded($period, $date) |
| 120 | { |
| 121 | if (is_string($period) && is_string($date) && $period === 'range') { |
| 122 | $dates = explode(',', $date); |
| 123 | if (count($dates) === 2 && $dates[0] === $dates[1]) { |
| 124 | $period = 'day'; |
| 125 | $date = $dates[0]; |
| 126 | } |
| 127 | } |
| 128 | return array($period, $date); |
| 129 | } |
| 130 | /** |
| 131 | * Creates a Period instance using a period, date and timezone. |
| 132 | * |
| 133 | * @param string $timezone The timezone of the date. Only used if `$date` is `'now'`, `'today'`, |
| 134 | * `'yesterday'` or `'yesterdaySameTime'`. |
| 135 | * @param string $period The period string: `"day"`, `"week"`, `"month"`, `"year"`, `"range"`. |
| 136 | * @param string $date The date or date range string. Can be a special value including |
| 137 | * `'now'`, `'today'`, `'yesterday'`, `'yesterdaySameTime'`. |
| 138 | * @return \Piwik\Period |
| 139 | */ |
| 140 | public static function makePeriodFromQueryParams($timezone, $period, $date) |
| 141 | { |
| 142 | if (empty($timezone)) { |
| 143 | $timezone = 'UTC'; |
| 144 | } |
| 145 | [$period, $date] = self::convertRangeToDateIfNeeded($period, $date); |
| 146 | if ($period == 'range') { |
| 147 | self::checkPeriodIsEnabled('range'); |
| 148 | $oPeriod = new \Piwik\Period\Range('range', $date, $timezone, Date::factory('today', $timezone)); |
| 149 | } else { |
| 150 | if (!$date instanceof Date) { |
| 151 | if (preg_match('/^(now|today|yesterday|yesterdaySameTime|last[ -]?(?:week|month|year))$/i', $date)) { |
| 152 | $date = Date::factoryInTimezone($date, $timezone); |
| 153 | } |
| 154 | $date = Date::factory($date); |
| 155 | } |
| 156 | $oPeriod = \Piwik\Period\Factory::build($period, $date); |
| 157 | } |
| 158 | return $oPeriod; |
| 159 | } |
| 160 | /** |
| 161 | * @param $period |
| 162 | * @return bool |
| 163 | */ |
| 164 | public static function isPeriodEnabledForAPI($period) |
| 165 | { |
| 166 | $periodValidator = new \Piwik\Period\PeriodValidator(); |
| 167 | return $periodValidator->isPeriodAllowedForAPI($period); |
| 168 | } |
| 169 | /** |
| 170 | * @return array |
| 171 | */ |
| 172 | public static function getPeriodsEnabledForAPI() |
| 173 | { |
| 174 | $periodValidator = new \Piwik\Period\PeriodValidator(); |
| 175 | return $periodValidator->getPeriodsAllowedForAPI(); |
| 176 | } |
| 177 | public static function isAnyLowerPeriodDisabledForAPI($periodLabel) |
| 178 | { |
| 179 | $parentPeriod = null; |
| 180 | switch ($periodLabel) { |
| 181 | case 'week': |
| 182 | $parentPeriod = 'day'; |
| 183 | break; |
| 184 | case 'month': |
| 185 | $parentPeriod = 'week'; |
| 186 | break; |
| 187 | case 'year': |
| 188 | $parentPeriod = 'month'; |
| 189 | break; |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | if ($parentPeriod === null) { |
| 194 | return \false; |
| 195 | } |
| 196 | return !self::isPeriodEnabledForAPI($parentPeriod) || self::isAnyLowerPeriodDisabledForAPI($parentPeriod); |
| 197 | } |
| 198 | } |
| 199 |