1
|
|
|
<?php |
2
|
|
|
namespace DavisPeixoto\ReportDates; |
3
|
|
|
|
4
|
|
|
use DateInterval; |
5
|
|
|
use DateTime; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class Dates |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var DatesConfig $config |
12
|
|
|
*/ |
13
|
|
|
private $config; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Dates constructor. |
17
|
|
|
* |
18
|
|
|
* @param DatesConfig $config |
19
|
|
|
*/ |
20
|
|
|
public function __construct(DatesConfig $config) |
21
|
|
|
{ |
22
|
|
|
$this->config = $config; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param DateTime $startDate |
27
|
|
|
* @param DateTime $endDate |
28
|
|
|
* @param bool $full |
29
|
|
|
* @param bool $inclusive |
30
|
|
|
* |
31
|
|
|
* @return WeekInterval[] |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
|
|
public function get_weeks_break(DateTime $startDate, DateTime $endDate, $full = false, $inclusive = false) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$output = []; |
37
|
|
|
$innerArray = []; |
38
|
|
|
$isOpen = false; |
39
|
|
|
$i = 0; |
40
|
|
|
|
41
|
|
|
$interval = new DateInterval('P1D'); |
42
|
|
|
$daylightSavingsTime = new DateInterval('PT1H'); |
43
|
|
|
|
44
|
|
|
if ($inclusive) { |
45
|
|
|
$oldStart = clone $startDate; |
46
|
|
|
$oldEnd = clone $endDate; |
47
|
|
|
|
48
|
|
|
$aux = new DateTime(); |
49
|
|
|
|
50
|
|
|
if ($oldStart->format('w') !== '0') { |
51
|
|
|
$aux->setISODate($oldStart->format('o'), $oldStart->format('W'), 0); |
52
|
|
|
$startDate = clone $aux; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($oldEnd->format('w') !== '6') { |
56
|
|
|
$aux->setISODate($oldEnd->format('o'), $oldEnd->format('W'), 6); |
57
|
|
|
$endDate = clone $aux; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$start = clone $startDate; |
62
|
|
|
$end = clone $endDate; |
63
|
|
|
|
64
|
|
|
while ($start <= $end) { |
65
|
|
|
$str = (int)$start->format('w'); |
66
|
|
|
|
67
|
|
|
if (!$full) { |
68
|
|
|
if (in_array($str, range(1, 5)) && !$isOpen) { |
69
|
|
|
$innerArray[$i]['start'] = clone $start; |
70
|
|
|
$isOpen = true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!in_array($str, range(1, 5)) && $isOpen) { |
74
|
|
|
$x = clone $start; |
75
|
|
|
$x->sub($interval); |
76
|
|
|
$innerArray[$i]['end'] = clone $x; |
77
|
|
|
$isOpen = false; |
78
|
|
|
$i++; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
if ($start == $end && $isOpen) { |
|
|
|
|
82
|
|
|
$innerArray[$i]['end'] = clone $start; |
83
|
|
|
$isOpen = false; |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
View Code Duplication |
if (($str !== 6) && !$isOpen) { |
|
|
|
|
87
|
|
|
$innerArray[$i]['start'] = clone $start; |
88
|
|
|
$isOpen = true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (($str === 6) && $isOpen) { |
92
|
|
|
$x = clone $start; |
93
|
|
|
$innerArray[$i]['end'] = clone $x; |
94
|
|
|
$isOpen = false; |
95
|
|
|
$i++; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
if (($start == $end) && $isOpen) { |
|
|
|
|
100
|
|
|
$innerArray[$i]['end'] = clone $start; |
101
|
|
|
$isOpen = false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$start->add($interval); |
105
|
|
|
|
106
|
|
|
// fix for day light savings time |
107
|
|
|
if ($start->format('h') === "01") { |
108
|
|
|
$start->sub($daylightSavingsTime); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($start->format('h') === "23") { |
112
|
|
|
$start->add($daylightSavingsTime); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($innerArray as $key => $value) { |
117
|
|
|
$weekInterval = new WeekInterval($value['start'], $value['end']); |
118
|
|
|
$output[$key] = $weekInterval; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $output; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function unwrap_week($yearweek, $yearmonth) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$output = array(); |
127
|
|
|
|
128
|
|
|
$x = str_split($yearmonth, 4); |
129
|
|
|
$x[] = "01"; |
130
|
|
|
$obj_start = DateTime::createFromFormat('Y-m-d', implode('-', $x)); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$start_date = date('Y-m-01', $obj_start->getTimeStamp()); |
|
|
|
|
133
|
|
|
$end_date = date('Y-m-t', $obj_start->getTimeStamp()); |
|
|
|
|
134
|
|
|
|
135
|
|
|
$weeks = $this->get_weeks_break($start_date, $end_date); |
|
|
|
|
136
|
|
|
$weeks_full = $this->get_weeks_break($start_date, $end_date, true); |
|
|
|
|
137
|
|
|
$today = new DateTime(); |
138
|
|
|
|
139
|
|
|
foreach ($weeks as $key => $value) { |
140
|
|
|
if ($value['yearweek'] == $yearweek) { |
141
|
|
|
$output['start'] = $value['start_date']; |
142
|
|
|
$output['end'] = $value['end_date']; |
143
|
|
|
|
144
|
|
|
$endDateObj = DateTime::createFromFormat('Y-m-d', $output['end']); |
145
|
|
|
|
146
|
|
|
if ($today > $endDateObj) { |
147
|
|
|
foreach ($weeks_full as $innerWeek) { |
|
|
|
|
148
|
|
|
if ($value['yearweek'] == $innerWeek['yearweek']) { |
149
|
|
|
$output['start'] = $innerWeek['start_date']; |
150
|
|
|
$output['end'] = $innerWeek['end_date']; |
151
|
|
|
break; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
break; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $output; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.