|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Converter\LeapYearCalculator; |
|
4
|
|
|
|
|
5
|
|
|
use Popy\Calendar\Converter\SimpleLeapYearCalculatorInterface; |
|
6
|
|
|
use Popy\Calendar\Converter\CompleteLeapYearCalculatorInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Abstract calculator implementation, implementing every method but isLeapYear, |
|
10
|
|
|
* every method relying on it to produce their results. |
|
11
|
|
|
*/ |
|
12
|
|
|
class AgnosticCompleteCalculator implements CompleteLeapYearCalculatorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Simple leap year calculator. |
|
16
|
|
|
* |
|
17
|
|
|
* @var SimpleLeapYearCalculatorInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $calculator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Regular year duration, in days. |
|
23
|
|
|
* |
|
24
|
|
|
* @var integer |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $yearLengthInDays = 365; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* First year number. |
|
30
|
|
|
* |
|
31
|
|
|
* @var integer |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $firstYear = 1; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param SimpleLeapYearCalculatorInterface $calculator Decorated calculator |
|
39
|
|
|
* @param integer|null $yearLengthInDays Year length. |
|
40
|
|
|
* @param integer|null $firstYear First year number. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(SimpleLeapYearCalculatorInterface $calculator, $yearLengthInDays = null, $firstYear = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->calculator = $calculator; |
|
45
|
|
|
|
|
46
|
|
|
if (null !== $yearLengthInDays) { |
|
47
|
|
|
$this->yearLengthInDays = $yearLengthInDays; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (null !== $firstYear) { |
|
51
|
|
|
$this->firstYear = $firstYear; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritDoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function isLeapYear($year) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->calculator->isLeapYear($year); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getYearLength($year) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->yearLengthInDays + (int)$this->isLeapYear($year); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getYearEraDayIndex($year) |
|
75
|
|
|
{ |
|
76
|
|
|
$index = 0; |
|
77
|
|
|
|
|
78
|
|
|
$sign = $year < $this->firstYear ? -1 : 1; |
|
79
|
|
|
|
|
80
|
|
|
for ($i=min($year, $this->firstYear); $i < max($year, $this->firstYear); $i++) { |
|
81
|
|
|
$index += $sign * $this->getYearLength($i); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $index; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritDoc |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getYearAndDayIndexFromErayDayIndex($eraDayIndex) |
|
91
|
|
|
{ |
|
92
|
|
|
$dayIndex = $eraDayIndex; |
|
93
|
|
|
$year = $this->firstYear; |
|
94
|
|
|
|
|
95
|
|
|
// Handling negative years |
|
96
|
|
|
while ($dayIndex < 0) { |
|
97
|
|
|
$dayIndex += $this->getYearLength(--$year); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Positive years |
|
101
|
|
|
while ($dayIndex >= $dayCount = $this->getYearLength($year)) { |
|
102
|
|
|
$dayIndex -= $dayCount; |
|
103
|
|
|
$year++; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return [$year, $dayIndex]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|