Completed
Push — master ( f1ae35...ad67fa )
by Philip
23:55
created

FlexDate::getDay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
namespace Dontdrinkandroot\Date;
5
6
class FlexDate
7
{
8
9
    const PRECISION_YEAR = 'year';
10
11
    const PRECISION_MONTH = 'month';
12
13
    const PRECISION_DAY = 'day';
14
15
    /**
16
     * @var int|null
17
     */
18
    protected $year;
19
20
    /**
21
     * @var int|null
22
     */
23
    protected $month;
24
25
    /**
26
     * @var int|null
27
     */
28
    protected $day;
29
30
    /**
31
     * @param int|null $year
32
     * @param int|null $month
33
     * @param int|null $day
34
     */
35
    public function __construct($year = null, $month = null, $day = null)
36
    {
37
        $this->year = $year;
38
        $this->month = $month;
39
        $this->day = $day;
40
    }
41
42
    /**
43
     * @return int|null
44
     */
45
    public function getYear()
46
    {
47
        return $this->year;
48
    }
49
50
    /**
51
     * @param int|null $year
52
     */
53
    public function setYear($year)
54
    {
55
        $this->year = $year;
56
    }
57
58
    /**
59
     * @return int|null
60
     */
61
    public function getMonth()
62
    {
63
        return $this->month;
64
    }
65
66
    /**
67
     * @param int|null $month
68
     */
69
    public function setMonth($month)
70
    {
71
        $this->month = $month;
72
    }
73
74
    /**
75
     * @return int|null
76
     */
77
    public function getDay()
78
    {
79
        return $this->day;
80
    }
81
82
    /**
83
     * @param int|null $day
84
     */
85
    public function setDay($day)
86
    {
87
        $this->day = $day;
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function hasValue()
94
    {
95
        return null !== $this->year || null !== $this->month || null !== $this->day;
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    public function isCompleteDate()
102
    {
103
        return null !== $this->year && null !== $this->month && null !== $this->day;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isValid()
110
    {
111
        try {
112
            $this->assertValid();
113
114
            return true;
115
        } catch (\Exception $e) {
116
            return false;
117
        }
118
    }
119
120
    /**
121
     * @return bool
122
     * @throws \Exception
123
     */
124
    public function assertValid()
125
    {
126
        if (null !== $this->day && null === $this->month) {
127
            throw new \Exception('Day set, but no month');
128
        }
129
130
        if (null !== $this->month && null === $this->year) {
131
            throw new \Exception('Month, but no year');
132
        }
133
134
        return true;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    public function isValidDate()
141
    {
142
        return checkdate($this->month, $this->day, $this->year);
143
    }
144
145
    /**
146
     * @¶eturn \DateTime
147
     */
148
    public function toDateTime()
149
    {
150
        $dateTime = new \DateTime();
151
        $inferredYear = $this->year !== null ? $this->year : 0;
152
        $inferredMonth = $this->month !== null ? $this->month : 1;
153
        $inferredDay = $this->day !== null ? $this->day : 1;
154
        $dateTime->setDate($inferredYear, $inferredMonth, $inferredDay);
155
156
        return $dateTime;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function __toString()
163
    {
164
        $string = '';
165
        if (null !== $this->year) {
166
            $string .= $this->year;
167
        }
168
        if (null !== $this->month) {
169
            $string .= '-';
170
            $string .= str_pad($this->month, 2, '0', STR_PAD_LEFT);
171
        }
172
        if (null !== $this->day) {
173
            $string .= '-';
174
            $string .= str_pad($this->day, 2, '0', STR_PAD_LEFT);
175
        }
176
177
        return $string;
178
    }
179
180
    /**
181
     * @param string $dateString
182
     *
183
     * @return FlexDate
184
     */
185
    public static function fromString($dateString)
186
    {
187
        $flexDate = new FlexDate();
188
        if (empty($dateString)) {
189
            return $flexDate;
190
        }
191
192
        $parts = explode('-', $dateString);
193
        $numParts = count($parts);
194
        if ($numParts > 0) {
195
            $flexDate->setYear((int)$parts[0]);
196
        }
197
        if ($numParts > 1) {
198
            $flexDate->setMonth((int)$parts[1]);
199
        }
200
        if ($numParts > 2) {
201
            $flexDate->setDay((int)$parts[2]);
202
        }
203
204
        return $flexDate;
205
    }
206
207
    public function getPrecision()
208
    {
209
        if (null === $this->year) {
210
            return null;
211
        }
212
213
        if (null === $this->month) {
214
            return self::PRECISION_YEAR;
215
        }
216
217
        if (null === $this->day) {
218
            return self::PRECISION_MONTH;
219
        }
220
221
        return self::PRECISION_DAY;
222
    }
223
}
224