parseInput($input) as [$team1, $team2, $result]) { $this->initTeam($teams, $team1); $this->initTeam($teams, $team2); $this->updateMatchesPlayed($teams, $team1, $team2); $this->updateMatchResult($teams, $team1, $team2, $result); } $this->sortTeams($teams); return $this->generateTable($teams); } private function parseInput(string $input): array { $rows = explode("\n", $input); $matches = []; foreach ($rows as $row) { if ($row === "") { continue; } $matches[] = explode(";", $row); } return $matches; } private function initTeam(array &$teams, string $team): void { if (!isset($teams[$team])) { $teams[$team] = [ "MP" => 0, "W" => 0, "D" => 0, "L" => 0, "P" => 0, ]; } } private function updateMatchesPlayed( array &$teams, string $team1, string $team2, ): void { $teams[$team1]["MP"]++; $teams[$team2]["MP"]++; } private function updateMatchResult( array &$teams, string $team1, string $team2, string $result, ): void { switch ($result) { case "win": $teams[$team1]["W"]++; $teams[$team2]["L"]++; $teams[$team1]["P"] += 3; break; case "loss": $teams[$team2]["W"]++; $teams[$team1]["L"]++; $teams[$team2]["P"] += 3; break; case "draw": $teams[$team1]["D"]++; $teams[$team2]["D"]++; $teams[$team1]["P"]++; $teams[$team2]["P"]++; break; } } private function sortTeams(array &$teams): void { uksort($teams, function ($teamA, $teamB) use ($teams) { return $teams[$teamB]["P"] <=> $teams[$teamA]["P"] ?: strcmp($teamA, $teamB); }); } private function generateTable(array $teams): string { $output = self::HEADER; foreach ($teams as $team => $stats) { $output .= sprintf( "\n%-31s| %2d | %2d | %2d | %2d | %2d", $team, $stats["MP"], $stats["W"], $stats["D"], $stats["L"], $stats["P"], ); } return $output; } }