Range::getInterval()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spiral\Statistics\Extract;
4
5
use Spiral\Statistics\Exceptions\InvalidExtractRangeException;
6
use Spiral\Statistics\Extract;
7
8
class Range
9
{
10
    const DAILY   = 'day';
11
    const WEEKLY  = 'week';
12
    const MONTHLY = 'month';
13
    const YEARLY  = 'year';
14
15
    const RANGES = [self::DAILY, self::WEEKLY, self::MONTHLY, self::YEARLY];
16
17
    /** @var string */
18
    private $range;
19
20
    /** @var \DateInterval */
21
    private $interval;
22
23
    /** @var string */
24
    private $format;
25
26
    /** @var string */
27
    private $field;
28
29
    /**
30
     * RangeState constructor.
31
     *
32
     * @param string $range
33
     */
34
    public function __construct(string $range)
35
    {
36
        if (!in_array($range, self::RANGES)) {
37
            throw new InvalidExtractRangeException($range);
38
        }
39
40
        $this->range = $range;
41
        $this->define();
42
    }
43
44
    /**
45
     * Define range values.
46
     */
47
    protected function define()
48
    {
49
        $interval = null;
50
        $format = null;
51
        $field = null;
52
53
        switch ($this->range) {
54
            case self::DAILY:
55
                $interval = 'P1D';
56
                $format = 'M, d Y';
57
                $field = 'day_mark';
58
                break;
59
60
            case self::WEEKLY:
61
                $interval = 'P7D';
62
                $format = 'W, Y';
63
                $field = 'week_mark';
64
                break;
65
66
            case self::MONTHLY:
67
                $interval = 'P1M';
68
                $format = 'M, Y';
69
                $field = 'month_mark';
70
                break;
71
72
            case self::YEARLY:
73
                $interval = 'P1Y';
74
                $format = 'Y';
75
                $field = 'year_mark';
76
                break;
77
        }
78
79
        $this->interval = new \DateInterval($interval);
80
        $this->format = $format;
81
        $this->field = $field;
82
    }
83
84
    /**
85
     * @return \DateInterval
86
     */
87
    public function getInterval(): \DateInterval
88
    {
89
        return $this->interval;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getFormat(): string
96
    {
97
        return $this->format;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getField(): string
104
    {
105
        return $this->field;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getRange(): string
112
    {
113
        return $this->range;
114
    }
115
}