|
1
|
|
|
<?php |
|
2
|
|
|
namespace GetSky\AIRAC; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class Producer |
|
6
|
|
|
* @deprecated Use AiracProducer. This class will be removed in version 1.2.0 |
|
7
|
|
|
* @package GetSky\AIRAC |
|
8
|
|
|
*/ |
|
9
|
|
|
class Producer |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \DateTime |
|
13
|
|
|
*/ |
|
14
|
|
|
private $bearing; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param \DateTime|null $bearing |
|
18
|
|
|
*/ |
|
19
|
57 |
|
public function __construct(\DateTime $bearing = null) |
|
20
|
|
|
{ |
|
21
|
57 |
|
$this->bearing = $bearing ? $bearing : new \DateTime('2016-01-07'); |
|
22
|
57 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return \DateTime |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function getBearingDate(): \DateTime |
|
28
|
|
|
{ |
|
29
|
2 |
|
return $this->bearing; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get next AIRAC date. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \DateTime $date |
|
36
|
|
|
* @return Airac |
|
37
|
|
|
*/ |
|
38
|
6 |
|
public function next(\DateTime $date): Airac |
|
39
|
|
|
{ |
|
40
|
6 |
|
return $this->circle($date, 1); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get next AIRAC date. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $number |
|
47
|
|
|
* @return Airac |
|
48
|
|
|
* @throws AiracNumberValidationException |
|
49
|
|
|
*/ |
|
50
|
13 |
|
public function nextByNumber(string $number): Airac |
|
51
|
|
|
{ |
|
52
|
13 |
|
return $this->circleByNumber($number, 1); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get next AIRAC date. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Airac $airac |
|
59
|
|
|
* @return Airac |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function nextByAirac(Airac $airac): Airac |
|
62
|
|
|
{ |
|
63
|
6 |
|
return $this->circle($airac->getDateStart(), 1); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get current AIRAC date. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \DateTime $date |
|
70
|
|
|
* @return Airac |
|
71
|
|
|
*/ |
|
72
|
6 |
|
public function now(\DateTime $date): Airac |
|
73
|
|
|
{ |
|
74
|
6 |
|
return $this->circle($date, 0); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get current AIRAC date. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $number |
|
81
|
|
|
* @return Airac |
|
82
|
|
|
* @throws AiracNumberValidationException |
|
83
|
|
|
*/ |
|
84
|
6 |
|
public function nowByNumber(string $number): Airac |
|
85
|
|
|
{ |
|
86
|
6 |
|
return $this->circleByNumber($number, 0); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get last AIRAC date. |
|
91
|
|
|
* |
|
92
|
|
|
* @param \DateTime $date |
|
93
|
|
|
* @return Airac |
|
94
|
|
|
*/ |
|
95
|
6 |
|
public function last(\DateTime $date): Airac |
|
96
|
|
|
{ |
|
97
|
6 |
|
return $this->circle($date, -1); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get last AIRAC date. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $number |
|
104
|
|
|
* @return Airac |
|
105
|
|
|
* @throws AiracNumberValidationException |
|
106
|
|
|
*/ |
|
107
|
6 |
|
public function lastByNumber(string $number): Airac |
|
108
|
|
|
{ |
|
109
|
6 |
|
return $this->circleByNumber($number, -1); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get last AIRAC date. |
|
114
|
|
|
* |
|
115
|
|
|
* @param Airac $airac |
|
116
|
|
|
* @return Airac |
|
117
|
|
|
*/ |
|
118
|
6 |
|
public function lastByAirac(Airac $airac): Airac |
|
119
|
|
|
{ |
|
120
|
6 |
|
return $this->circle($airac->getDateStart(), -1); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Generator AIRAC dates. |
|
125
|
|
|
* |
|
126
|
|
|
* @param \DateTime $date |
|
127
|
|
|
* @param $step |
|
128
|
|
|
* @return Airac |
|
129
|
|
|
*/ |
|
130
|
48 |
|
private function circle(\DateTime $date, int $step): Airac |
|
131
|
|
|
{ |
|
132
|
48 |
|
$positive = ($date >= $this->bearing) ? 1 : -1; |
|
133
|
48 |
|
$days = $date->diff($this->bearing)->days; |
|
134
|
48 |
|
$countCircle = $positive * floor($days / 28) + $step - (($positive < 0 && $days % 28 != 0) ? 1 : 0); |
|
135
|
48 |
|
$dateStart = clone $this->bearing; |
|
136
|
48 |
|
$dateStart->modify(($countCircle * 28) . ' day'); |
|
137
|
48 |
|
$dateEnd = clone $dateStart; |
|
138
|
48 |
|
$dateEnd->modify('28 day'); |
|
139
|
|
|
|
|
140
|
48 |
|
return new Airac($dateStart, $dateEnd, $this->calcNumber($dateStart)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Generator AIRAC dates. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $number |
|
147
|
|
|
* @param int $step |
|
148
|
|
|
* @return Airac |
|
149
|
|
|
* @throws AiracNumberValidationException |
|
150
|
|
|
*/ |
|
151
|
25 |
|
private function circleByNumber(string $number, int $step): Airac |
|
152
|
|
|
{ |
|
153
|
25 |
|
$date = \DateTime::createFromFormat('!y', substr($number, 0, 2)); |
|
154
|
25 |
|
if (preg_match("/[0-9]{4}/", $number) === 0 || $date === false) { |
|
155
|
7 |
|
throw new AiracNumberValidationException("Number '$number' isn't correct AIRAC."); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
18 |
|
return $this->circle($date, $step + substr($number, 2, 2)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
*Calculated number of AIRAC. |
|
163
|
|
|
* |
|
164
|
|
|
* @param \DateTime $date |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
48 |
|
private function calcNumber(\DateTime $date): string |
|
168
|
|
|
{ |
|
169
|
48 |
|
$year = \DateTime::createFromFormat('!Y', $date->format('Y')); |
|
170
|
48 |
|
$number = 1 + floor($year->diff($date)->days / 28); |
|
171
|
|
|
|
|
172
|
48 |
|
return $year->format('y') . sprintf("%02d", $number); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|