Passed
Push — new-api ( f151f9...5a646f )
by Sebastian
04:44
created

DateTime::getArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2017 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Rendering\Date;
12
13
use DateTimeZone;
14
use Exception;
15
use Seboettg\CiteProc\Exception\InvalidDateTimeException;
16
17
class DateTime extends \DateTime
18
{
19
    /**
20
     * @var int
21
     */
22
    private $year = 0;
23
24
    /**
25
     * @var int
26
     */
27
    private $month = 0;
28
29
    /**
30
     * @var int
31
     */
32
    private $day = 0;
33
34
    /**
35
     * DateTime constructor.
36
     * @param int|string $year
37
     * @param int|string $month
38
     * @param int|string $day
39
     * @throws Exception
40
     */
41 63
    public function __construct($year, $month, $day)
42
    {
43
        try {
44 63
            parent::__construct(sprintf("%s-%s-%s", $year, $month, $day), new DateTimeZone("Europe/Berlin"));
45
        } catch (Exception $e) {
46
            throw new InvalidDateTimeException(
47
                sprintf("Could not create valid date with year=%s, month=%s, day=%s.", $year, $month, $day)
48
            );
49
        }
50
51 63
        $this->year = intval(self::format("Y"));
0 ignored issues
show
Bug Best Practice introduced by
The method DateTime::format() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $this->year = intval(self::/** @scrutinizer ignore-call */ format("Y"));
Loading history...
52 63
        $this->month = intval(self::format("n"));
53 63
        $this->day = intval(self::format("j"));
54 63
    }
55
56
    /**
57
     * @param int $year
58
     * @return $this
59
     */
60 1
    public function setYear(int $year): DateTime
61
    {
62 1
        $this->year = $year;
63 1
        return $this;
64
    }
65
66
    /**
67
     * @param int $month
68
     * @return $this
69
     */
70 32
    public function setMonth(int $month): DateTime
71
    {
72 32
        $this->month = $month;
73 32
        return $this;
74
    }
75
76
    /**
77
     * @param int $day
78
     * @return $this
79
     */
80 37
    public function setDay(int $day): DateTime
81
    {
82 37
        $this->day = $day;
83 37
        return $this;
84
    }
85
86
    /**
87
     * @param int $year
88
     * @param int $month
89
     * @param int $day
90
     * @return $this
91
     */
92
    public function setDate($year, $month, $day): DateTime
93
    {
94
        parent::setDate($year, $month, $day);
95
        $this->year = $year;
96
        $this->month = $month;
97
        $this->day = $day;
98
        return $this;
99
    }
100
101
    /**
102
     * @return int
103
     */
104 61
    public function getYear(): int
105
    {
106 61
        return $this->year;
107
    }
108
109
    /**
110
     * @return int
111
     */
112 19
    public function getMonth(): int
113
    {
114 19
        return $this->month;
115
    }
116
117
    /**
118
     * @return int
119
     */
120 16
    public function getDay(): int
121
    {
122 16
        return $this->day;
123
    }
124
125
    /**
126
     * @return string
127
     */
128 1
    public function renderNumeric(): string
129
    {
130 1
        $ret  = $this->year;
131 1
        $ret .= $this->month > 0 && $this->month < 13 ? "-" . sprintf("%02s", $this->month) : "";
132 1
        $ret .= $this->day > 0 && $this->day < 32 ? "-" . sprintf("%02s", $this->day) : "";
133 1
        return $ret;
134
    }
135
}
136