Fibinger Ádám
2019-10-17 b40e9386fb4253a5866b8e4cdcda0c2023783856
commit | author | age
b40e93 1 <?php
2
3
4 namespace EOG\Models;
5
6
7 class TeamList
8 {
9     /**
10      * @var string[] Csapatok listája (előtöltve)
11      */
12     protected $teams = [];
13
14     private $requiredRawFields = ['name', 'seed', 'status'];
15
16     public function fromJson(string $jsonString)
17     {
18         $rawData = json_decode($jsonString, true);
19
20         if ($rawData === null) return false;
21
22         if (!is_array($rawData)) {
23             return false;
24         }
25
26         $this->teams = [];
27
28         foreach ($rawData as $key => $team) {
29             $rawKeys = array_keys($team);
30             if (count(array_intersect($rawKeys, $this->requiredRawFields)) != 3) {
31                 continue;
32             }
33
34             if ($team['status'] == 'checkedIn') {
35                 $this->teams[] = $team;
36             }
37         }
38
39         return (bool)count($this->teams);
40     }
41
42     public function getTeamNames()
43     {
44         return array_column($this->teams, 'name');
45     }
46
47     /**
48      * @return string[]
49      */
50     public function getTeams(): array
51     {
52         return $this->teams;
53     }
54
55     /**
56      * @param string[] $teams
57      * @return TeamList
58      */
59     public function setTeams(array $teams): TeamList
60     {
61         $this->teams = $teams;
62
63         return $this;
64     }
65
66     public function getJson()
67     {
68         return json_encode($this->teams);
69     }
70 }