From 6f4cd9178ba55ffb3fd80816cf9f4cda39ee0cad Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 12 Mar 2017 17:46:18 +0000 Subject: Doc block and PSR --- src/Config/Config.php | 18 ++++++++++- src/Core/Main.php | 25 ++++++++++++--- src/Game.php | 19 +++++++++-- src/Message/Base.php | 83 ++++++++++++++++++++++++++++++++++++++++++------ src/Message/Handler.php | 72 ++++++++++++++++++++++++++++++++++++----- src/Message/Overview.php | 50 +++++++++++++++++++++++++++-- 6 files changed, 238 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/Config/Config.php b/src/Config/Config.php index 10236f0..710c9e0 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -2,10 +2,26 @@ namespace App\Config; +/** + * Config class to handle loading and returning config stuff + * + * @author Phil Burton + */ class Config { + /** + * Cached config + * + * @var mixed[] + */ protected static $config; + /** + * Return the cofnig + * + * @author Phil Burton + * @return mixed[] + */ public static function getconfig() { if (!static::$config) { @@ -14,4 +30,4 @@ class Config return static::$config; } -} \ No newline at end of file +} diff --git a/src/Core/Main.php b/src/Core/Main.php index 594b15c..7aa643e 100644 --- a/src/Core/Main.php +++ b/src/Core/Main.php @@ -5,11 +5,15 @@ namespace App\Core; use App\Game; /** -* Core Main class, deals with all set-up -*/ + * Not really sure if we need this + */ class Main { - + /** + * Construct + * + * @author Phil Burton + */ public function __construct() { $this->init(); @@ -17,13 +21,24 @@ class Main $this->run(); } + /** + * Initalise + * + * @author Phil Burton + * @return bool + */ protected function init() { return true; } + /** + * Run the Game + * + * @author Phil Burton + */ protected function run() { - $game = new Game(); + $game = new Game(); } -} \ No newline at end of file +} diff --git a/src/Game.php b/src/Game.php index c2141ce..1bedba2 100644 --- a/src/Game.php +++ b/src/Game.php @@ -6,16 +6,26 @@ use App\Message\Handler; /** * A Game class that handles game execution + * + * @author Phil Burton */ class Game { - protected $messages; - + /** + * Constuct a new Game + * + * @author Phil Burton + */ public function __construct() { $this->init(); } + /** + * Initalise the Game + * + * @author Phil Burton + */ protected function init() { // Do any pre set-up @@ -23,6 +33,10 @@ class Game $this->sendOpeningMessage(); } + /** + * Send opening message + * @author Phil Burton + */ public function sendOpeningMessage() { $message = $this->handler->renderOpeningMessage(); @@ -33,5 +47,4 @@ class Game var_dump($message, $important, $message2); } - } diff --git a/src/Message/Base.php b/src/Message/Base.php index 4565de9..2b7b0e4 100644 --- a/src/Message/Base.php +++ b/src/Message/Base.php @@ -8,15 +8,51 @@ use App\Message\Handler; /** * Base class for a message +* +* @author Phil Burton */ class Base { + /** + * Message name to load + * + * @var string + */ protected $message_name; + + /** + * Cache of the loaded config array + * + * @var string + */ protected $config; + + /** + * The raw message string loaded from config + * + * @var string + */ protected $message; + + /** + * The list of vars we need to inject + * + * @var array + */ protected $injects = []; + + /** + * The array of important vars + * + * @var string + */ protected $important; + /** + * Load the message from the config + * + * @author Phil Burton + */ protected function loadMessage() { if (!array_key_exists($this->message_name, $this->config)) { @@ -26,6 +62,9 @@ class Base $this->message = $this->config[$this->message_name]; } + /** + * Load the config + */ protected function loadConfig() { $config =Config::getConfig(); @@ -37,39 +76,65 @@ class Base $this->config = $config; } + /** + * Construct, load config and message + * + * @author Phil Burton + */ public function __construct() { $this->loadConfig(); - + $this->loadMessage(); } + /** + * Render the message, injecting any values into the message using mustache + * + * @author Phil Burton + */ public function renderMessage() { $m = new Mustache_Engine; return $m->render($this->message, $this->getInjectValues()); } + + /** + * Return the injected values + * + * @author Phil Burton + */ public function getInjectValues() { $out = []; - foreach ($this->injects as $inject) { - $out[$inject] = $this->$inject; - } + foreach ($this->injects as $inject) { + $out[$inject] = $this->$inject; + } return $out; } + /** + * Get the important injected values + * + * @author Phil Burton + */ public function getImportantValues() { - $out = []; - foreach ($this->important as $important) { - $out[$important] = $this->$important; - } + $out = []; + foreach ($this->important as $important) { + $out[$important] = $this->$important; + } return $out; } + /** + * Set-up this message's inject values based on an input array + * + * @author Phil Burton + */ public function setFromArray(array $array) { foreach ($array as $key => $value) { @@ -78,4 +143,4 @@ class Base } } } -} \ No newline at end of file +} diff --git a/src/Message/Handler.php b/src/Message/Handler.php index 89752c5..825ceda 100644 --- a/src/Message/Handler.php +++ b/src/Message/Handler.php @@ -6,25 +6,52 @@ use App\Message\Overview; /** * Handles messages and context +* +* @author Phil Burton */ class Handler { + /** + * The important set of injects for this handler + * + * @var string[] + */ protected $important = []; + /** + * The important set of values of injects for this handler + * + * @var mixed[] + */ protected $importantValues = []; + /** + * Construct our handler + * + * @author Phil Burton + * @param string[] $important + */ public function __construct($important) { $this->setupImportant($important); } /** - * Set's up important attributes on this class + * Set the important list + * + * @author Phil Burton + * @param string[] $important */ public function setupImportant($important = []) { $this->important = $important; } + /** + * Set important values + * + * @author Phil Burton + * @param mixed[] $array + */ protected function setImportantValues($array) { foreach ($array as $name => $value) { @@ -32,6 +59,13 @@ class Handler } } + /** + * Add an important value to our array + * + * @author Phil Burton + * @param string $name + * @param mixed[] $value + */ protected function addImportantValue($name, $value) { if (in_array($name, $this->important)) { @@ -41,15 +75,31 @@ class Handler return $name; } + /** + * Get a single important value + * + * @author Phil Burton + * @param string $name + * @return mixed + */ public function returnImportant($name) { if (!array_key_exists($name, $this->importantValues)) { throw new \Exception('The var ' . $name . ' is not set in this context'); } - + return $this->importantValues[$name]; } + /** + * Magic __call override to be able to get improtant values using getMyImportant() + * which then runs $this->returnImportant('myImportant') + * + * @author Phil Burton + * @param string $name + * @param mixed[] $arguments + * @return string + */ public function __call($name, $arguments) { if (strpos($name, 'get') === 0) { @@ -57,16 +107,22 @@ class Handler } } + /** + * Render the opening message + * + * @author Phil Burton + * @return string + */ public function renderOpeningMessage() { - $overview = new Overview(); + $overview = new Overview(); - $overview->setFromArray($this->importantValues); + $overview->setFromArray($this->importantValues); - $message = $overview->renderMessage(); + $message = $overview->renderMessage(); - $this->setImportantValues($overview->getImportantValues()); + $this->setImportantValues($overview->getImportantValues()); - return $message; + return $message; } -} \ No newline at end of file +} diff --git a/src/Message/Overview.php b/src/Message/Overview.php index c328acc..0494c16 100644 --- a/src/Message/Overview.php +++ b/src/Message/Overview.php @@ -4,22 +4,66 @@ namespace App\Message; use App\Message\Base; +/** + * Overview message + * + * @author Phil Burton + */ class Overview extends Base { - protected $message_name = 'opening'; + /** + * Message name to load + * + * @var string + */ + protected $message_name; + + /** + * The list of vars we need to inject + * + * @var array + */ protected $injects = ['inject', 'second']; + + /** + * The array of important vars + * + * @var string + */ protected $important = ['inject']; + + /** + * Inject + * @var string + */ public $inject; + + /** + * Second inject + * + * @var string + */ public $second = "second"; + /** + * Construct to set any inject vars from functions if required + * + * @author Phil Burton + */ public function __construct() { $this->inject = $this->getInject(); parent::__construct(); } + /** + * Get our inject value + * + * @author Phil Burton + * @return string + */ public function getInject() { - return time(); + return (string) time(); } -} \ No newline at end of file +} -- cgit v1.2.3