1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Odiseo\SyliusReportPlugin\DataFetcher; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstract class to provide time periods logic. |
7
|
|
|
* |
8
|
|
|
* @author Łukasz Chruściel <[email protected]> |
9
|
|
|
* @author Diego D'amico <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
abstract class TimePeriodDataFetcher extends BaseDataFetcher |
12
|
|
|
{ |
13
|
|
|
const PERIOD_DAY = 'day'; |
14
|
|
|
const PERIOD_MONTH = 'month'; |
15
|
|
|
const PERIOD_YEAR = 'year'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public static function getPeriodChoices() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
'Daily' => self::PERIOD_DAY, |
24
|
|
|
'Monthly' => self::PERIOD_MONTH, |
25
|
|
|
'Yearly' => self::PERIOD_YEAR, |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function fetch(array $configuration): Data |
33
|
|
|
{ |
34
|
|
|
$data = new Data(); |
35
|
|
|
|
36
|
|
|
/** @var \DateTime $endDate */ |
37
|
|
|
$endDate = $configuration['timePeriod']['end']; |
38
|
|
|
|
39
|
|
|
//There is added 23 hours 59 minutes 59 seconds to the end date to provide records for whole end date |
40
|
|
|
$configuration['timePeriod']['end'] = $endDate->add(new \DateInterval('PT23H59M59S')); |
41
|
|
|
|
42
|
|
|
switch ($configuration['timePeriod']['period']) { |
43
|
|
|
case self::PERIOD_DAY: |
44
|
|
|
$this->setExtraConfiguration($configuration, 'P1D', '%a', 'Y-m-d', ['date']); |
45
|
|
|
break; |
46
|
|
|
case self::PERIOD_MONTH: |
47
|
|
|
$this->setExtraConfiguration($configuration, 'P1M', '%m', 'F Y', ['month', 'year']); |
48
|
|
|
break; |
49
|
|
|
case self::PERIOD_YEAR: |
50
|
|
|
$this->setExtraConfiguration($configuration, 'P1Y', '%y', 'Y', ['year']); |
51
|
|
|
break; |
52
|
|
|
default: |
53
|
|
|
throw new \InvalidArgumentException('Wrong data fetcher period'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$rawData = $this->getData($configuration); |
57
|
|
|
|
58
|
|
|
if (empty($rawData)) { |
59
|
|
|
return $data; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$labelsAux = array_keys($rawData[0]); |
63
|
|
|
$labels = []; |
64
|
|
|
foreach ($labelsAux as $label) |
65
|
|
|
{ |
66
|
|
|
if(!in_array($label, ['MonthDate', 'YearDate', 'DateDate'])) |
67
|
|
|
{ |
68
|
|
|
$labels[] = $label; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
$data->setLabels($labels); |
72
|
|
|
|
73
|
|
|
$fetched = []; |
74
|
|
|
|
75
|
|
|
if ($configuration['empty_records']) { |
76
|
|
|
$fetched = $this->fillEmptyRecords($fetched, $configuration); |
77
|
|
|
} |
78
|
|
|
foreach ($rawData as $row) { |
79
|
|
|
$date = new \DateTime($row[$labels[0]]); |
80
|
|
|
$fetched[$date->format($configuration['timePeriod']['presentationFormat'])] = $row[$labels[1]]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$data->setData($fetched); |
84
|
|
|
|
85
|
|
|
$labels = []; |
86
|
|
|
foreach ($labelsAux as $label) |
87
|
|
|
{ |
88
|
|
|
if(!in_array($label, ['MonthDate', 'YearDate', 'DateDate'])) |
89
|
|
|
{ |
90
|
|
|
$labels[] = preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $label); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
$data->setLabels($labels); |
94
|
|
|
|
95
|
|
|
return $data; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $configuration |
100
|
|
|
* @param string $interval |
101
|
|
|
* @param string $periodFormat |
102
|
|
|
* @param string $presentationFormat |
103
|
|
|
* @param array $groupBy |
104
|
|
|
*/ |
105
|
|
|
protected function setExtraConfiguration( |
106
|
|
|
array &$configuration, |
107
|
|
|
$interval, |
108
|
|
|
$periodFormat, |
109
|
|
|
$presentationFormat, |
110
|
|
|
array $groupBy |
111
|
|
|
) { |
112
|
|
|
$configuration['timePeriod']['interval'] = $interval; |
113
|
|
|
$configuration['timePeriod']['periodFormat'] = $periodFormat; |
114
|
|
|
$configuration['timePeriod']['presentationFormat'] = $presentationFormat; |
115
|
|
|
$configuration['groupBy'] = $groupBy; |
116
|
|
|
$configuration['empty_records'] = false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $fetched |
121
|
|
|
* @param array $configuration |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
private function fillEmptyRecords(array $fetched, array $configuration) |
126
|
|
|
{ |
127
|
|
|
/** @var \DateTime $startDate */ |
128
|
|
|
$startDate = $configuration['start']; |
129
|
|
|
/** @var \DateTime $startDate */ |
130
|
|
|
$endDate = $configuration['end']; |
131
|
|
|
|
132
|
|
|
try { |
133
|
|
|
$dateInterval = new \DateInterval($configuration['interval']); |
134
|
|
|
} catch (\Exception $e) { |
135
|
|
|
return $fetched; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$numberOfPeriods = $startDate->diff($endDate); |
139
|
|
|
$formattedNumberOfPeriods = $numberOfPeriods->format($configuration['periodFormat']); |
140
|
|
|
|
141
|
|
|
for ($i = 0; $i <= $formattedNumberOfPeriods; ++$i) { |
142
|
|
|
$fetched[$startDate->format($configuration['presentationFormat'])] = 0; |
143
|
|
|
$startDate = $startDate->add($dateInterval); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $fetched; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $datas |
151
|
|
|
* @param array $configuration |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function getMediaResults(array $datas = [], array $configuration = []) |
155
|
|
|
{ |
156
|
|
|
if (empty($datas)) { |
157
|
|
|
return []; |
158
|
|
|
} |
159
|
|
|
$labels = array_keys($datas[0]); |
160
|
|
|
$datesMedia = []; |
161
|
|
|
foreach($datas as $data) |
162
|
|
|
{ |
163
|
|
|
$date = new \DateTime($data[$labels[0]]); |
164
|
|
|
$dateFormated = $date->format($configuration['timePeriod']['presentationFormat']); |
165
|
|
|
$currentDateMedia = isset($datesMedia[$dateFormated])?$datesMedia[$dateFormated]:array('quantity' => 0, 'media' => 0); |
166
|
|
|
$currentDateMedia['quantity'] = $currentDateMedia['quantity']+1; |
167
|
|
|
$currentDateMedia['media'] = $currentDateMedia['media']+$data[$labels[1]]; |
168
|
|
|
$datesMedia[$dateFormated] = $currentDateMedia; |
169
|
|
|
} |
170
|
|
|
$fetched = []; |
171
|
|
|
foreach($datesMedia as $date => $dateMedia) |
172
|
|
|
{ |
173
|
|
|
$fetched[] = [ |
174
|
|
|
$labels[0] => $date, |
175
|
|
|
$labels[1] => round($dateMedia['media']/$dateMedia['quantity'], 1) |
176
|
|
|
]; |
177
|
|
|
} |
178
|
|
|
return $fetched; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|