Completed
Pull Request — master (#71)
by ignace nyamagana
04:02
created

Datepoint::getDay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * League.Period (https://period.thephpleague.com).
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\Period;
15
16
use DateInterval;
17
use DateTime;
18
use DateTimeImmutable;
19
use DateTimeInterface;
20
use DateTimeZone;
21
use function filter_var;
22
use function intdiv;
23
use const FILTER_VALIDATE_INT;
24
25
/**
26
 * League Period Datepoint.
27
 *
28
 * @package League.period
29
 * @author  Ignace Nyamagana Butera <[email protected]>
30
 * @since   4.2.0
31
 */
32
final class Datepoint extends DateTimeImmutable
33
{
34
    /**
35
     * Returns a position in time expressed as a DateTimeImmutable object.
36
     *
37
     * A datepoint can be
38
     * <ul>
39
     * <li>a DateTimeInterface object
40
     * <li>a integer interpreted as a timestamp
41
     * <li>a string parsable by DateTime::__construct
42
     * </ul>
43
     *
44
     * @param mixed $datepoint a position in time
45
     */
46
    public static function create($datepoint): self
47
    {
48
        if ($datepoint instanceof self) {
49
            return $datepoint;
50
        }
51
52
        if ($datepoint instanceof DateTimeInterface) {
53
            return new self($datepoint->format('Y-m-d H:i:s.u'), $datepoint->getTimezone());
54
        }
55
56
        if (false !== ($timestamp = filter_var($datepoint, FILTER_VALIDATE_INT))) {
57
            return new self('@'.$timestamp);
58
        }
59
60
        return new self($datepoint);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     *
66
     * @param string $format
67
     * @param string $datetime
68
     *
69
     * @return self|bool
70
     */
71
    public static function createFromFormat($format, $datetime, DateTimeZone $timezone = null)
72
    {
73
        $datepoint = parent::createFromFormat($format, $datetime, $timezone);
74
        if (false !== $datepoint) {
75
            return self::create($datepoint);
76
        }
77
78
        return $datepoint;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     *
84
     * @param DateTime $datetime
85
     */
86
    public static function createFromMutable($datetime): self
87
    {
88
        return self::create(parent::createFromMutable($datetime));
89
    }
90
91
    /**
92
     * Returns a Period instance.
93
     *
94
     *  - the starting datepoint represents the beginning of the current datepoint second
95
     *  - the duration is equal to 1 second
96
     */
97
    public function getSecond(): Period
98
    {
99
        $datepoint = $this->setTime(
100
            (int) $this->format('H'),
101
            (int) $this->format('i'),
102
            (int) $this->format('s')
103
        );
104
105
        return new Period($datepoint, $datepoint->add(new DateInterval('PT1S')));
106
    }
107
108
    /**
109
     * Returns a Period instance.
110
     *
111
     *  - the starting datepoint represents the beginning of the current datepoint minute
112
     *  - the duration is equal to 1 minute
113
     */
114
    public function getMinute(): Period
115
    {
116
        $datepoint = $this->setTime((int) $this->format('H'), (int) $this->format('i'), 0);
117
118
        return new Period($datepoint, $datepoint->add(new DateInterval('PT1M')));
119
    }
120
121
    /**
122
     * Returns a Period instance.
123
     *
124
     *  - the starting datepoint represents the beginning of the current datepoint hour
125
     *  - the duration is equal to 1 hour
126
     */
127
    public function getHour(): Period
128
    {
129
        $datepoint = $this->setTime((int) $this->format('H'), 0);
130
131
        return new Period($datepoint, $datepoint->add(new DateInterval('PT1H')));
132
    }
133
134
    /**
135
     * Returns a Period instance.
136
     *
137
     *  - the starting datepoint represents the beginning of the current datepoint day
138
     *  - the duration is equal to 1 day
139
     */
140
    public function getDay(): Period
141
    {
142
        $datepoint = $this->setTime(0, 0);
143
144
        return new Period($datepoint, $datepoint->add(new DateInterval('P1D')));
145
    }
146
147
    /**
148
     * Returns a Period instance.
149
     *
150
     *  - the starting datepoint represents the beginning of the current datepoint iso week day
151
     *  - the duration is equal to 7 days
152
     */
153
    public function getIsoWeek(): Period
154
    {
155
        $startDate = $this
156
            ->setTime(0, 0)
157
            ->setISODate((int) $this->format('o'), (int) $this->format('W'), 1);
158
159
        return new Period($startDate, $startDate->add(new DateInterval('P7D')));
160
    }
161
162
    /**
163
     * Returns a Period instance.
164
     *
165
     *  - the starting datepoint represents the beginning of the current datepoint month
166
     *  - the duration is equal to 1 month
167
     */
168
    public function getMonth(): Period
169
    {
170
        $startDate = $this
171
            ->setTime(0, 0)
172
            ->setDate((int) $this->format('Y'), (int) $this->format('n'), 1);
173
174
        return new Period($startDate, $startDate->add(new DateInterval('P1M')));
175
    }
176
177
    /**
178
     * Returns a Period instance.
179
     *
180
     *  - the starting datepoint represents the beginning of the current datepoint quarter
181
     *  - the duration is equal to 3 months
182
     */
183
    public function getQuarter(): Period
184
    {
185
        $startDate = $this
186
            ->setTime(0, 0)
187
            ->setDate((int) $this->format('Y'), (intdiv((int) $this->format('n'), 3) * 3) + 1, 1);
188
189
        return new Period($startDate, $startDate->add(new DateInterval('P3M')));
190
    }
191
192
    /**
193
     * Returns a Period instance.
194
     *
195
     *  - the starting datepoint represents the beginning of the current datepoint semester
196
     *  - the duration is equal to 6 months
197
     */
198
    public function getSemester(): Period
199
    {
200
        $startDate = $this
201
            ->setTime(0, 0)
202
            ->setDate((int) $this->format('Y'), (intdiv((int) $this->format('n'), 6) * 6) + 1, 1);
203
204
        return new Period($startDate, $startDate->add(new DateInterval('P6M')));
205
    }
206
207
    /**
208
     * Returns a Period instance.
209
     *
210
     *  - the starting datepoint represents the beginning of the current datepoint year
211
     *  - the duration is equal to 1 year
212
     */
213
    public function getYear(): Period
214
    {
215
        $year = (int) $this->format('Y');
216
        $datepoint = $this->setTime(0, 0);
217
218
        return new Period($datepoint->setDate($year, 1, 1), $datepoint->setDate(++$year, 1, 1));
219
    }
220
221
    /**
222
     * Returns a Period instance.
223
     *
224
     *  - the starting datepoint represents the beginning of the current datepoint iso year
225
     *  - the duration is equal to 1 iso year
226
     */
227
    public function getIsoYear(): Period
228
    {
229
        $year = (int) $this->format('o');
230
        $datepoint = $this->setTime(0, 0);
231
232
        return new Period($datepoint->setISODate($year, 1, 1), $datepoint->setISODate(++$year, 1, 1));
233
    }
234
}
235