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