HasInterval   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 4
c 1
b 0
f 0
dl 0
loc 49
ccs 3
cts 3
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A interval() 0 5 1
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
}