getMessages() as $message) { echo getMessageLine($message); echo "
"; } } function toDataArray($filename) { return json_decode(file_get_contents($filename), true); } function getMessageLine($message) { return $message->getSender()->getUsername() . " - " . $message->getMessage(); } function toSnapshot($dataArray) { $identifier = $dataArray["token"]; $players = getPlayers($dataArray["usernames"]); $messages = getMessages($players, $dataArray["snapshots"]); return new Snapshot($identifier, $messages, $players); } function getMessages($snapshot, $messagesArray) { $messages = array(); for ($i = 0; $i < count($messagesArray); $i++) { $messages[$i] = getMessage($snapshot, $messagesArray[$i]); } return $messages; } function getMessage($players, $messageData) { $sender = getPlayer($players, $messageData["sender"]); $recipients = getPlayersFromUUIDs($players, $messageData["recipients"]); $timestamp = $messageData["time"]; $message = $messageData["message"]; return new Message($sender, $recipients, $timestamp, $message); } function getPlayersFromUUIDs($players, $uuidArray) { $matchedPlayers = array(); for ($i = 0; $i < count($uuidArray); $i++) { $matchedPlayers[$i] = getPlayer($players, $uuidArray[$i]); } return $matchedPlayers; } function getPlayers($playersArray) { $players = array(); foreach ($playersArray as $uuid => $username) { $players[$uuid] = new Player($uuid, $username); } return $players; } function getPlayer($players, $uuid) { $player = $players[$uuid]; if ($player != null) { return $player; } else { throw new RuntimeException("Player for UUID not found."); } } class Snapshot { private $identifier; private $messages; private $players; function Snapshot($identifier, $messages, $players) { $this->identifier = $identifier; $this->messages = $messages; $this->players = $players; } public function getIdentifier() { return $this->identifier; } public function getMessages() { return $this->messages; } public function getPlayers() { return $this->players; } } class Message { private $sender; private $recipients; private $timestamp; private $message; function Message($sender, $recipients, $timestamp, $message) { $this->sender = $sender; $this->recipients = $recipients; $this->timestamp = $timestamp; $this->message = $message; } public function getSender() { return $this->sender; } public function getRecipients() { return $this->recipients; } public function getTimestamp() { return $this->timestamp; } public function getMessage() { return $this->message; } } class Player { private $uuid; private $username; function Player($uuid, $username) { $this->uuid = $uuid; $this->username = $username; } public function getUUID() { return $this->uuid; } public function getUsername() { return $this->username; } }