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