Fibinger Ádám
2019-11-27 c5946f44341c29594803c5b2ca64dcaf335fbca3
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
cb9ca2 14     private $requiredRawFields = ['name', 'seed', 'status'];
b40e93 15
cb9ca2 16     public function fromJson(string $jsonString) : int
17     {
18         $rawData = json_decode($jsonString, true);
b40e93 19
cb9ca2 20         if ($rawData === null)
21         {
22             throw new \InvalidArgumentException("Could not decode JSON");
23         }
b40e93 24
cb9ca2 25         if (!is_array($rawData))
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)
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')
41             {
42                 $this->teams[] = $team;
43             }
44         }
b40e93 45
cb9ca2 46         return (bool)count($this->teams);
47     }
b40e93 48
cb9ca2 49     public function getTeamNames()
50     {
51         return array_column($this->teams, 'name');
52     }
b40e93 53
cb9ca2 54     /**
55      * @return string[]
56      */
57     public function getTeams(): array
58     {
59         return $this->teams;
60     }
b40e93 61
cb9ca2 62     /**
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;
71     }
b40e93 72
cb9ca2 73     public function getJson()
74     {
75         return json_encode($this->teams);
76     }
b40e93 77 }