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