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

Progression::getTo()   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 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 0
b 0
f 0
1
<?php
2
3
namespace TournamentGenerator;
4
5
use Exception;
6
7
/**
8
 * Progression is a class that takes care of moving teams between groups.
9
 *
10
 * Progressions are used in order to move teams from one group to another. This can be used to progress the winning teams to semi-finals and finals round, but you can also progress teams between groups in different categories and even tournaments if you ever needed to.
11
 * Progressions use a similar syntax to php's array_slice() function or it can use defined filters.
12
 *
13
 * @package TournamentGenerator
14
 * @author  Tomáš Vojík <[email protected]>
15
 * @since   0.1
16
 */
17
class Progression
18
{
19
20
	/** @var Group What group to progress from */
21
	protected Group $from;
22
	/** @var Group What group to progress to */
23
	protected Group $to;
24
	/** @var int Offset to start picking teams */
25
	protected int   $start;
26
	/** @var int|null Maximum number of teams to progress */
27
	protected ?int  $len;
28
	/** @var TeamFilter[] Filters to use */
29
	protected array $filters    = [];
30
	/** @var bool If the progression was already called */
31
	protected bool  $progressed = false;
32
33
	/**
34
	 * Progression constructor.
35
	 *
36
	 * @param Group    $from What group to progress from
37
	 * @param Group    $to What group to progress to
38
	 * @param int      $start Offset to start picking teams
39
	 * @param int|null $len Maximum number of teams to progress
40
	 */
41 35
	public function __construct(Group $from, Group $to, int $start = 0, ?int $len = null) {
42 35
		$this->from = $from;
43 35
		$this->to = $to;
44 35
		$this->start = $start;
45 35
		$this->len = $len;
46 35
	}
47
48
	/**
49
	 * Gets a description
50
	 *
51
	 * @return string
52
	 */
53 17
	public function __toString() {
54 17
		return 'Team from '.$this->from;
55
	}
56
57
	/**
58
	 * Sets progression's filters
59
	 *
60
	 * @param TeamFilter[] $filters
61
	 *
62
	 * @return $this
63
	 */
64 1
	public function setFilters(array $filters) : Progression {
65 1
		$this->filters = $filters;
66 1
		return $this;
67
	}
68
69
	/**
70
	 * Adds progression's filters
71
	 *
72
	 * @param TeamFilter[] $filters
73
	 *
74
	 * @return $this
75
	 */
76 8
	public function addFilter(TeamFilter ...$filters) : Progression {
77 8
		foreach ($filters as $filter) {
78 8
			$this->filters[] = $filter;
79
		}
80 8
		return $this;
81
	}
82
83
	/**
84
	 * Progress the teams using set rules
85
	 *
86
	 * @param bool $blank If true -> do not move the real team objects, but create new dummy teams
87
	 *
88
	 * @return $this
89
	 * @throws Exception
90
	 */
91 33
	public function progress(bool $blank = false) : Progression {
92 33
		if ($this->progressed) {
93 1
			return $this;
94
		}
95
96 33
		if ($blank) {
97 17
			$teams = $this->from->isPlayed() ? $this->from->sortTeams(null, $this->filters) : $this->from->simulate($this->filters);
98
		}
99
		else {
100 16
			$teams = $this->from->sortTeams(null, $this->filters);
101
		}
102
103 33
		if ($this->start !== 0 || $this->len !== null) {
104 29
			$next = array_splice($teams, $this->start, ($this->len ?? count($teams)));
105
		}
106
		else {
107 5
			$next = $teams;
108
		}
109
110 33
		$i = 1;
111
112 33
		foreach ($next as $team) {
113 33
			if ($blank) {
114 17
				$this->to->addTeam(new BlankTeam($this.' - '.$i++, $team, $this->from, $this));
115
			}
116
			else {
117 16
				$team->addPoints($this->from->getProgressPoints());
118
			}
119
		}
120
121 33
		$this->from->addProgressed(...$next);
122 33
		if (!$blank) {
123 16
			$this->to->addTeam(...$next);
124
		}
125 33
		$this->progressed = true;
126 33
		return $this;
127
	}
128
129
	/**
130
	 * Reset progression
131
	 *
132
	 * @warning This does not remove the teams from the progressed groups!
133
	 *
134
	 * @return $this
135
	 */
136 1
	public function reset() : Progression {
137 1
		$this->progressed = false;
138 1
		return $this;
139
	}
140
141
	/**
142
	 * @return Group
143
	 */
144 3
	public function getFrom() : Group {
145 3
		return $this->from;
146
	}
147
148
	/**
149
	 * @return Group
150
	 */
151 3
	public function getTo() : Group {
152 3
		return $this->to;
153
	}
154
155
	/**
156
	 * @return int
157
	 */
158 3
	public function getStart() : int {
159 3
		return $this->start;
160
	}
161
162
	/**
163
	 * @return int|null
164
	 */
165 3
	public function getLen() : ?int {
166 3
		return $this->len;
167
	}
168
169
	/**
170
	 * @return TeamFilter[]
171
	 */
172 3
	public function getFilters() : array {
173 3
		return $this->filters;
174
	}
175
176
	/**
177
	 * @return bool
178
	 */
179 3
	public function isProgressed() : bool {
180 3
		return $this->progressed;
181
	}
182
183
}
184