1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Jean Silva <[email protected]> |
4
|
|
|
* @license MIT |
5
|
|
|
*/ |
6
|
|
|
namespace Jeancsil\FlightSpy\Api\DataTransfer; |
7
|
|
|
|
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
|
10
|
|
|
class Period |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
private $durationInDays; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Datetime |
19
|
|
|
*/ |
20
|
|
|
private $dateFrom; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Datetime |
24
|
|
|
*/ |
25
|
|
|
private $dateTo; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ArrayCollection |
29
|
|
|
*/ |
30
|
|
|
private $combinations; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $durationInDays |
34
|
|
|
* @param \Datetime $dateFrom |
35
|
|
|
* @param \Datetime $dateTo |
36
|
|
|
*/ |
37
|
|
|
public function __construct($durationInDays, \Datetime $dateFrom, \Datetime $dateTo) |
38
|
|
|
{ |
39
|
|
|
$this->durationInDays = $durationInDays; |
40
|
|
|
$this->dateFrom = $dateFrom; |
|
|
|
|
41
|
|
|
$this->dateTo = $dateTo; |
|
|
|
|
42
|
2 |
|
$this->combinations = new ArrayCollection(); |
|
|
|
|
43
|
|
|
} |
44
|
2 |
|
|
45
|
2 |
|
/** |
46
|
|
|
* @return ArrayCollection |
|
|
|
|
47
|
2 |
|
*/ |
48
|
2 |
|
public function generateDateCombinations() |
49
|
|
|
{ |
50
|
2 |
|
$possibleDays = $this->dateFrom->diff($this->dateTo)->days; |
51
|
2 |
|
|
52
|
2 |
|
$initialDate = clone $this->dateFrom; |
53
|
2 |
|
$endDate = clone $this->dateFrom->add(new \DateInterval(sprintf('P%dD', $this->durationInDays))); |
|
|
|
|
54
|
|
|
|
55
|
|
|
for ($i = 0; $i < $possibleDays; $i++) { |
56
|
2 |
|
$this->combinations->add([ |
57
|
2 |
|
'outboundDate' => $initialDate->format('Y-m-d'), |
58
|
|
|
'inboundDate' => $endDate->format('Y-m-d') |
59
|
|
|
]); |
60
|
2 |
|
|
61
|
2 |
|
if ($endDate->diff($this->dateTo)->days == 0) { |
62
|
|
|
return $this->combinations; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$initialDate->add($this->getOneDayMore()); |
66
|
|
|
$endDate->add($this->getOneDayMore()); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
|
|
public function getDurationInDays() |
74
|
|
|
{ |
75
|
|
|
return $this->durationInDays; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return \Datetime |
80
|
|
|
*/ |
81
|
|
|
public function getDateFrom() |
82
|
|
|
{ |
83
|
|
|
return $this->dateFrom; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return \Datetime |
88
|
|
|
*/ |
89
|
|
|
public function getDateTo() |
90
|
|
|
{ |
91
|
|
|
return $this->dateTo; |
92
|
2 |
|
} |
93
|
2 |
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function __toString() |
98
|
|
|
{ |
99
|
|
|
$string = sprintf( |
100
|
|
|
"Duration: %d days\nBetween %s and %s (inclusive)", |
101
|
|
|
$this->durationInDays, |
102
|
|
|
$this->dateFrom->format('d/m/Y'), |
103
|
|
|
$this->dateTo->format('d/m/Y') |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
if (!$this->combinations->isEmpty()) { |
107
|
|
|
$string .= sprintf( |
108
|
|
|
"\nResulted in these combinations:\n %s", |
109
|
|
|
var_export($this->combinations->toArray(), true) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $string; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return \DateInterval |
118
|
|
|
*/ |
119
|
|
|
private function getOneDayMore() { |
120
|
|
|
return new \DateInterval('P1D'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.