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