1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TournamentGenerator\Export\Hierarchy; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use TournamentGenerator\Category; |
9
|
|
|
use TournamentGenerator\Export\ExporterInterface; |
10
|
|
|
use TournamentGenerator\Export\ExporterBase; |
11
|
|
|
use TournamentGenerator\Group; |
12
|
|
|
use TournamentGenerator\Interfaces\WithCategories; |
13
|
|
|
use TournamentGenerator\Interfaces\WithGames; |
14
|
|
|
use TournamentGenerator\Interfaces\WithGroups; |
15
|
|
|
use TournamentGenerator\Interfaces\WithId; |
16
|
|
|
use TournamentGenerator\Interfaces\WithRounds; |
17
|
|
|
use TournamentGenerator\Interfaces\WithTeams; |
18
|
|
|
use TournamentGenerator\Preset\Preset; |
19
|
|
|
use TournamentGenerator\Progression; |
20
|
|
|
use TournamentGenerator\Round; |
21
|
|
|
use TournamentGenerator\TeamFilter; |
22
|
|
|
use TournamentGenerator\Tournament; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class SetupExporter |
26
|
|
|
* |
27
|
|
|
* @package TournamentGenerator\Export |
28
|
|
|
* @author Tomáš Vojík <[email protected]> |
29
|
|
|
* @since 0.5 |
30
|
|
|
*/ |
31
|
|
|
class SetupExporter extends ExporterBase |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
2 |
|
public static function export(WithId $object) : array { |
38
|
2 |
|
return self::start($object)->get(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
6 |
|
public static function start(WithId $object) : ExporterInterface { |
45
|
6 |
|
return new self($object); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Finish the export query -> get the result |
50
|
|
|
* |
51
|
|
|
* @return array The query result |
52
|
|
|
* @throws Exception |
53
|
|
|
*/ |
54
|
6 |
|
public function get() : array { |
55
|
6 |
|
$data = $this->getBasic(); |
56
|
6 |
|
$this->applyModifiers($data); |
57
|
6 |
|
return $data; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
* @throws Exception |
63
|
|
|
*/ |
64
|
6 |
|
public function getBasic() : array { |
65
|
6 |
|
$data = []; |
66
|
6 |
|
$this->getTournamentData($data); |
67
|
6 |
|
$this->getCategoriesData($data); |
68
|
6 |
|
$this->getRoundsData($data); |
69
|
6 |
|
$this->getGroupsData($data); |
70
|
6 |
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get all setup information from a Tournament class |
75
|
|
|
* |
76
|
|
|
* @param array $data |
77
|
|
|
* |
78
|
|
|
* @throws Exception |
79
|
|
|
*/ |
80
|
6 |
|
protected function getTournamentData(array &$data) : void { |
81
|
6 |
|
if (!$this->object instanceof Tournament) { |
82
|
2 |
|
return; |
83
|
|
|
} |
84
|
5 |
|
$data['tournament'] = (object) [ |
85
|
5 |
|
'type' => $this->object instanceof Preset ? get_class($this->object) : 'general', |
86
|
5 |
|
'name' => $this->object->getName(), |
|
|
|
|
87
|
5 |
|
'skip' => $this->object->getSkip(), |
|
|
|
|
88
|
|
|
'timing' => (object) [ |
89
|
5 |
|
'play' => $this->object->getPlay(), |
|
|
|
|
90
|
5 |
|
'gameWait' => $this->object->getGameWait(), |
|
|
|
|
91
|
5 |
|
'categoryWait' => $this->object->getCategoryWait(), |
|
|
|
|
92
|
5 |
|
'roundWait' => $this->object->getRoundWait(), |
|
|
|
|
93
|
5 |
|
'expectedTime' => $this->object->getTournamentTime(), |
|
|
|
|
94
|
|
|
], |
95
|
5 |
|
'categories' => $this->object instanceof WithCategories ? $this->object->queryCategories()->ids()->get() : [], |
|
|
|
|
96
|
5 |
|
'rounds' => $this->object instanceof WithRounds ? $this->object->queryRounds()->ids()->get() : [], |
|
|
|
|
97
|
5 |
|
'groups' => $this->object instanceof WithGroups ? $this->object->queryGroups()->ids()->get() : [], |
|
|
|
|
98
|
5 |
|
'teams' => $this->object instanceof WithTeams ? $this->object->getTeamContainer()->ids()->unique()->get() : [], |
|
|
|
|
99
|
5 |
|
'games' => $this->object instanceof WithGames ? $this->object->getGameContainer()->ids()->get() : [], |
|
|
|
|
100
|
|
|
]; |
101
|
5 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get all setup information for categories |
105
|
|
|
* |
106
|
|
|
* @param array $data |
107
|
|
|
* |
108
|
|
|
* @throws Exception |
109
|
|
|
*/ |
110
|
6 |
|
protected function getCategoriesData(array &$data) : void { |
111
|
6 |
|
if ($this->object instanceof Category) { |
112
|
1 |
|
$data['categories'] = [ |
113
|
1 |
|
$this->object->getId() => $this->getCategoryData($this->object), |
114
|
|
|
]; |
115
|
|
|
} |
116
|
6 |
|
elseif ($this->object instanceof WithCategories) { |
117
|
5 |
|
$data['categories'] = []; |
118
|
5 |
|
foreach ($this->object->getCategories() as $category) { |
|
|
|
|
119
|
1 |
|
$data['categories'][$category->getId()] = $this->getCategoryData($category); |
120
|
|
|
} |
121
|
|
|
} |
122
|
6 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get all setup information from a Category class |
126
|
|
|
* |
127
|
|
|
* @param Category $category Category class to export |
128
|
|
|
* |
129
|
|
|
* @return object |
130
|
|
|
* @throws Exception |
131
|
|
|
*/ |
132
|
1 |
|
protected function getCategoryData(Category $category) : object { |
133
|
|
|
return (object) [ |
134
|
1 |
|
'id' => $category->getId(), |
135
|
1 |
|
'name' => $category->getName(), |
136
|
1 |
|
'skip' => $category->getSkip(), |
137
|
1 |
|
'rounds' => $category instanceof WithRounds ? $category->queryRounds()->ids()->get() : [], |
|
|
|
|
138
|
1 |
|
'groups' => $category instanceof WithGroups ? $category->queryGroups()->ids()->get() : [], |
|
|
|
|
139
|
1 |
|
'teams' => $category instanceof WithTeams ? $category->getTeamContainer()->ids()->unique()->get() : [], |
|
|
|
|
140
|
1 |
|
'games' => $category instanceof WithGames ? $category->getGameContainer()->ids()->get() : [], |
|
|
|
|
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get all setup information for rounds |
146
|
|
|
* |
147
|
|
|
* @param array $data |
148
|
|
|
* |
149
|
|
|
* @throws Exception |
150
|
|
|
*/ |
151
|
6 |
|
protected function getRoundsData(array &$data) : void { |
152
|
6 |
|
if ($this->object instanceof Round) { |
153
|
2 |
|
$data['rounds'] = [ |
154
|
2 |
|
$this->object->getId() => $this->getRoundData($this->object), |
155
|
|
|
]; |
156
|
|
|
} |
157
|
6 |
|
elseif ($this->object instanceof WithRounds) { |
158
|
5 |
|
$data['rounds'] = []; |
159
|
5 |
|
foreach ($this->object->getRounds() as $round) { |
|
|
|
|
160
|
5 |
|
$data['rounds'][$round->getId()] = $this->getRoundData($round); |
161
|
|
|
} |
162
|
|
|
} |
163
|
6 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get all setup information from a Round class |
167
|
|
|
* |
168
|
|
|
* @param Round $round Round class to export |
169
|
|
|
* |
170
|
|
|
* @return object |
171
|
|
|
* @throws Exception |
172
|
|
|
*/ |
173
|
6 |
|
protected function getRoundData(Round $round) : object { |
174
|
|
|
return (object) [ |
175
|
6 |
|
'id' => $round->getId(), |
176
|
6 |
|
'name' => $round->getName(), |
177
|
6 |
|
'skip' => $round->getSkip(), |
178
|
6 |
|
'played' => $round->isPlayed(), |
179
|
6 |
|
'groups' => $round instanceof WithGroups ? $round->queryGroups()->ids()->get() : [], |
|
|
|
|
180
|
6 |
|
'teams' => $round instanceof WithTeams ? $round->getTeamContainer()->ids()->unique()->get() : [], |
|
|
|
|
181
|
6 |
|
'games' => $round instanceof WithGames ? $round->getGameContainer()->ids()->get() : [], |
|
|
|
|
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get all setup information for groups and progressions |
187
|
|
|
* |
188
|
|
|
* @param array $data |
189
|
|
|
* |
190
|
|
|
* @throws Exception |
191
|
|
|
*/ |
192
|
6 |
|
protected function getGroupsData(array &$data) : void { |
193
|
6 |
|
$data['groups'] = []; |
194
|
6 |
|
$data['progressions'] = []; |
195
|
6 |
|
if ($this->object instanceof Group) { |
196
|
2 |
|
$data['groups'][$this->object->getId()] = $this->getGroupData($this->object); |
197
|
2 |
|
foreach ($this->object->getProgressions() as $progression) { |
|
|
|
|
198
|
1 |
|
$data['progressions'][] = $this->getProgressionData($progression); |
199
|
|
|
} |
200
|
|
|
} |
201
|
6 |
|
elseif ($this->object instanceof WithGroups) { |
202
|
6 |
|
foreach ($this->object->getGroups() as $group) { |
|
|
|
|
203
|
6 |
|
$data['groups'][$group->getId()] = $this->getGroupData($group); |
204
|
6 |
|
foreach ($group->getProgressions() as $progression) { |
205
|
5 |
|
$data['progressions'][] = $this->getProgressionData($progression); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
6 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get all setup information from a Group class |
213
|
|
|
* |
214
|
|
|
* @param Group $group Group class to export |
215
|
|
|
* |
216
|
|
|
* @return object |
217
|
|
|
* @throws Exception |
218
|
|
|
*/ |
219
|
6 |
|
protected function getGroupData(Group $group) : object { |
220
|
|
|
return (object) [ |
221
|
6 |
|
'id' => $group->getId(), |
222
|
6 |
|
'name' => $group->getName(), |
223
|
6 |
|
'type' => $group->getType(), |
224
|
6 |
|
'skip' => $group->getSkip(), |
225
|
|
|
'points' => (object) [ |
226
|
6 |
|
'win' => $group->getWinPoints(), |
227
|
6 |
|
'loss' => $group->getLostPoints(), |
228
|
6 |
|
'draw' => $group->getDrawPoints(), |
229
|
6 |
|
'second' => $group->getSecondPoints(), |
230
|
6 |
|
'third' => $group->getThirdPoints(), |
231
|
6 |
|
'progression' => $group->getProgressPoints(), |
232
|
|
|
], |
233
|
6 |
|
'played' => $group->isPlayed(), |
234
|
6 |
|
'inGame' => $group->getInGame(), |
235
|
6 |
|
'maxSize' => $group->getMaxSize(), |
236
|
6 |
|
'teams' => $group instanceof WithTeams ? $group->getTeamContainer()->ids()->unique()->get() : [], |
|
|
|
|
237
|
6 |
|
'games' => $group instanceof WithGames ? $group->getGameContainer()->ids()->get() : [], |
|
|
|
|
238
|
|
|
]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get all setup information from a Progression class |
243
|
|
|
* |
244
|
|
|
* @param Progression $progression Progression class to export |
245
|
|
|
* |
246
|
|
|
* @return object |
247
|
|
|
*/ |
248
|
5 |
|
protected function getProgressionData(Progression $progression) : object { |
249
|
|
|
return (object) [ |
250
|
5 |
|
'from' => $progression->getFrom()->getId(), |
251
|
5 |
|
'to' => $progression->getTo()->getId(), |
252
|
5 |
|
'offset' => $progression->getStart(), |
253
|
5 |
|
'length' => $progression->getLen(), |
254
|
5 |
|
'progressed' => $progression->isProgressed(), |
255
|
5 |
|
'filters' => array_map([$this, 'getTeamFilterData'], $progression->getFilters()), |
256
|
|
|
]; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get all setup information from a TeamFilter class |
261
|
|
|
* |
262
|
|
|
* @param TeamFilter $filter TeamFilter class to export |
263
|
|
|
* |
264
|
|
|
* @return object |
265
|
|
|
*/ |
266
|
1 |
|
protected function getTeamFilterData(TeamFilter $filter) : object { |
267
|
|
|
return (object) [ |
268
|
1 |
|
'what' => $filter->getWhat(), |
269
|
1 |
|
'how' => $filter->getHow(), |
270
|
1 |
|
'val' => $filter->getVal(), |
271
|
1 |
|
'groups' => $filter->getGroups(), |
272
|
|
|
]; |
273
|
|
|
} |
274
|
|
|
} |