Completed
Push — master ( a743c4...8e8354 )
by Tomáš
02:35
created

Exporter::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
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 8
	public function __construct(HierarchyBase $object) {
26 8
		if ($object instanceof WithTeams) {
27 8
			$this->exporters['teams'] = TeamExporter::start($object);
28
		}
29 8
		if ($object instanceof WithGames) {
30 8
			$this->exporters['games'] = GameExporter::start($object);
31
		}
32 8
		parent::__construct($object);
33 8
	}
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 3
	public static function export(HierarchyBase $object) : array {
66 3
		return self::start($object)->get();
67
	}
68
69
	/**
70
	 * Start an export query
71
	 *
72
	 * @param HierarchyBase $object
73
	 *
74
	 * @return Export
75
	 */
76 8
	public static function start(HierarchyBase $object) : Export {
77 8
		return new self($object);
78
	}
79
80
	/**
81
	 * Finish the export query -> get the result
82
	 *
83
	 * @return array The query result
84
	 */
85 7
	public function get() : array {
86 7
		$data = $this->getBasic();
87 7
		$this->applyModifiers($data);
88 7
		foreach ($this->exporters as $name => $exporter) {
89 7
			$data[$name] = $exporter->get();
90
		}
91 7
		return $data;
92
	}
93
94
	/**
95
	 * Gets the basic unmodified data
96
	 *
97
	 * @return array
98
	 */
99 7
	public function getBasic() : array {
100 7
		return [];
101
	}
102
103
	/**
104
	 * @defgroup ExporterQueryModifiers Query modifiers
105
	 * @brief    Modifier methods for the query
106
	 */
107
108
	/**
109
	 * Query modifier, adding a setup exporter
110
	 *
111
	 * @return $this
112
	 * @ingroup ExporterQueryModifiers
113
	 */
114 2
	public function withSetup() : Exporter {
115 2
		$this->exporters['setup'] = SetupExporter::start($this->object);
116 2
		return $this;
117
	}
118
}