diff options
Diffstat (limited to 'src/Message/Handler.php')
-rw-r--r-- | src/Message/Handler.php | 93 |
1 files changed, 57 insertions, 36 deletions
diff --git a/src/Message/Handler.php b/src/Message/Handler.php index 825ceda..8f216b2 100644 --- a/src/Message/Handler.php +++ b/src/Message/Handler.php @@ -2,7 +2,7 @@ namespace App\Message; -use App\Message\Overview; +use Exception; /** * Handles messages and context @@ -19,13 +19,6 @@ class Handler protected $important = []; /** - * The important set of values of injects for this handler - * - * @var mixed[] - */ - protected $importantValues = []; - - /** * Construct our handler * * @author Phil Burton <phil@pgburton.com> @@ -33,17 +26,8 @@ class Handler */ public function __construct($important) { - $this->setupImportant($important); - } - /** - * Set the important list - * - * @author Phil Burton <phil@pgburton.com> - * @param string[] $important - */ - public function setupImportant($important = []) - { $this->important = $important; + $this->initImportantValues(); } /** @@ -52,10 +36,10 @@ class Handler * @author Phil Burton <phil@pgburton.com> * @param mixed[] $array */ - protected function setImportantValues($array) + protected function initImportantValues() { - foreach ($array as $name => $value) { - $this->addImportantValue($name, $value); + foreach ($this->important as $name) { + $this->addImportantValue($name, false); } } @@ -69,7 +53,7 @@ class Handler protected function addImportantValue($name, $value) { if (in_array($name, $this->important)) { - $this->importantValues[$name] = $value; + $this->$name = $value; } return $name; @@ -84,16 +68,52 @@ class Handler */ public function returnImportant($name) { - if (!array_key_exists($name, $this->importantValues)) { - throw new \Exception('The var ' . $name . ' is not set in this context'); + if (!isset($this->$name)) { + throw new Exception('The var ' . $name . ' is not set in this context'); } - return $this->importantValues[$name]; + return $this->$name; + } + + protected function addImportantFromArray($array) + { + foreach ($array as $key => $value) { + if (!isset($this->$key)) { + throw new Exception('The var ' . $value . ' is not set in this context'); + } + + $this->$key = $value; + } + } + + /** + * Return all importnat vars and values in array + * + * @author Phil Burton <phil@pgburton.com> + * @return string[] + */ + public function returnImportants() + { + return $this->important; + } + + /** + * Magic __get override to be able to get important values using ->myValue + * which then runs $this->returnImportant('myValue') + * + * @author Phil Burton <phil@pgburton.com> + * @param string $name + * @param mixed[] $arguments + * @return string + */ + public function __get($name) + { + return $this->returnImporant(substr($name, 3)); } /** - * Magic __call override to be able to get improtant values using getMyImportant() - * which then runs $this->returnImportant('myImportant') + * Allow for magic rendering + * Call renderFooBar to render the FooBar message class * * @author Phil Burton <phil@pgburton.com> * @param string $name @@ -102,8 +122,8 @@ class Handler */ public function __call($name, $arguments) { - if (strpos($name, 'get') === 0) { - $this->returnImporant(substr($name, 3)); + if (strpos($name, 'render') === 0) { + return $this->doRender(substr($name, 6)); } } @@ -113,16 +133,17 @@ class Handler * @author Phil Burton <phil@pgburton.com> * @return string */ - public function renderOpeningMessage() + public function doRender($name) { - $overview = new Overview(); - - $overview->setFromArray($this->importantValues); - - $message = $overview->renderMessage(); + $name = "App\\Message\\" . $name; + if (!class_exists($name)) { + throw new Exception('Could not find message class `' . $name . '`'); + } - $this->setImportantValues($overview->getImportantValues()); + $messageObj = new $name($this); + $message = $messageObj->renderMessage(); + $this->addImportantFromArray($messageObj->getImportantValues()); return $message; } } |