|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SecretSanta; |
|
4
|
|
|
|
|
5
|
|
|
use SecretSanta\Exceptions\PlayersCollectionException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class PlayersCollection |
|
9
|
|
|
* @package SecretSanta |
|
10
|
|
|
*/ |
|
11
|
|
|
class PlayersCollection implements \Countable |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var Player[] */ |
|
14
|
|
|
private $players = []; |
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
private $exclusivePlayers = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param Player $player |
|
20
|
|
|
* @throws PlayersCollectionException |
|
21
|
|
|
*/ |
|
22
|
11 |
|
public function addPlayer(Player $player) |
|
23
|
|
|
{ |
|
24
|
11 |
|
if (!$this->isDuplicatePlayer($player)) { |
|
25
|
11 |
|
$this->players[$player->id()] = $player; |
|
26
|
11 |
|
} |
|
27
|
11 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Player $player |
|
31
|
|
|
* @param Player $couple |
|
32
|
|
|
* @throws PlayersCollectionException |
|
33
|
|
|
*/ |
|
34
|
17 |
|
public function addCouple(Player $player, Player $couple) |
|
35
|
|
|
{ |
|
36
|
17 |
|
if (!$this->areDifferentPlayers([$player, $couple])) { |
|
37
|
2 |
|
throw new PlayersCollectionException('The couple can not be the same player'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
15 |
|
if (!$this->isDuplicatePlayer($player) && !$this->isDuplicatePlayer($couple) ) { |
|
41
|
14 |
|
$this->players[$player->id()] = $player; |
|
42
|
14 |
|
$this->players[$couple->id()] = $couple; |
|
43
|
|
|
|
|
44
|
14 |
|
$this->exclusivePlayers([$player, $couple]); |
|
45
|
14 |
|
} |
|
46
|
14 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Player[] $players |
|
50
|
|
|
* @throws PlayersCollectionException |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function addExclusivePlayers($players) |
|
53
|
|
|
{ |
|
54
|
4 |
|
if (!$this->areDifferentPlayers($players)) { |
|
55
|
1 |
|
throw new PlayersCollectionException('The players must be different'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
foreach ($players as $player) { |
|
59
|
3 |
|
if (!$this->isDuplicatePlayer($player)) { |
|
60
|
3 |
|
$this->players[$player->id()] = $player; |
|
61
|
3 |
|
} |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
$this->exclusivePlayers($players); |
|
65
|
3 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return Player[] |
|
69
|
|
|
*/ |
|
70
|
13 |
|
public function players() |
|
71
|
|
|
{ |
|
72
|
13 |
|
return $this->players; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return Player[] |
|
77
|
|
|
*/ |
|
78
|
11 |
|
public function shufflePlayers() |
|
79
|
|
|
{ |
|
80
|
11 |
|
return $this->shuffleAssoc($this->players); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $id |
|
85
|
|
|
* @return Player |
|
86
|
|
|
* @throws PlayersCollectionException |
|
87
|
|
|
*/ |
|
88
|
13 |
|
public function player($id) |
|
89
|
|
|
{ |
|
90
|
13 |
|
if (!isset($this->players[$id])) { |
|
91
|
1 |
|
throw new PlayersCollectionException("Player {$id} not found"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
12 |
|
return $this->players[$id]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Player[] $players |
|
100
|
|
|
*/ |
|
101
|
16 |
|
private function exclusivePlayers($players) |
|
102
|
|
|
{ |
|
103
|
16 |
|
foreach ($players as $mainPlayer) { |
|
104
|
16 |
|
foreach ($players as $player) { |
|
105
|
16 |
|
if ($mainPlayer->id() == $player->id()){ |
|
106
|
16 |
|
continue; |
|
107
|
|
|
} |
|
108
|
16 |
|
$this->exclusivePlayers[$mainPlayer->id()][] = $player->id(); |
|
109
|
16 |
|
} |
|
110
|
16 |
|
} |
|
111
|
16 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param Player $player |
|
115
|
|
|
* @param Player $player2 |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
11 |
|
public function areExclusive(Player $player, Player $player2) |
|
119
|
|
|
{ |
|
120
|
11 |
|
if (array_key_exists($player->id(), $this->exclusivePlayers) |
|
121
|
11 |
|
&& in_array($player2->id(), $this->exclusivePlayers[$player->id()]) |
|
122
|
11 |
|
) { |
|
123
|
10 |
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
11 |
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return int |
|
131
|
|
|
*/ |
|
132
|
12 |
|
public function count() |
|
133
|
|
|
{ |
|
134
|
12 |
|
return count($this->players); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return int |
|
139
|
|
|
*/ |
|
140
|
11 |
|
public function countExclusivePlayers() |
|
141
|
|
|
{ |
|
142
|
11 |
|
return count($this->exclusivePlayers); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param Player $player |
|
148
|
|
|
* @return bool |
|
149
|
|
|
* @throws PlayersCollectionException |
|
150
|
|
|
*/ |
|
151
|
21 |
|
private function isDuplicatePlayer(Player $player) |
|
152
|
|
|
{ |
|
153
|
21 |
|
if (array_key_exists($player->id(), $this->players)) { |
|
154
|
4 |
|
throw new PlayersCollectionException('Duplicate player: '.$player->email()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
21 |
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param Player[] $players |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
20 |
|
private function areDifferentPlayers($players) |
|
165
|
|
|
{ |
|
166
|
20 |
|
$uniqueIds = []; |
|
167
|
20 |
|
foreach ($players as $player) { |
|
168
|
20 |
|
$uniqueIds[] = $player->id(); |
|
169
|
20 |
|
} |
|
170
|
|
|
|
|
171
|
20 |
|
return count($players) == count(array_unique($uniqueIds)); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param array $list |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
11 |
|
private function shuffleAssoc($list) |
|
179
|
|
|
{ |
|
180
|
11 |
|
if (!is_array($list)) return $list; |
|
181
|
|
|
|
|
182
|
11 |
|
$keys = $this->forceShuffle(array_keys($list)); |
|
183
|
11 |
|
$random = []; |
|
184
|
11 |
|
foreach ($keys as $key) { |
|
185
|
11 |
|
$random[$key] = $list[$key]; |
|
186
|
11 |
|
} |
|
187
|
|
|
|
|
188
|
11 |
|
return $random; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param array $list |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
11 |
|
private function forceShuffle($list) |
|
196
|
|
|
{ |
|
197
|
11 |
|
if (!is_array($list) || count($list) < 2) return $list; |
|
198
|
|
|
|
|
199
|
11 |
|
$shuffleList = $list; |
|
200
|
11 |
|
while ($shuffleList == $list) { |
|
201
|
11 |
|
shuffle($shuffleList); |
|
202
|
11 |
|
} |
|
203
|
|
|
|
|
204
|
11 |
|
return $shuffleList; |
|
205
|
|
|
} |
|
206
|
|
|
} |