Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Period.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7edf4ea81083ef7a121532746b4d4546c80f85a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?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
 */
namespace Piwik;
use Exception;
use Piwik\Piwik;
use Piwik\Date;
use Piwik\Period\Day;
use Piwik\Period\Month;
use Piwik\Period\Range;
use Piwik\Period\Week;
use Piwik_Period_Year;

/**
 * Creating a new 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.
 *
 * @package Piwik
 * @subpackage Period
 */
abstract class Period
{
    /**
     * Array of subperiods
     * @var \Piwik\Period[]
     */
    protected $subperiods = array();
    protected $subperiodsProcessed = false;

    /**
     * @var string
     */
    protected $label = null;

    /**
     * @var Date
     */
    protected $date = null;
    static protected $errorAvailablePeriods = 'day, week, month, year, range';

    /**
     * Constructor
     * @param Date $date
     */
    public function __construct($date)
    {
        $this->checkInputDate($date);
        $this->date = clone $date;
    }

    /**
     * @param string $strPeriod "day", "week", "month", "year"
     * @param Date $date Date object
     * @throws Exception
     * @return \Piwik\Period
     */
    static public function factory($strPeriod, Date $date)
    {
        switch ($strPeriod) {
            case 'day':
                return new Day($date);
                break;

            case 'week':
                return new Week($date);
                break;

            case 'month':
                return new 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;
        }
    }

    /**
     * Indicate if $dateString and $period correspond to multiple periods
     *
     * @static
     * @param  $dateString
     * @param  $period
     * @return boolean
     */
    public static function isMultiplePeriod($dateString, $period)
    {
        return
            is_string($dateString)
            && (preg_match('/^(last|previous){1}([0-9]*)$/D', $dateString, $regs)
            || Range::parseDateRange($dateString))
            && $period != 'range';
    }

    /**
     * The advanced factory method is easier to use from the API than the factory
     * method above. It doesn't require an instance of 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 Period.
     *
     * @param string $strPeriod "day", "week", "month", "year", "range"
     * @param string $strDate
     * @return \Piwik\Period
     */
    static public function advancedFactory($strPeriod, $strDate)
    {
        if (Period::isMultiplePeriod($strDate, $strPeriod) || $strPeriod == 'range') {
            return new Range($strPeriod, $strDate);
        }
        return Period::factory($strPeriod, Date::factory($strDate));
    }

    /**
     * Creates a period instance using a Site instance and two strings describing
     * the period & date.
     *
     * @param string $timezone
     * @param string $period The period string: day, week, month, year, range
     * @param string $date The date or date range string.
     * @return \Piwik\Period
     */
    public static function makePeriodFromQueryParams($timezone, $period, $date)
    {
        if (empty($timezone)) {
            $timezone = 'UTC';
        }

        if ($period == 'range') {
            $oPeriod = new Period\Range('range', $date, $timezone, Date::factory('today', $timezone));
        } else {
            if (!($date instanceof Date)) {
                if ($date == 'now' || $date == 'today') {
                    $date = date('Y-m-d', Date::factory('now', $timezone)->getTimestamp());
                } elseif ($date == 'yesterday' || $date == 'yesterdaySameTime') {
                    $date = date('Y-m-d', Date::factory('now', $timezone)->subDay(1)->getTimestamp());
                }
                $date = Date::factory($date);
            }
            $oPeriod = Period::factory($period, $date);
        }
        return $oPeriod;
    }

    /**
     * Returns the first day of the period
     *
     * @return 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 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()];
    }

    /**
     * Returns the label for the current period
     * @return string
     */
    public function getLabel()
    {
        return $this->label;
    }

    /**
     * @return Date
     */
    protected function getDate()
    {
        return $this->date;
    }

    /**
     * Checks if the given date is an instance of Date
     *
     * @param Date $date
     *
     * @throws Exception
     */
    protected function checkInputDate($date)
    {
        if (!($date instanceof Date)) {
            throw new Exception("The date must be a 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 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 Period object
     */
    protected function addSubperiod($period)
    {
        $this->subperiods[] = $period;
    }

    /**
     * Returns a string representing the current period
     * Given param will be used to format the returned value
     *
     * @param string $format
     *
     * @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();

    public function getRangeString()
    {
        return $this->getDateStart()->toString("Y-m-d") . "," . $this->getDateEnd()->toString("Y-m-d");
    }
}