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