Fibinger Ádám
2020-09-10 f2e0b5357a6f019780773cb87cc5c633b27c88f2
commit | author | age
b40e93 1 <?php
2
3
4 namespace EOG\Models;
5
6
7 class TeamList
8 {
cb9ca2 9     /**
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     {
16         $rawData = json_decode($jsonString, true);
b40e93 17
cb9ca2 18         if ($rawData === null)
19         {
20             throw new \InvalidArgumentException("Could not decode JSON");
21         }
b40e93 22
cb9ca2 23         if (!is_array($rawData))
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)
31         {
7a3972 32             $players = [];
33             if (is_array($team['players']))
cb9ca2 34             {
7a3972 35                 foreach ($team['players'] as $player)
36                 {
37                     if (!empty($player['inGameName']))
38                     {
39                         $players[] = $player['inGameName'];
40                     }
41                 }
cb9ca2 42             }
7a3972 43             $this->teams[] = [
44                 'name'    => $team['name'],
45                 'players' => $players
46             ];
cb9ca2 47         }
b40e93 48
cb9ca2 49         return (bool)count($this->teams);
50     }
b40e93 51
cb9ca2 52     public function getTeamNames()
53     {
54         return array_column($this->teams, 'name');
55     }
b40e93 56
cb9ca2 57     /**
58      * @return string[]
59      */
60     public function getTeams(): array
61     {
62         return $this->teams;
63     }
b40e93 64
cb9ca2 65     /**
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;
74     }
b40e93 75
cb9ca2 76     public function getJson()
77     {
78         return json_encode($this->teams);
79     }
b40e93 80 }