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
|
|
|
* @param int $durationInDays |
29
|
|
|
* @param \Datetime $dateFrom |
30
|
|
|
* @param \Datetime $dateTo |
31
|
|
|
*/ |
32
|
|
|
public function __construct($durationInDays, \Datetime $dateFrom, \Datetime $dateTo) |
33
|
|
|
{ |
34
|
|
|
$this->durationInDays = $durationInDays; |
35
|
|
|
$this->dateFrom = $dateFrom; |
|
|
|
|
36
|
|
|
$this->dateTo = $dateTo; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return ArrayCollection |
|
|
|
|
41
|
|
|
*/ |
42
|
2 |
|
public function generateDateCombinations() |
43
|
|
|
{ |
44
|
2 |
|
$combinations = new ArrayCollection(); |
45
|
2 |
|
$possibleDays = $this->dateFrom->diff($this->dateTo)->days; |
46
|
|
|
|
47
|
2 |
|
$initialDate = clone $this->dateFrom; |
48
|
2 |
|
$endDate = clone $this->dateFrom->add(new \DateInterval(sprintf('P%dD', $this->durationInDays))); |
|
|
|
|
49
|
|
|
|
50
|
2 |
|
for ($i = 0; $i < $possibleDays; $i++) { |
51
|
2 |
|
$combinations->add([ |
52
|
2 |
|
'from' => clone $initialDate, |
53
|
2 |
|
'to' => clone $endDate |
54
|
|
|
]); |
55
|
|
|
|
56
|
2 |
|
if ($endDate->diff($this->dateTo)->days == 0) { |
57
|
2 |
|
return $combinations; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$initialDate->add($this->getOneDayMore()); |
61
|
2 |
|
$endDate->add($this->getOneDayMore()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public function getDurationInDays() |
69
|
|
|
{ |
70
|
|
|
return $this->durationInDays; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \Datetime |
75
|
|
|
*/ |
76
|
|
|
public function getDateFrom() |
77
|
|
|
{ |
78
|
|
|
return $this->dateFrom; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \Datetime |
83
|
|
|
*/ |
84
|
|
|
public function getDateTo() |
85
|
|
|
{ |
86
|
|
|
return $this->dateTo; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return \DateInterval |
91
|
|
|
*/ |
92
|
2 |
|
private function getOneDayMore() { |
93
|
2 |
|
return new \DateInterval('P1D'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.