commit | author | age
|
b40e93
|
1 |
<?php |
FÁ |
2 |
|
|
3 |
|
|
4 |
namespace EOG\Models; |
|
5 |
|
|
6 |
|
|
7 |
class TeamList |
|
8 |
{ |
cb9ca2
|
9 |
/** |
FÁ |
10 |
* @var string[] Csapatok listája (előtöltve) |
|
11 |
*/ |
|
12 |
protected $teams = []; |
b40e93
|
13 |
|
7a3972
|
14 |
public function fromJson(string $jsonString): int |
cb9ca2
|
15 |
{ |
FÁ |
16 |
$rawData = json_decode($jsonString, true); |
b40e93
|
17 |
|
cb9ca2
|
18 |
if ($rawData === null) |
FÁ |
19 |
{ |
|
20 |
throw new \InvalidArgumentException("Could not decode JSON"); |
|
21 |
} |
b40e93
|
22 |
|
cb9ca2
|
23 |
if (!is_array($rawData)) |
FÁ |
24 |
{ |
|
25 |
throw new \InvalidArgumentException("Data is not array"); |
|
26 |
} |
b40e93
|
27 |
|
cb9ca2
|
28 |
$this->teams = []; |
b40e93
|
29 |
|
cb9ca2
|
30 |
foreach ($rawData as $key => $team) |
FÁ |
31 |
{ |
7a3972
|
32 |
$players = []; |
FÁ |
33 |
if (is_array($team['players'])) |
cb9ca2
|
34 |
{ |
7a3972
|
35 |
foreach ($team['players'] as $player) |
FÁ |
36 |
{ |
|
37 |
if (!empty($player['inGameName'])) |
|
38 |
{ |
|
39 |
$players[] = $player['inGameName']; |
|
40 |
} |
|
41 |
} |
cb9ca2
|
42 |
} |
7a3972
|
43 |
$this->teams[] = [ |
FÁ |
44 |
'name' => $team['name'], |
|
45 |
'players' => $players |
|
46 |
]; |
cb9ca2
|
47 |
} |
b40e93
|
48 |
|
cb9ca2
|
49 |
return (bool)count($this->teams); |
FÁ |
50 |
} |
b40e93
|
51 |
|
cb9ca2
|
52 |
public function getTeamNames() |
FÁ |
53 |
{ |
|
54 |
return array_column($this->teams, 'name'); |
|
55 |
} |
b40e93
|
56 |
|
cb9ca2
|
57 |
/** |
FÁ |
58 |
* @return string[] |
|
59 |
*/ |
|
60 |
public function getTeams(): array |
|
61 |
{ |
|
62 |
return $this->teams; |
|
63 |
} |
b40e93
|
64 |
|
cb9ca2
|
65 |
/** |
FÁ |
66 |
* @param string[] $teams |
|
67 |
* @return TeamList |
|
68 |
*/ |
|
69 |
public function setTeams(array $teams): TeamList |
|
70 |
{ |
|
71 |
$this->teams = $teams; |
b40e93
|
72 |
|
cb9ca2
|
73 |
return $this; |
FÁ |
74 |
} |
b40e93
|
75 |
|
cb9ca2
|
76 |
public function getJson() |
FÁ |
77 |
{ |
|
78 |
return json_encode($this->teams); |
|
79 |
} |
b40e93
|
80 |
} |