Passed
Push — master ( 1c0e23...afe80e )
by Andrey
10:06
created

DateTimes::getDateTimeFromTo()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 15
rs 9.6111
1
<?php
2
3
namespace Daaner\NovaPoshta\Traits;
4
5
use Carbon\Carbon;
6
7
trait DateTimes
8
{
9
    protected $dateTime;
10
    protected $dateTimeFrom;
11
    protected $dateTimeTo;
12
    protected $format = 'd.m.Y';
13
14
    /**
15
     * @param string||Carbon||date $dateTime
0 ignored issues
show
Documentation Bug introduced by
The doc comment string||Carbon||date at position 2 could not be parsed: Unknown type name '|' at position 2 in string||Carbon||date.
Loading history...
16
     * @return this
17
     */
18
    public function setDateTime($dateTime)
19
    {
20
        $this->dateTime = $this->checkDate($dateTime);
21
22
        return $this;
23
    }
24
25
    /**
26
     * @param string||Carbon||date $dateTimeFrom
0 ignored issues
show
Documentation Bug introduced by
The doc comment string||Carbon||date at position 2 could not be parsed: Unknown type name '|' at position 2 in string||Carbon||date.
Loading history...
27
     * @return this
28
     */
29
    public function setDateTimeFrom($dateTimeFrom)
30
    {
31
        $this->dateTimeFrom = $this->checkDate($dateTimeFrom);
32
33
        return $this;
34
    }
35
36
    /**
37
     * @param string||Carbon||date $dateTimeTo
0 ignored issues
show
Documentation Bug introduced by
The doc comment string||Carbon||date at position 2 could not be parsed: Unknown type name '|' at position 2 in string||Carbon||date.
Loading history...
38
     * @return this
39
     */
40
    public function setDateTimeTo($dateTimeTo)
41
    {
42
        $this->dateTimeTo = $this->checkDate($dateTimeTo);
43
44
        return $this;
45
    }
46
47
    /**
48
     * @return this
49
     */
50
    public function getDateTime()
51
    {
52
        if ($this->dateTime && (!$this->dateTimeFrom || !$this->dateTimeTo)) {
53
            $this->methodProperties['DateTime'] = $this->dateTime;
1 ignored issue
show
Bug Best Practice introduced by
The property methodProperties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return this
61
     */
62
    public function getDateTimeFromTo()
63
    {
64
        if ($this->dateTimeFrom || $this->dateTimeTo) {
65
            if (!$this->dateTimeTo) {
66
                $this->dateTimeTo = Carbon::now()->format($this->format);
67
            }
68
            if (!$this->dateTimeFrom) {
69
                $this->dateTimeFrom = $this->dateTimeTo;
70
            }
71
72
            $this->methodProperties['DateTimeFrom'] = $this->dateTimeFrom;
1 ignored issue
show
Bug Best Practice introduced by
The property methodProperties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
            $this->methodProperties['DateTimeTo'] = $this->dateTimeTo;
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string||Carbon||date $date
0 ignored issues
show
Documentation Bug introduced by
The doc comment string||Carbon||date at position 2 could not be parsed: Unknown type name '|' at position 2 in string||Carbon||date.
Loading history...
81
     * @return $date
0 ignored issues
show
Documentation Bug introduced by
The doc comment $date at position 0 could not be parsed: Unknown type name '$date' at position 0 in $date.
Loading history...
82
     */
83
    public function checkDate($date)
84
    {
85
        if ($date instanceof Carbon) {
86
            $date = $date->format($this->format);
87
        } else {
88
            try {
89
                $date = Carbon::parse($date)->format($this->format);
90
            } catch (\Exception $e) {
91
                $date = null;
92
            }
93
        }
94
95
        return $date;
96
    }
97
}
98