Passed
Pull Request — master (#71)
by ignace nyamagana
02:00
created

Datepoint::extractMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
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 function filter_var;
21
use function intdiv;
22
use const FILTER_VALIDATE_INT;
23
24
/**
25
 * League Period Datepoint.
26
 *
27
 * @package League.period
28
 * @author  Ignace Nyamagana Butera <[email protected]>
29
 * @since   4.2.0
30
 */
31
final class Datepoint extends DateTimeImmutable
32
{
33
    /**
34
     * Returns a position in time expressed as a DateTimeImmutable object.
35
     *
36
     * A datepoint can be
37
     * <ul>
38
     * <li>a DateTimeInterface object
39
     * <li>a integer interpreted as a timestamp
40
     * <li>a string parsable by DateTime::__construct
41
     * </ul>
42
     *
43
     * @param mixed $datepoint a position in time
44
     */
45
    public static function create($datepoint): DateTimeImmutable
46
    {
47
        if ($datepoint instanceof DateTimeImmutable) {
48
            return new self($datepoint->format('Y-m-d H:i:s.u'), $datepoint->getTimezone());
49
        }
50
51
        if ($datepoint instanceof DateTime) {
52
            return self::createFromMutable($datepoint);
53
        }
54
55
        if (false !== ($timestamp = filter_var($datepoint, FILTER_VALIDATE_INT))) {
56
            return new self('@'.$timestamp);
57
        }
58
59
        return new self($datepoint);
60
    }
61
62
    public function extractDay(): Period
63
    {
64
        $datepoint = $this->setTime(0, 0);
65
66
        return new Period($datepoint, $datepoint->add(new DateInterval('P1D')));
67
    }
68
69
    public function extractIsoWeek(): Period
70
    {
71
        $startDate = $this
72
            ->setTime(0, 0)
73
            ->setISODate((int) $this->format('o'), (int) $this->format('W'), 1);
74
75
        return new Period($startDate, $startDate->add(new DateInterval('P7D')));
76
    }
77
78
    public function extractMonth(): Period
79
    {
80
        $startDate = $this
81
            ->setTime(0, 0)
82
            ->setDate((int) $this->format('Y'), (int) $this->format('n'), 1);
83
84
        return new Period($startDate, $startDate->add(new DateInterval('P1M')));
85
    }
86
87
    public function extractQuarter(): Period
88
    {
89
        $startDate = $this
90
            ->setTime(0, 0)
91
            ->setDate((int) $this->format('Y'), (intdiv((int) $this->format('n'), 3) * 3) + 1, 1);
92
93
        return new Period($startDate, $startDate->add(new DateInterval('P3M')));
94
    }
95
96
    public function extractSemester(): Period
97
    {
98
        $startDate = $this
99
            ->setTime(0, 0)
100
            ->setDate((int) $this->format('Y'), (intdiv((int) $this->format('n'), 6) * 6) + 1, 1);
101
102
        return new Period($startDate, $startDate->add(new DateInterval('P6M')));
103
    }
104
105
    public function extractYear(): Period
106
    {
107
        $year = (int) $this->format('Y');
108
        $datepoint = $this->setTime(0, 0);
109
110
        return new Period($datepoint->setDate($year, 1, 1), $datepoint->setDate(++$year, 1, 1));
111
    }
112
113
114
    public function extractIsoYear(): Period
115
    {
116
        $year = (int) $this->format('o');
117
        $datepoint = $this->setTime(0, 0);
118
119
        return new Period($datepoint->setISODate($year, 1, 1), $datepoint->setISODate(++$year, 1, 1));
120
    }
121
}
122