Completed
Push — master ( 5c3586...68bdab )
by Tomáš
02:53
created

ExportBase::__construct()   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
7
use TournamentGenerator\HierarchyBase;
8
9
/**
10
 * Base class for exporters
11
 *
12
 * Exporters operate on some HierarchyBase class. They extract data and/or settings from these classes in a form of PHP array.
13
 * Exporters also allow of adding modifiers to the exported query - adding more data. These modifiers are added via specific methods (usually starting with "with" keyword).
14
 *
15
 * @package TournamentGenerator\Export
16
 * @author  Tomáš Vojík <[email protected]>
17
 * @since   0.5
18
 */
19
abstract class ExportBase implements Export
20
{
21
22
	/** @var HierarchyBase Hierarchy object to export */
23
	protected HierarchyBase $object;
24
	protected array         $modifiers = [];
25
26 12
	public function __construct(HierarchyBase $object) {
27 12
		$this->object = $object;
28 12
	}
29
30
	/**
31
	 * Finish the export query -> get the result
32
	 *
33
	 * @return array The query result
34
	 */
35 11
	public function get() : array {
36 11
		$data = $this->getBasic();
37 11
		$this->applyModifiers($data);
38 11
		return array_map(static function(object $object) {
39 11
			unset($object->object);
40 11
			return $object;
41 11
		}, $data);
42
	}
43
44
	/**
45
	 * Apply set modifiers to data array
46
	 *
47
	 * @param array $data
48
	 */
49 11
	protected function applyModifiers(array &$data) : void {
50 11
		foreach ($this->modifiers as $modifier) {
51 5
			$this->$modifier($data);
52
		}
53
	}
54
}