1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TournamentGenerator\Export\Hierarchy; |
5
|
|
|
|
6
|
|
|
use Error; |
7
|
|
|
use TournamentGenerator\Export\ExporterInterface; |
8
|
|
|
use TournamentGenerator\Export\ExporterBase; |
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 ExporterBase |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** @var ExporterBase[] Other exporters used */ |
27
|
|
|
protected array $exporters = []; |
28
|
|
|
|
29
|
10 |
|
public function __construct(HierarchyBase $object) { |
30
|
10 |
|
if ($object instanceof WithTeams) { |
31
|
10 |
|
$this->exporters['teams'] = TeamsExporter::start($object); |
32
|
|
|
} |
33
|
10 |
|
if ($object instanceof WithGames) { |
34
|
10 |
|
$this->exporters['games'] = GamesExporter::start($object); |
35
|
|
|
} |
36
|
10 |
|
parent::__construct($object); |
37
|
10 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Simple export query without any modifiers |
41
|
|
|
* |
42
|
|
|
* @param HierarchyBase $object |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
3 |
|
public static function export(WithId $object) : array { |
47
|
3 |
|
return self::start($object)->get(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Start an export query |
52
|
|
|
* |
53
|
|
|
* @param HierarchyBase $object |
54
|
|
|
* |
55
|
|
|
* @return Exporter |
56
|
|
|
*/ |
57
|
10 |
|
public static function start(WithId $object) : ExporterInterface { |
58
|
10 |
|
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
|
3 |
|
public function __call(string $name, array $arguments) { |
70
|
3 |
|
$called = false; |
71
|
3 |
|
foreach ($this->exporters as $exporter) { |
72
|
3 |
|
if (method_exists($exporter, $name)) { |
73
|
2 |
|
$exporter->$name(...$arguments); |
74
|
2 |
|
$called = true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
3 |
|
if ($called) { |
78
|
2 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
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
|
9 |
|
public function get() : array { |
90
|
9 |
|
$data = $this->getBasic(); |
91
|
9 |
|
$this->applyModifiers($data); |
92
|
9 |
|
foreach ($this->exporters as $name => $exporter) { |
93
|
9 |
|
if ($name === 'setup') { |
94
|
4 |
|
$data += $exporter->get(); |
95
|
|
|
} |
96
|
|
|
else { |
97
|
9 |
|
$data[$name] = $exporter->get(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
9 |
|
return $data; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets the basic unmodified data |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
9 |
|
public function getBasic() : array { |
109
|
9 |
|
return []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @defgroup ExporterQueryModifiers Query modifiers |
114
|
|
|
* @brief Modifier methods for the query |
115
|
|
|
*/ |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Query modifier, adding a setup exporter |
119
|
|
|
* |
120
|
|
|
* @return $this |
121
|
|
|
* @ingroup ExporterQueryModifiers |
122
|
|
|
*/ |
123
|
4 |
|
public function withSetup() : ExporterInterface { |
124
|
4 |
|
$this->exporters['setup'] = SetupExporter::start($this->object); |
125
|
4 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |