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/Message/Base.php | 83 ++++++++++++++++++++++++++++++++++++++++++------ src/Message/Handler.php | 72 ++++++++++++++++++++++++++++++++++++----- src/Message/Overview.php | 50 +++++++++++++++++++++++++++-- 3 files changed, 185 insertions(+), 20 deletions(-) (limited to 'src/Message') 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