Added some documentation (mainly declaring variable types).

This commit is contained in:
Keir 2015-12-08 10:38:44 +00:00
parent 529eab5aef
commit c22a206a0e

View File

@ -9,6 +9,7 @@ if (!isset($_GET["id"]) || empty($_GET["id"])) {
} }
$id = $_GET["id"]; $id = $_GET["id"];
// TODO: prevent escaping to other directories
$filePath = $dataDir . $id . ".json"; $filePath = $dataDir . $id . ".json";
if (file_exists($filePath)) { if (file_exists($filePath)) {
@ -17,6 +18,9 @@ if (file_exists($filePath)) {
echo "Invalid identifier."; echo "Invalid identifier.";
} }
/**
* @param array $dataArray root JSON data array.
*/
function displayMessages($dataArray) function displayMessages($dataArray)
{ {
$snapshot = toSnapshot($dataArray); $snapshot = toSnapshot($dataArray);
@ -27,36 +31,58 @@ function displayMessages($dataArray)
} }
} }
/**
* @param String $filename
* @return array JSON data array.
*/
function toDataArray($filename) function toDataArray($filename)
{ {
return json_decode(file_get_contents($filename), true); return json_decode(file_get_contents($filename), true);
} }
/**
* @param Message $message
* @return string
*/
function getMessageLine($message) function getMessageLine($message)
{ {
return $message->getSender()->getUsername() . " - " . $message->getMessage(); return $message->getSender()->getUsername() . " - " . $message->getMessage();
} }
function toSnapshot($dataArray) /**
* @param array $snapshotData Snapshot data array.
* @return Snapshot
*/
function toSnapshot($snapshotData)
{ {
$identifier = $dataArray["token"]; $identifier = $snapshotData["token"];
$players = getPlayers($dataArray["usernames"]); $players = getPlayers($snapshotData["usernames"]);
$messages = getMessages($players, $dataArray["snapshots"]); $messages = getMessages($players, $snapshotData["snapshots"]);
return new Snapshot($identifier, $messages, $players); return new Snapshot($identifier, $messages, $players);
} }
function getMessages($snapshot, $messagesArray) /**
* @param Player[] $players
* @param array $messagesData Messages data array.
* @return Message[]
*/
function getMessages($players, $messagesData)
{ {
$messages = array(); $messages = array();
for ($i = 0; $i < count($messagesArray); $i++) for ($i = 0; $i < count($messagesData); $i++)
{ {
$messages[$i] = getMessage($snapshot, $messagesArray[$i]); $messages[$i] = getMessage($players, $messagesData[$i]);
} }
return $messages; return $messages;
} }
/**
* @param Player[] $players
* @param array $messageData Message data array.
* @return Message
*/
function getMessage($players, $messageData) function getMessage($players, $messageData)
{ {
$sender = getPlayer($players, $messageData["sender"]); $sender = getPlayer($players, $messageData["sender"]);
@ -67,6 +93,11 @@ function getMessage($players, $messageData)
return new Message($sender, $recipients, $timestamp, $message); return new Message($sender, $recipients, $timestamp, $message);
} }
/**
* @param Player[] $players
* @param String[] $uuidArray the UUIDs of the players to fetch
* @return Player[]
*/
function getPlayersFromUUIDs($players, $uuidArray) function getPlayersFromUUIDs($players, $uuidArray)
{ {
$matchedPlayers = array(); $matchedPlayers = array();
@ -79,7 +110,11 @@ function getPlayersFromUUIDs($players, $uuidArray)
return $matchedPlayers; return $matchedPlayers;
} }
function getPlayers($playersArray) /**
* @param Player[] $playersArray
* @return Player[]
*/
function getPlayers($playersArray) // String UUID as Key
{ {
$players = array(); $players = array();
@ -91,6 +126,11 @@ function getPlayers($playersArray)
return $players; return $players;
} }
/**
* @param Player[] $players
* @param String $uuid
* @return Player
*/
function getPlayer($players, $uuid) function getPlayer($players, $uuid)
{ {
$player = $players[$uuid]; $player = $players[$uuid];
@ -106,10 +146,22 @@ function getPlayer($players, $uuid)
} }
class Snapshot { class Snapshot {
private $identifier;
private $messages;
private $players;
/** @var String */
private $identifier;
/** @var Message[] */
private $messages;
/** @var Player[] */
private $players; // String UUID as Key
/**
* Snapshot constructor.
* @param String $identifier
* @param Message[] $messages
* @param Player[] $players
*/
function Snapshot($identifier, $messages, $players) function Snapshot($identifier, $messages, $players)
{ {
$this->identifier = $identifier; $this->identifier = $identifier;
@ -117,16 +169,25 @@ class Snapshot {
$this->players = $players; $this->players = $players;
} }
/**
* @return String
*/
public function getIdentifier() public function getIdentifier()
{ {
return $this->identifier; return $this->identifier;
} }
/**
* @return Message[]
*/
public function getMessages() public function getMessages()
{ {
return $this->messages; return $this->messages;
} }
/**
* @return Player[]
*/
public function getPlayers() public function getPlayers()
{ {
return $this->players; return $this->players;
@ -135,11 +196,25 @@ class Snapshot {
class Message class Message
{ {
/** @var Player */
private $sender; private $sender;
/** @var Player[] */
private $recipients; private $recipients;
/** @var Int */
private $timestamp; private $timestamp;
/** @var String */
private $message; private $message;
/**
* Message constructor.
* @param Player $sender
* @param Player[] $recipients
* @param Int $timestamp
* @param String $message
*/
function Message($sender, $recipients, $timestamp, $message) function Message($sender, $recipients, $timestamp, $message)
{ {
$this->sender = $sender; $this->sender = $sender;
@ -148,21 +223,33 @@ class Message
$this->message = $message; $this->message = $message;
} }
/**
* @return Player
*/
public function getSender() public function getSender()
{ {
return $this->sender; return $this->sender;
} }
/**
* @return Player[]
*/
public function getRecipients() public function getRecipients()
{ {
return $this->recipients; return $this->recipients;
} }
/**
* @return Int
*/
public function getTimestamp() public function getTimestamp()
{ {
return $this->timestamp; return $this->timestamp;
} }
/**
* @return String
*/
public function getMessage() public function getMessage()
{ {
return $this->message; return $this->message;
@ -171,20 +258,34 @@ class Message
class Player class Player
{ {
/** @var String */
private $uuid; private $uuid;
/** @var String */
private $username; private $username;
/**
* Player constructor.
* @param String $uuid
* @param String $username
*/
function Player($uuid, $username) function Player($uuid, $username)
{ {
$this->uuid = $uuid; $this->uuid = $uuid;
$this->username = $username; $this->username = $username;
} }
/**
* @return String
*/
public function getUUID() public function getUUID()
{ {
return $this->uuid; return $this->uuid;
} }
/**
* @return String
*/
public function getUsername() public function getUsername()
{ {
return $this->username; return $this->username;