Passed
Push — master ( 411ad7...cc8d44 )
by Tomáš
03:33
created

Team::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 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace TournamentGenerator;
4
5
use Exception;
6
use InvalidArgumentException;
7
use TournamentGenerator\Export\Exporter;
8
use TournamentGenerator\Export\Single\TeamExporter;
9
use TournamentGenerator\Interfaces\Exportable;
10
use TournamentGenerator\Traits\HasPositions;
11
use TournamentGenerator\Traits\HasScore;
12
13
/**
14
 * Class that identifies a team and holds the scores from the whole tournament.
15
 *
16
 * @author  Tomáš Vojík <[email protected]>
17
 *
18
 * @since   0.1
19
 * @package TournamentGenerator
20
 */
21
class Team extends Base implements Exportable
22
{
23
	use HasScore;
24
	use HasPositions;
25
26
	/**
27
	 * @var string     $name The name of the team
28
	 * @var string|int $id   The unique identifier of the team
29
	 */
30
31
	/** @var Game[] $games A list of games played by this team */
32
	protected array $games = [];
33
	/** @var array $gamesWith Multi-dimensional associative array of number of games together with other teams */
34
	protected array $gamesWith = [];
35
36
	/**
37
	 * Initiates a team class
38
	 *
39
	 * @param string     $name Name of the team
40
	 * @param string|int $id   Unique identifier of the team
41
	 *
42
	 * @throws InvalidArgumentException if the provided argument id is not of type 'null' or 'string' or 'int'
43
	 */
44 260
	public function __construct(string $name = 'team', $id = null) {
45 260
		$this->setName($name);
46 260
		$this->setId($id ?? uniqid('', false));
47 260
	}
48
49
	/**
50
	 * Gets team statistics from the given group without the group object
51
	 *
52
	 * @param string|int $groupId Unique identifier of the group to get its results
53
	 *
54
	 * @return array  All the statistics including points, score, wins, draws, losses, times being second, times being third
55
	 * @throws Exception if the group with given groupId doesn't exist
56
	 *
57
	 */
58 1
	public function getGamesInfo($groupId) : array {
59 1
		return array_filter($this->getGroupResults($groupId), static function($k) {
60 1
			return $k !== 'group';
61 1
		}, ARRAY_FILTER_USE_KEY);
62
	}
63
64
	/**
65
	 * Gets team statistics from the given group
66
	 *
67
	 * @param string|int|null $groupId Unique identifier of the group to get its results
68
	 *
69
	 * @return array  All the statistics including points, score, wins, draws, losses, times being second, times being third if the group id is set or all the statistics
70
	 * @throws Exception if the group with given groupId doesn't exist
71
	 *
72
	 */
73 21
	public function getGroupResults($groupId = null) : array {
74 21
		if (isset($groupId)) {
75 6
			if (!isset($this->groupResults[$groupId])) {
76 1
				throw new Exception('Trying to get nonexistent group results ('.$groupId.')');
77
			}
78 5
			return $this->groupResults[$groupId];
79
		}
80 15
		return $this->groupResults;
81
	}
82
83
	/**
84
	 * Creates a new data-array to store statistics for a new group
85
	 *
86
	 * Resets the statistics if the group was already added
87
	 *
88
	 * @param Group $group A group object to add its results
89
	 *
90
	 * @return $this
91
	 */
92 212
	public function addGroupResults(Group $group) : Team {
93 212
		$this->groupResults[$group->getId()] = [
94 212
			'group'  => $group,
95 212
			'points' => 0,
96 212
			'score'  => 0,
97 212
			'wins'   => 0,
98 212
			'draws'  => 0,
99 212
			'losses' => 0,
100 212
			'second' => 0,
101 212
			'third'  => 0
102
		];
103 212
		return $this;
104
	}
105
106
	/**
107
	 * Adds a record of a game with another team in a group
108
	 *
109
	 * @param Team  $team  A team that played with this team
110
	 * @param Group $group A group that the teams were playing in
111
	 *
112
	 * @return $this
113
	 */
114 201
	public function addGameWith(Team $team, Group $group) : Team {
115 201
		if (!isset($this->gamesWith[$group->getId()][$team->getId()])) {
116 201
			$this->gamesWith[$group->getId()][$team->getId()] = 0;
117
		}
118 201
		$this->gamesWith[$group->getId()][$team->getId()]++;
119 201
		return $this;
120
	}
121
122
	/**
123
	 * Gets a record of a game with another team or teams
124
	 *
125
	 * @param Team|null  $team  A team to get the games with
126
	 * @param Group|null $group A group from where to get the games
127
	 *
128
	 * @return array|int The number of games played with a team in a group if both arguments are given, array of all games with all teams from a group if only group is given, array of games with team from all groups if only a team argument is given or all games with all teams from all groups if no argument is given
129
	 */
130 3
	public function getGameWith(Team $team = null, Group $group = null) {
131 3
		if (isset($group)) {
132 3
			if (isset($team)) {
133 3
				return $this->gamesWith[$group->getId()][$team->getId()];
134
			}
135 3
			return $this->gamesWith[$group->getId()];
136
		}
137 2
		if (isset($team)) {
138 1
			$return = [];
139 1
			foreach ($this->gamesWith as $id => $games) {
140 1
				$filter = array_filter($games, static function($key) use ($team) {
141 1
					return $key === $team->getId();
142 1
				}, ARRAY_FILTER_USE_KEY);
143 1
				if (count($filter) > 0) {
144 1
					$return[$id] = $filter;
145
				}
146
			}
147 1
			return $return;
148
		}
149 1
		return $this->gamesWith;
150
	}
151
152
	/**
153
	 * Adds a group to a team and creates an array for all games to be played
154
	 *
155
	 * @param Group $group A group to add
156
	 *
157
	 * @return $this
158
	 */
159 1
	public function addGroup(Group $group) : Team {
160 1
		if (!isset($this->games[$group->getId()])) {
161 1
			$this->games[$group->getId()] = [];
162
		}
163 1
		return $this;
164
	}
165
166
	/**
167
	 * Adds a game to this team
168
	 *
169
	 * @param Game $game A game to add
170
	 *
171
	 * @return $this
172
	 */
173 201
	public function addGame(Game $game) : Team {
174 201
		$group = $game->getGroup();
175 201
		if (!isset($this->games[$group->getId()])) {
176 201
			$this->games[$group->getId()] = [];
177
		}
178 201
		$this->games[$group->getId()][] = $game;
179 201
		return $this;
180
	}
181
182
	/**
183
	 * Gets all game from given group
184
	 *
185
	 * @param Group|null      $group   A group to get its game from
186
	 * @param string|int|null $groupId An id of group to get its game from
187
	 *
188
	 * @return array Games from a group or all games if both arguments are null
189
	 */
190 4
	public function getGames(?Group $group = null, $groupId = null) {
191 4
		if (!is_null($group) && isset($this->games[$group->getId()])) {
192 1
			return $this->games[$group->getId()];
193
		}
194 4
		if (isset($groupId, $this->games[$groupId])) {
195 3
			return $this->games[$groupId];
196
		}
197 1
		return $this->games;
198
	}
199
200
	/**
201
	 * Prepares an export query for the object
202
	 *
203
	 * @return Exporter Exporter for this class
204
	 */
205 13
	public function export() : Exporter {
206 13
		return TeamExporter::start($this);
207
	}
208
}
209