1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Level23\Druid\Concerns; |
5
|
|
|
|
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use Level23\Druid\Interval\Interval; |
8
|
|
|
|
9
|
|
|
trait HasIntervals |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array|\Level23\Druid\Interval\IntervalInterface[] |
13
|
|
|
*/ |
14
|
|
|
protected array $intervals = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Add the interval, e.g. the date where we want to select data from. |
18
|
|
|
* |
19
|
|
|
* You should specify the interval in string form like "$start/$stop" format, or give two parameters |
20
|
|
|
* where each parameter should be a DateTime object, unix timestamp or string accepted by DateTime::__construct. |
21
|
|
|
* |
22
|
|
|
* All these examples are valid: |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* // Select an interval with string values. Anything which can be parsed by the DateTime object |
26
|
|
|
* // can be given. Also, "yesterday" or "now" is valid. |
27
|
|
|
* interval('2019-12-23', '2019-12-24'); |
28
|
|
|
* |
29
|
|
|
* // When a string is given which contains a slash, we will split it for you and parse it as "begin/end". |
30
|
|
|
* interval('yesterday/now'); |
31
|
|
|
* |
32
|
|
|
* // An "raw" interval as druid uses them is also allowed |
33
|
|
|
* interval('2015-09-12T00:00:00.000Z/2015-09-13T00:00:00.000Z'); |
34
|
|
|
* |
35
|
|
|
* // You can also give DateTime objects |
36
|
|
|
* interval(new DateTime('yesterday'), new DateTime('now')); |
37
|
|
|
* |
38
|
|
|
* // Carbon is also supported, as it extends DateTime |
39
|
|
|
* interval(Carbon::now()->subDay(), Carbon::now()); |
40
|
|
|
* |
41
|
|
|
* // Timestamps are also supported: |
42
|
|
|
* interval(1570643085, 1570729485); |
43
|
|
|
* ``` |
44
|
|
|
* |
45
|
|
|
* @param \DateTimeInterface|int|string $start DateTime object, unix timestamp or string accepted by |
46
|
|
|
* DateTime::__construct |
47
|
|
|
* @param \DateTimeInterface|int|string|null $stop DateTime object, unix timestamp or string accepted by |
48
|
|
|
* DateTime::__construct |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
47 |
|
public function interval(DateTimeInterface|int|string $start, DateTimeInterface|int|string|null $stop = null): self |
54
|
|
|
{ |
55
|
47 |
|
$this->intervals[] = new Interval($start, $stop); |
56
|
|
|
|
57
|
46 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array|\Level23\Druid\Interval\IntervalInterface[] |
62
|
|
|
*/ |
63
|
2 |
|
public function getIntervals(): array |
64
|
|
|
{ |
65
|
2 |
|
return $this->intervals; |
66
|
|
|
} |
67
|
|
|
} |