DateTimes::setDateTimeTo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Daaner\NovaPoshta\Traits;
4
5
use Carbon\Carbon;
6
use Exception;
7
use Illuminate\Support\Facades\Date;
8
9
trait DateTimes
10
{
11
    protected $dateTime;
12
    protected $dateTimeFrom;
13
    protected $dateBegin;
14
    protected $dateEnd;
15
    protected $dateTimeTo;
16
    protected $format = 'd.m.Y';
17
    protected $formatTime = 'd.m.Y H:i:s';
18
19
    /**
20
     * @param  string|Carbon|date  $dateTime  Указание даты
21
     * @return $this
22
     */
23
    public function setDateTime($dateTime): self
24
    {
25
        $this->dateTime = $this->checkDate($dateTime);
26
27
        return $this;
28
    }
29
30
    /**
31
     * @param  string|Carbon|date  $dateTimeFrom  С текущей даты
32
     * @return $this
33
     */
34
    public function setDateTimeFrom($dateTimeFrom): self
35
    {
36
        $this->dateTimeFrom = $this->checkDate($dateTimeFrom);
37
38
        return $this;
39
    }
40
41
    /**
42
     * @param  string|Carbon|date  $dateTimeTo  До текущей даты
43
     * @return $this
44
     */
45
    public function setDateTimeTo($dateTimeTo): self
46
    {
47
        $this->dateTimeTo = $this->checkDate($dateTimeTo);
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return void
54
     */
55
    public function getDateTime(): void
56
    {
57
        if ($this->dateTime && (! $this->dateTimeFrom || ! $this->dateTimeTo)) {
58
            $this->methodProperties['DateTime'] = $this->dateTime;
59
        }
60
61
        if (! $this->dateTime) {
62
            $this->dateTime = Carbon::now()->format($this->format);
63
            $this->methodProperties['DateTime'] = $this->dateTime;
64
        }
65
    }
66
67
    /**
68
     * @return void
69
     */
70
    public function getDateTimeFromTo(): void
71
    {
72
        if ($this->dateTimeFrom || $this->dateTimeTo) {
73
            if (! $this->dateTimeTo) {
74
                $this->dateTimeTo = Carbon::now()->format($this->format);
75
            }
76
            if (! $this->dateTimeFrom) {
77
                $this->dateTimeFrom = $this->dateTimeTo;
78
            }
79
80
            $this->methodProperties['DateTimeFrom'] = $this->dateTimeFrom;
81
            $this->methodProperties['DateTimeTo'] = $this->dateTimeTo;
82
        }
83
    }
84
85
    /**
86
     * Странно, но тут с минутами и секундами.
87
     *
88
     * @param  string|Carbon|date|null  $from  С текущей даты
89
     * @param  string|Carbon|date|null  $to  До текущей даты
90
     * @return void
91
     */
92
    public function getDateFromTo($from = null, $to = null): void
93
    {
94
        if ($from) {
95
            $from = $this->checkDate($from, $this->formatTime);
96
        } else {
97
            $from = Carbon::now()->/** @scrutinizer ignore-call */subMonths(3)
98
                ->format($this->formatTime);
99
        }
100
101
        if ($to) {
102
            $to = $this->checkDate($to, $this->formatTime);
103
        } else {
104
            $to = Carbon::now()->format($this->formatTime);
105
        }
106
107
        $this->methodProperties['DateFrom'] = $from;
108
        $this->methodProperties['DateTo'] = $to;
109
    }
110
111
    /**
112
     * Проверка даты на валидность.
113
     *
114
     * @param  string|Carbon|date  $date  Дата
115
     * @param  string|null  $format  Формат даты
116
     * @return string $date
117
     */
118
    public function checkDate($date, ?string $format = null): string
119
    {
120
        if (! $format) {
121
            $format = $this->format;
122
        }
123
124
        if ($date instanceof Carbon) {
125
            $date = $date->format($format);
126
        } else {
127
            try {
128
                $date = Carbon::parse($date)->format($format);
129
            } catch (Exception $e) {
130
                $date = Carbon::now()->format($format);
131
            }
132
        }
133
134
        return $date;
135
    }
136
137
    /**
138
     * @param  string|Carbon|date  $dateBegin  Дата начала
139
     * @return $this
140
     */
141
    public function setDateBegin($dateBegin): self
142
    {
143
        $this->dateBegin = $this->checkDate($dateBegin, $this->format);
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param  string|Carbon|date  $dateEnd  Дата окончания
150
     * @return $this
151
     */
152
    public function setDateEnd($dateEnd): self
153
    {
154
        $this->dateEnd = $this->checkDate($dateEnd, $this->format);
155
156
        return $this;
157
    }
158
159
    /**
160
     * @return void
161
     */
162
    public function getDateBeginEnd(): void
163
    {
164
        if ($this->dateBegin) {
165
            $this->methodProperties['BeginDate'] = $this->dateBegin;
166
        }
167
168
        if ($this->dateEnd) {
169
            $this->methodProperties['EndDate'] = $this->dateEnd;
170
        }
171
    }
172
}
173