From edfd095021ee5d89f53da4fd78d7dea7346d7617 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 12 Mar 2017 20:05:19 +0000 Subject: more magic in rendering mesages --- src/Message/Handler.php | 93 ++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 36 deletions(-) (limited to 'src/Message/Handler.php') 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 @@ -18,13 +18,6 @@ class Handler */ protected $important = []; - /** - * The important set of values of injects for this handler - * - * @var mixed[] - */ - protected $importantValues = []; - /** * Construct our handler * @@ -32,18 +25,9 @@ class Handler * @param string[] $important */ public function __construct($important) - { - $this->setupImportant($important); - } - /** - * Set the important list - * - * @author Phil Burton - * @param string[] $important - */ - public function setupImportant($important = []) { $this->important = $important; + $this->initImportantValues(); } /** @@ -52,10 +36,10 @@ class Handler * @author Phil Burton * @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 + * @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 + * @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 * @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 * @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; } } -- cgit v1.2.3