Completed
Push — master ( 7f186d...ef28ef )
by Philip
03:23
created

FlexDate::hasValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 3
nop 0
crap 3
1
<?php
2
3
4
namespace Dontdrinkandroot\Date;
5
6
class FlexDate
7
{
8
9
    /**
10
     * @var int|null
11
     */
12
    protected $year;
13
14
    /**
15
     * @var int|null
16
     */
17
    protected $month;
18
19
    /**
20
     * @var int|null
21
     */
22
    protected $day;
23
24
    /**
25
     * @param int|null $year
26
     * @param int|null $month
27
     * @param int|null $day
28
     */
29
    public function __construct($year = null, $month = null, $day = null)
30
    {
31
32
        $this->year = $year;
33
        $this->month = $month;
34
        $this->day = $day;
35 7
    }
36
37 7
    /**
38 7
     * @return int|null
39 7
     */
40 7
    public function getYear()
41
    {
42
        return $this->year;
43
    }
44
45 1
    /**
46
     * @param int|null $year
47 1
     */
48
    public function setYear($year)
49
    {
50
        $this->year = $year;
51
    }
52
53 5
    /**
54
     * @return int|null
55 5
     */
56 5
    public function getMonth()
57
    {
58
        return $this->month;
59
    }
60
61 1
    /**
62
     * @param int|null $month
63 1
     */
64
    public function setMonth($month)
65
    {
66
        $this->month = $month;
67
    }
68
69 5
    /**
70
     * @return int|null
71 5
     */
72 5
    public function getDay()
73
    {
74
        return $this->day;
75
    }
76
77 1
    /**
78
     * @param int|null $day
79 1
     */
80
    public function setDay($day)
81
    {
82
        $this->day = $day;
83
    }
84
85 5
    /**
86
     * @return bool
87 5
     */
88 5
    public function hasValue()
89
    {
90
        return null !== $this->year || null !== $this->month || null !== $this->day;
91
    }
92
93 1
    /**
94
     * @return bool
95 1
     */
96
    public function isCompleteDate()
97
    {
98
        return null !== $this->year && null !== $this->month && null !== $this->day;
99
    }
100
101 2
    /**
102
     * @return bool
103 2
     */
104
    public function isValid()
105
    {
106
        try {
107
            $this->assertValid();
108
109 2
            return true;
110
        } catch (\Exception $e) {
111
            return false;
112 2
        }
113
    }
114 1
115 1
    /**
116 1
     * @return bool
117
     * @throws \Exception
118
     */
119
    public function assertValid()
120
    {
121 View Code Duplication
        if (null !== $this->day && null === $this->month) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
            throw new \Exception('Day set, but no month');
123
        }
124 2
125 View Code Duplication
        if (null !== $this->month && null === $this->year) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126 2
            throw new \Exception('Month, but no year');
127 1
        }
128
129
        return true;
130 2
    }
131 1
132
    /**
133
     * @return bool
134 1
     */
135
    public function isValidDate()
136
    {
137
        return checkdate($this->month, $this->day, $this->year);
138
    }
139
140 2
    /**
141
     * @¶eturn \DateTime
142 2
     */
143
    public function toDateTime()
144
    {
145
        $dateTime = new \DateTime();
146
        $inferredYear = $this->year !== null ? $this->year : 0;
147
        $inferredMonth = $this->month !== null ? $this->month : 1;
148 1
        $inferredDay = $this->day !== null ? $this->day : 1;
149
        $dateTime->setDate($inferredYear, $inferredMonth, $inferredDay);
150 1
151 1
        return $dateTime;
152 1
    }
153 1
154 1
    /**
155
     * {@inheritdoc}
156 1
     */
157
    public function __toString()
158
    {
159
        $string = '';
160
        if (null !== $this->year) {
161
            $string .= $this->year;
162 1
        }
163 View Code Duplication
        if (null !== $this->month) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164 1
            $string .= '-';
165 1
            $string .= str_pad($this->month, 2, '0', STR_PAD_LEFT);
166 1
        }
167 1 View Code Duplication
        if (null !== $this->day) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168 1
            $string .= '-';
169 1
            $string .= str_pad($this->day, 2, '0', STR_PAD_LEFT);
170 1
        }
171 1
172 1
        return $string;
173 1
    }
174 1
175 1
    /**
176
     * @param string $dateString
177 1
     *
178
     * @return FlexDate
179
     */
180
    public static function fromString($dateString)
181
    {
182
        $flexDate = new FlexDate();
183
        if (empty($dateString)) {
184
            return $flexDate;
185 1
        }
186
187 1
        $parts = explode('-', $dateString);
188 1
        $numParts = count($parts);
189 1
        if ($numParts > 0) {
190
            $flexDate->setYear((int)$parts[0]);
191
        }
192 1
        if ($numParts > 1) {
193 1
            $flexDate->setMonth((int)$parts[1]);
194 1
        }
195 1
        if ($numParts > 2) {
196 1
            $flexDate->setDay((int)$parts[2]);
197 1
        }
198 1
199 1
        return $flexDate;
200 1
    }
201
}
202