*/ 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)) { throw new Exception('Could not load messge from config (' . $this->message_name . ')'); } $this->message = $this->config[$this->message_name]; } /** * Load the config */ protected function loadConfig() { $config =Config::getConfig(); if (!$config) { throw new \Exception('Could not load config'); } $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; } 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 setFromArray(array $array) { foreach ($array as $key => $value) { if (in_array($key, $this->injects)) { $this->$key = (string) $value; } } } }