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/Base.php | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'src/Message/Base.php') diff --git a/src/Message/Base.php b/src/Message/Base.php index 2b7b0e4..b6c1c8e 100644 --- a/src/Message/Base.php +++ b/src/Message/Base.php @@ -5,6 +5,7 @@ namespace App\Message; use Mustache_Engine; use App\Config\Config; use App\Message\Handler; +use Exception; /** * Base class for a message @@ -13,13 +14,6 @@ use App\Message\Handler; */ class Base { - /** - * Message name to load - * - * @var string - */ - protected $message_name; - /** * Cache of the loaded config array * @@ -55,11 +49,11 @@ class Base */ protected function loadMessage() { - if (!array_key_exists($this->message_name, $this->config)) { - throw new Exception('Could not load messge from config (' . $this->message_name . ')'); + if (!array_key_exists($this->getName(), $this->config)) { + throw new Exception('Could not load messge from config (' . $this->getName() . ')'); } - $this->message = $this->config[$this->message_name]; + $this->message = $this->config[$this->getName()]; } /** @@ -67,25 +61,39 @@ class Base */ protected function loadConfig() { - $config =Config::getConfig(); + $config = Config::getConfig(); if (!$config) { - throw new \Exception('Could not load config'); + throw new Exception('Could not load config'); } $this->config = $config; } + /** + * Get the name for this message, by default we use the class name + * + * @author Phil Burton + * @return string + */ + public function getName() + { + $split = explode("\\", get_class($this)); + return strtolower($split[count($split) - 1]); + } + /** * Construct, load config and message * * @author Phil Burton */ - public function __construct() + public function __construct(Handler $handler) { $this->loadConfig(); $this->loadMessage(); + + $this->setFromHandler($handler); } /** @@ -135,11 +143,15 @@ class Base * * @author Phil Burton */ - public function setFromArray(array $array) + public function setFromHandler(Handler $handler) { - foreach ($array as $key => $value) { - if (in_array($key, $this->injects)) { - $this->$key = (string) $value; + foreach ($handler->returnImportants() as $name) { + if (in_array($name, $this->injects)) { + $value = $handler->$name; + + if (isset($value) && $value) { + $this->$name = $value; + } } } } -- cgit v1.2.3