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

TeamExporter::export()   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
/** @noinspection PhpDocFieldTypeMismatchInspection */
3
4
5
namespace TournamentGenerator\Export;
6
7
use Exception;
8
use InvalidArgumentException;
9
use TournamentGenerator\HierarchyBase;
10
use TournamentGenerator\Interfaces\WithTeams;
11
use TournamentGenerator\Team;
12
13
/**
14
 * Exporter for teams
15
 *
16
 * A specific exporter for teams and their related data.
17
 *
18
 * @package TournamentGenerator\Export
19
 * @author  Tomáš Vojík <[email protected]>
20
 * @since   0.5
21
 */
22
class TeamExporter extends ExportBase
23
{
24
25
	/** @var WithTeams */
26
	protected HierarchyBase $object;
27
28
	/**
29
	 * TeamExporter constructor.
30
	 *
31
	 * @param HierarchyBase $object
32
	 */
33 11
	public function __construct(HierarchyBase $object) {
34 11
		if (!$object instanceof WithTeams) {
35 1
			throw new InvalidArgumentException('Object must be instance of WithTeams.');
36
		}
37 10
		parent::__construct($object);
38 10
	}
39
40
	/**
41
	 * Simple export query without any modifiers
42
	 *
43
	 * @param HierarchyBase $object
44
	 *
45
	 * @return array
46
	 */
47 3
	public static function export(HierarchyBase $object) : array {
48 3
		return self::start($object)->get();
49
	}
50
51
	/**
52
	 * Start an export query
53
	 *
54
	 * @param HierarchyBase $object
55
	 *
56
	 * @return Export
57
	 */
58 11
	public static function start(HierarchyBase $object) : Export {
59 11
		return new self($object);
60
	}
61
62
	/**
63
	 * Gets the basic unmodified data
64
	 *
65
	 * @return array
66
	 */
67 9
	public function getBasic() : array {
68 9
		return array_map(static function(Team $team) {
69
			return (object) [
70 9
				'object' => $team, // Passed for reference in the modifier methods
71 9
				'id'     => $team->getId(),
72 9
				'name'   => $team->getName(),
73
			];
74 9
		}, $this->object->getTeams());
0 ignored issues
show
Bug introduced by
The method getTeams() does not exist on TournamentGenerator\HierarchyBase. Since it exists in all sub-types, consider adding an abstract or default implementation to TournamentGenerator\HierarchyBase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
		}, $this->object->/** @scrutinizer ignore-call */ getTeams());
Loading history...
75
	}
76
77
	/**
78
	 * @defgroup TeamExporterQueryModifiers Query modifiers
79
	 * @brief    Modifier methods for the query
80
	 */
81
82
	/**
83
	 * Include team scores in the result set
84
	 *
85
	 * @return TeamExporter
86
	 * @ingroup TeamExporterQueryModifiers
87
	 */
88 5
	public function withScores() : TeamExporter {
89 5
		$this->modifiers[] = 'withScoresModifier';
90 5
		return $this;
91
	}
92
93
	/**
94
	 * @defgroup TeamExporterModifiers Modifier callbacks
95
	 * @brief    Modifier callbacks
96
	 * @details  Modifier callbacks alter the input in some way and return the modified result.
97
	 */
98
99
	/**
100
	 * Includes team scores in the result set
101
	 *
102
	 * @param array $data
103
	 *
104
	 * @return array
105
	 * @ingroup TeamExporterModifiers
106
	 * @throws Exception
107
	 */
108 5
	protected function withScoresModifier(array &$data) : array {
109 5
		foreach ($data as $object) {
110
			/** @var Team $team */
111 5
			$team = $object->object;
112 5
			$object->scores = array_map(static function(array $group) {
113 4
				unset($group['group']); // Get rid of the Group object reference
114 4
				return $group;
115 5
			}, $team->getGroupResults());
116
		}
117 5
		return $data;
118
	}
119
}