From 7a397279805473a76251db4821b7a8c0a76a554c Mon Sep 17 00:00:00 2001 From: Fibinger Ádám <adam.fibinger@wup.hu> Date: Fri, 01 May 2020 16:33:27 +0200 Subject: [PATCH] Csapat import - Battlefy Edition --- EOG/Models/TeamList.php | 110 ++++++++++++++++++++++++++++++------------------------- 1 files changed, 60 insertions(+), 50 deletions(-) diff --git a/EOG/Models/TeamList.php b/EOG/Models/TeamList.php index 7897eba..feb2df2 100644 --- a/EOG/Models/TeamList.php +++ b/EOG/Models/TeamList.php @@ -6,65 +6,75 @@ class TeamList { - /** - * @var string[] Csapatok listája (előtöltve) - */ - protected $teams = []; + /** + * @var string[] Csapatok listája (előtöltve) + */ + protected $teams = []; - private $requiredRawFields = ['name', 'seed', 'status']; + public function fromJson(string $jsonString): int + { + $rawData = json_decode($jsonString, true); - public function fromJson(string $jsonString) - { - $rawData = json_decode($jsonString, true); + if ($rawData === null) + { + throw new \InvalidArgumentException("Could not decode JSON"); + } - if ($rawData === null) return false; + if (!is_array($rawData)) + { + throw new \InvalidArgumentException("Data is not array"); + } - if (!is_array($rawData)) { - return false; - } + $this->teams = []; - $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 + ]; + } - foreach ($rawData as $key => $team) { - $rawKeys = array_keys($team); - if (count(array_intersect($rawKeys, $this->requiredRawFields)) != 3) { - continue; - } + return (bool)count($this->teams); + } - if ($team['status'] == 'checkedIn') { - $this->teams[] = $team; - } - } + public function getTeamNames() + { + return array_column($this->teams, 'name'); + } - return (bool)count($this->teams); - } + /** + * @return string[] + */ + public function getTeams(): array + { + return $this->teams; + } - public function getTeamNames() - { - return array_column($this->teams, 'name'); - } + /** + * @param string[] $teams + * @return TeamList + */ + public function setTeams(array $teams): TeamList + { + $this->teams = $teams; - /** - * @return string[] - */ - public function getTeams(): array - { - return $this->teams; - } + return $this; + } - /** - * @param string[] $teams - * @return TeamList - */ - public function setTeams(array $teams): TeamList - { - $this->teams = $teams; - - return $this; - } - - public function getJson() - { - return json_encode($this->teams); - } + public function getJson() + { + return json_encode($this->teams); + } } \ No newline at end of file -- Gitblit v1.8.0