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
|
4 |
|
public function __construct($durationInDays, \Datetime $dateFrom, \Datetime $dateTo) |
38
|
|
|
{ |
39
|
4 |
|
if (!is_int($durationInDays) || $durationInDays <= 0) { |
40
|
3 |
|
throw new \InvalidArgumentException('The duration should be bigger than 0'); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
$this->durationInDays = $durationInDays; |
44
|
1 |
|
$this->dateFrom = $dateFrom; |
|
|
|
|
45
|
1 |
|
$this->dateTo = $dateTo; |
|
|
|
|
46
|
1 |
|
$this->combinations = new ArrayCollection(); |
|
|
|
|
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return ArrayCollection |
|
|
|
|
51
|
|
|
*/ |
52
|
3 |
|
public function generateDateCombinations() |
53
|
|
|
{ |
54
|
3 |
|
$possibleDays = $this->dateFrom->diff($this->dateTo)->days; |
55
|
|
|
|
56
|
3 |
|
if ($possibleDays < $this->durationInDays) { |
57
|
1 |
|
throw new \InvalidArgumentException('There are no combinations possible'); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$initialDate = clone $this->dateFrom; |
61
|
2 |
|
$endDate = clone $this->dateFrom->add(new \DateInterval(sprintf('P%dD', $this->durationInDays))); |
|
|
|
|
62
|
|
|
|
63
|
2 |
|
for ($i = 0; $i < $possibleDays; $i++) { |
64
|
2 |
|
$this->combinations->add([ |
65
|
2 |
|
'outboundDate' => $initialDate->format('Y-m-d'), |
66
|
2 |
|
'inboundDate' => $endDate->format('Y-m-d') |
67
|
|
|
]); |
68
|
|
|
|
69
|
2 |
|
if ($endDate->diff($this->dateTo)->days == 0) { |
70
|
2 |
|
return $this->combinations; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$initialDate->add($this->getOneDayMore()); |
74
|
2 |
|
$endDate->add($this->getOneDayMore()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function getDurationInDays() |
82
|
|
|
{ |
83
|
|
|
return $this->durationInDays; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return \Datetime |
88
|
|
|
*/ |
89
|
|
|
public function getDateFrom() |
90
|
|
|
{ |
91
|
|
|
return $this->dateFrom; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \Datetime |
96
|
|
|
*/ |
97
|
|
|
public function getDateTo() |
98
|
|
|
{ |
99
|
|
|
return $this->dateTo; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function __toString() |
106
|
|
|
{ |
107
|
|
|
$string = sprintf( |
108
|
|
|
"Duration: %d days\nBetween %s and %s (inclusive)", |
109
|
|
|
$this->durationInDays, |
110
|
|
|
$this->dateFrom->format('d/m/Y'), |
111
|
|
|
$this->dateTo->format('d/m/Y') |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
if (!$this->combinations->isEmpty()) { |
115
|
|
|
$string .= sprintf( |
116
|
|
|
"\nResulted in these combinations:\n %s", |
117
|
|
|
var_export($this->combinations->toArray(), true) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $string; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return \DateInterval |
126
|
|
|
*/ |
127
|
2 |
|
private function getOneDayMore() |
128
|
|
|
{ |
129
|
2 |
|
return new \DateInterval('P1D'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.