From cb9ca2b735b9fedc479799e88ddb731251b35b08 Mon Sep 17 00:00:00 2001 From: Fibinger Ádám <adam.fibinger@wup.hu> Date: Sun, 10 Nov 2019 13:27:28 +0100 Subject: [PATCH] ESL Api alapján JSON-ból felhúzni a csapatokat gyors kiegészítésre --- EOG/Models/TeamList.php | 103 +++++++++++++++++++++++++++------------------------ 1 files changed, 55 insertions(+), 48 deletions(-) diff --git a/EOG/Models/TeamList.php b/EOG/Models/TeamList.php index 7897eba..f61a8de 100644 --- a/EOG/Models/TeamList.php +++ b/EOG/Models/TeamList.php @@ -6,65 +6,72 @@ 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']; + private $requiredRawFields = ['name', 'seed', 'status']; - public function fromJson(string $jsonString) - { - $rawData = json_decode($jsonString, true); + public function fromJson(string $jsonString) : int + { + $rawData = json_decode($jsonString, true); - if ($rawData === null) return false; + if ($rawData === null) + { + throw new \InvalidArgumentException("Could not decode JSON"); + } - if (!is_array($rawData)) { - return false; - } + if (!is_array($rawData)) + { + throw new \InvalidArgumentException("Data is not array"); + } - $this->teams = []; + $this->teams = []; - foreach ($rawData as $key => $team) { - $rawKeys = array_keys($team); - if (count(array_intersect($rawKeys, $this->requiredRawFields)) != 3) { - continue; - } + foreach ($rawData as $key => $team) + { + $rawKeys = array_keys($team); + if (count(array_intersect($rawKeys, $this->requiredRawFields)) != 3) + { + continue; + } - if ($team['status'] == 'checkedIn') { - $this->teams[] = $team; - } - } + if ($team['status'] == 'checkedIn') + { + $this->teams[] = $team; + } + } - return (bool)count($this->teams); - } + return (bool)count($this->teams); + } - public function getTeamNames() - { - return array_column($this->teams, 'name'); - } + public function getTeamNames() + { + return array_column($this->teams, 'name'); + } - /** - * @return string[] - */ - public function getTeams(): array - { - return $this->teams; - } + /** + * @return string[] + */ + public function getTeams(): array + { + return $this->teams; + } - /** - * @param string[] $teams - * @return TeamList - */ - public function setTeams(array $teams): TeamList - { - $this->teams = $teams; + /** + * @param string[] $teams + * @return TeamList + */ + public function setTeams(array $teams): TeamList + { + $this->teams = $teams; - return $this; - } + 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