DateTime::setYear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2017 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Rendering\Date;
11
12
use DateTimeZone;
13
use Exception;
14
use Seboettg\CiteProc\Exception\InvalidDateTimeException;
15
16
class DateTime extends \DateTime
17
{
18
    /**
19
     * @var int
20
     */
21
    private $year = 0;
22
23
    /**
24
     * @var int
25
     */
26
    private $month = 0;
27
28
    /**
29
     * @var int
30
     */
31
    private $day = 0;
32
33
    /**
34
     * DateTime constructor.
35
     * @param string $year
36
     * @param string $month
37
     * @param string $day
38
     * @throws Exception
39
     */
40
    public function __construct($year, $month, $day)
41
    {
42
        try {
43
            parent::__construct("$year-$month-$day", new DateTimeZone("Europe/Berlin"));
44
        } catch (Exception $e) {
45
            throw new InvalidDateTimeException("Could not create valid date with year=$year, month=$month, day=$day.");
46
        }
47
48
        $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

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