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 |
|
cb9ca2
|
14 |
private $requiredRawFields = ['name', 'seed', 'status']; |
b40e93
|
15 |
|
cb9ca2
|
16 |
public function fromJson(string $jsonString) : int |
FÁ |
17 |
{ |
|
18 |
$rawData = json_decode($jsonString, true); |
b40e93
|
19 |
|
cb9ca2
|
20 |
if ($rawData === null) |
FÁ |
21 |
{ |
|
22 |
throw new \InvalidArgumentException("Could not decode JSON"); |
|
23 |
} |
b40e93
|
24 |
|
cb9ca2
|
25 |
if (!is_array($rawData)) |
FÁ |
26 |
{ |
|
27 |
throw new \InvalidArgumentException("Data is not array"); |
|
28 |
} |
b40e93
|
29 |
|
cb9ca2
|
30 |
$this->teams = []; |
b40e93
|
31 |
|
cb9ca2
|
32 |
foreach ($rawData as $key => $team) |
FÁ |
33 |
{ |
|
34 |
$rawKeys = array_keys($team); |
|
35 |
if (count(array_intersect($rawKeys, $this->requiredRawFields)) != 3) |
|
36 |
{ |
|
37 |
continue; |
|
38 |
} |
b40e93
|
39 |
|
cb9ca2
|
40 |
if ($team['status'] == 'checkedIn') |
FÁ |
41 |
{ |
|
42 |
$this->teams[] = $team; |
|
43 |
} |
|
44 |
} |
b40e93
|
45 |
|
cb9ca2
|
46 |
return (bool)count($this->teams); |
FÁ |
47 |
} |
b40e93
|
48 |
|
cb9ca2
|
49 |
public function getTeamNames() |
FÁ |
50 |
{ |
|
51 |
return array_column($this->teams, 'name'); |
|
52 |
} |
b40e93
|
53 |
|
cb9ca2
|
54 |
/** |
FÁ |
55 |
* @return string[] |
|
56 |
*/ |
|
57 |
public function getTeams(): array |
|
58 |
{ |
|
59 |
return $this->teams; |
|
60 |
} |
b40e93
|
61 |
|
cb9ca2
|
62 |
/** |
FÁ |
63 |
* @param string[] $teams |
|
64 |
* @return TeamList |
|
65 |
*/ |
|
66 |
public function setTeams(array $teams): TeamList |
|
67 |
{ |
|
68 |
$this->teams = $teams; |
b40e93
|
69 |
|
cb9ca2
|
70 |
return $this; |
FÁ |
71 |
} |
b40e93
|
72 |
|
cb9ca2
|
73 |
public function getJson() |
FÁ |
74 |
{ |
|
75 |
return json_encode($this->teams); |
|
76 |
} |
b40e93
|
77 |
} |