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