1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TournamentGenerator\Export\Hierarchy; |
5
|
|
|
|
6
|
|
|
use Error; |
7
|
|
|
use TournamentGenerator\Export\Export; |
8
|
|
|
use TournamentGenerator\Export\ExportBase; |
9
|
|
|
use TournamentGenerator\HierarchyBase; |
10
|
|
|
use TournamentGenerator\Interfaces\WithGames; |
11
|
|
|
use TournamentGenerator\Interfaces\WithId; |
12
|
|
|
use TournamentGenerator\Interfaces\WithTeams; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Basic exporter |
16
|
|
|
* |
17
|
|
|
* Basic exporter class for exporting all data from HierarchyBase objects. It uses all other specialized exporters and also inherits their modifiers specific. |
18
|
|
|
* |
19
|
|
|
* @package TournamentGenerator\Export |
20
|
|
|
* @author Tomáš Vojík <[email protected]> |
21
|
|
|
* @since 0.5 |
22
|
|
|
*/ |
23
|
|
|
class Exporter extends ExportBase |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** @var ExportBase[] Other exporters used */ |
27
|
|
|
protected array $exporters = []; |
28
|
|
|
|
29
|
|
|
public function __construct(HierarchyBase $object) { |
30
|
|
|
if ($object instanceof WithTeams) { |
31
|
|
|
$this->exporters['teams'] = TeamsExporter::start($object); |
32
|
|
|
} |
33
|
|
|
if ($object instanceof WithGames) { |
34
|
|
|
$this->exporters['games'] = GamesExporter::start($object); |
35
|
|
|
} |
36
|
|
|
parent::__construct($object); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Simple export query without any modifiers |
41
|
|
|
* |
42
|
|
|
* @param HierarchyBase $object |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public static function export(WithId $object) : array { |
47
|
|
|
return self::start($object)->get(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Start an export query |
52
|
|
|
* |
53
|
|
|
* @param HierarchyBase $object |
54
|
|
|
* |
55
|
|
|
* @return Export |
56
|
|
|
*/ |
57
|
|
|
public static function start(WithId $object) : Export { |
58
|
|
|
return new self($object); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Try to call a modifier method on other used exporters |
63
|
|
|
* |
64
|
|
|
* @param string $name |
65
|
|
|
* @param array $arguments |
66
|
|
|
* |
67
|
|
|
* @return Exporter |
68
|
|
|
*/ |
69
|
|
|
public function __call(string $name, array $arguments) { |
70
|
|
|
$called = false; |
71
|
|
|
foreach ($this->exporters as $exporter) { |
72
|
|
|
if (method_exists($exporter, $name)) { |
73
|
|
|
$exporter->$name(...$arguments); |
74
|
|
|
$called = true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
if ($called) { |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new Error('Call to undefined method '.__CLASS__.'::'.$name.'()'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Finish the export query -> get the result |
86
|
|
|
* |
87
|
|
|
* @return array The query result |
88
|
|
|
*/ |
89
|
|
|
public function get() : array { |
90
|
|
|
$data = $this->getBasic(); |
91
|
|
|
$this->applyModifiers($data); |
92
|
|
|
foreach ($this->exporters as $name => $exporter) { |
93
|
|
|
$data[$name] = $exporter->get(); |
94
|
|
|
} |
95
|
|
|
return $data; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Gets the basic unmodified data |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function getBasic() : array { |
104
|
|
|
return []; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @defgroup ExporterQueryModifiers Query modifiers |
109
|
|
|
* @brief Modifier methods for the query |
110
|
|
|
*/ |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Query modifier, adding a setup exporter |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
* @ingroup ExporterQueryModifiers |
117
|
|
|
*/ |
118
|
|
|
public function withSetup() : Exporter { |
119
|
|
|
$this->exporters['setup'] = SetupExporter::start($this->object); |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
} |