*/ class Base { /** * 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->getName(), $this->config)) { throw new Exception('Could not load messge from config (' . $this->getName() . ')'); } $this->message = $this->config[$this->getName()]; } /** * Load the config */ protected function loadConfig() { $config = Config::getConfig(); if (!$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(Handler $handler) { $this->loadConfig(); $this->loadMessage(); $this->setFromHandler($handler); } /** * 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; } return $out; } /** * Get the important injected values * * @author Phil Burton */ public function getImportantValues() { $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 setFromHandler(Handler $handler) { foreach ($handler->returnImportants() as $name) { if (in_array($name, $this->injects)) { $value = $handler->$name; if (isset($value) && $value) { $this->$name = $value; } } } } }