TimeRange::__toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace CloudyCity\TencentMarketingSDK\Kernel\Http\Parameters;
4
5
use Carbon\Carbon;
6
7
class TimeRange
8
{
9
    /**
10
     * Starting time.
11
     *
12
     * @var Carbon
13
     */
14
    protected $start;
15
16
    /**
17
     * End time.
18
     *
19
     * @var Carbon
20
     */
21
    protected $end;
22
23
    /**
24
     * TimeRange constructor.
25
     *
26
     * @param null $start
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $start is correct as it would always require null to be passed?
Loading history...
27
     * @param null $end
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $end is correct as it would always require null to be passed?
Loading history...
28
     */
29
    public function __construct($start = null, $end = null)
30
    {
31
        $this->start($start);
32
        $this->end($end);
33
    }
34
35
    /**
36
     * Set start time.
37
     *
38
     * @param $start
39
     *
40
     * @return $this
41
     */
42
    public function start($start)
43
    {
44
        if ($start) {
45
            $this->start = Carbon::make($start);
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * Set end time.
53
     *
54
     * @param $end
55
     *
56
     * @return $this
57
     */
58
    public function end($end)
59
    {
60
        if ($end) {
61
            $this->end = Carbon::make($end);
62
        }
63
64
        return $this;
65
    }
66
67
    /**
68
     * Check start time and end time.
69
     *
70
     * @throws \Exception
71
     */
72
    protected function checkTimeRange()
73
    {
74
        if (!($this->start instanceof Carbon)) {
0 ignored issues
show
introduced by
$this->start is always a sub-type of Carbon\Carbon.
Loading history...
75
            throw new \Exception('开始时间不能为空');
76
        }
77
78
        if (!($this->end instanceof  Carbon)) {
0 ignored issues
show
introduced by
$this->end is always a sub-type of Carbon\Carbon.
Loading history...
79
            throw new \Exception('结束时间不能为空');
80
        }
81
    }
82
83
    /**
84
     * Get the instance as an array.
85
     *
86
     * @return array
87
     */
88
    protected function __toArray()
89
    {
90
        return [
91
            'start_time' => $this->start->getTimestamp(),
92
            'end_time'   => $this->end->getTimestamp(),
93
        ];
94
    }
95
96
    /**
97
     * Check timeCheck time and get the instance as an array.
98
     *
99
     * @throws \Exception
100
     *
101
     * @return array
102
     */
103
    public function toArray()
104
    {
105
        $this->checkTimeRange();
106
107
        return $this->__toArray();
108
    }
109
}
110