|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Converter\UnixTimeConverter; |
|
4
|
|
|
|
|
5
|
|
|
use Popy\Calendar\Converter\Conversion; |
|
6
|
|
|
use Popy\Calendar\Converter\UnixTimeConverterInterface; |
|
7
|
|
|
use Popy\Calendar\ValueObject\DateSolarRepresentationInterface; |
|
8
|
|
|
use Popy\Calendar\ValueObject\DateFragmentedRepresentationInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Simple weeks. |
|
12
|
|
|
*/ |
|
13
|
|
|
class SimpleWeeks implements UnixTimeConverterInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Week length. |
|
17
|
|
|
* |
|
18
|
|
|
* @var integer |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $length; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param integer $length Week length. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($length) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->length = $length; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritDoc |
|
34
|
|
|
*/ |
|
35
|
|
|
public function fromUnixTime(Conversion $conversion) |
|
36
|
|
|
{ |
|
37
|
|
|
$input = $conversion->getTo(); |
|
38
|
|
|
|
|
39
|
|
|
if ( |
|
40
|
|
|
!$input instanceof DateFragmentedRepresentationInterface |
|
41
|
|
|
|| !$input instanceof DateSolarRepresentationInterface |
|
42
|
|
|
) { |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$dateParts = $input->getDateParts()->withTransversals([ |
|
47
|
|
|
$input->getYear(), |
|
48
|
|
|
intval($input->getDayIndex() / $this->length), |
|
49
|
|
|
$input->getDayIndex() % $this->length |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
$conversion->setTo($input->withDateParts($dateParts)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritDoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function toUnixTime(Conversion $conversion) |
|
59
|
|
|
{ |
|
60
|
|
|
$input = $conversion->getTo(); |
|
61
|
|
|
|
|
62
|
|
|
if ( |
|
63
|
|
|
!$input instanceof DateFragmentedRepresentationInterface |
|
64
|
|
|
|| !$input instanceof DateSolarRepresentationInterface |
|
65
|
|
|
) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$parts = $input->getDateParts(); |
|
70
|
|
|
|
|
71
|
|
|
if (null === $input->getYear()) { |
|
72
|
|
|
$input = $input->withYear( |
|
73
|
|
|
$parts->getTransversal(0), |
|
74
|
|
|
$input->isLeapYear() |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ( |
|
79
|
|
|
null === $input->getDayIndex() |
|
80
|
|
|
&& null !== $weekIndex = $parts->getTransversal(1) |
|
81
|
|
|
) { |
|
82
|
|
|
$dayOfWeek = (int)$parts->getTransversal(2); |
|
83
|
|
|
|
|
84
|
|
|
$input = $input->withDayIndex( |
|
85
|
|
|
$dayOfWeek + $weekIndex * $this->length, |
|
86
|
|
|
$input->getEraDayIndex() |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$conversion->setTo($input); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|