*/ class Handler { /** * The important set of injects for this handler * * @var string[] */ protected $important = []; /** * Construct our handler * * @author Phil Burton * @param string[] $important */ public function __construct($important) { $this->important = $important; $this->initImportantValues(); } /** * Set important values * * @author Phil Burton * @param mixed[] $array */ protected function initImportantValues() { foreach ($this->important as $name) { $this->addImportantValue($name, false); } } /** * Add an important value to our array * * @author Phil Burton * @param string $name * @param mixed[] $value */ protected function addImportantValue($name, $value) { if (in_array($name, $this->important)) { $this->$name = $value; } return $name; } /** * Get a single important value * * @author Phil Burton * @param string $name * @return mixed */ public function returnImportant($name) { if (!isset($this->$name)) { throw new Exception('The var ' . $name . ' is not set in this context'); } 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)); } /** * Allow for magic rendering * Call renderFooBar to render the FooBar message class * * @author Phil Burton * @param string $name * @param mixed[] $arguments * @return string */ public function __call($name, $arguments) { if (strpos($name, 'render') === 0) { return $this->doRender(substr($name, 6)); } } /** * Render the opening message * * @author Phil Burton * @return string */ public function doRender($name) { $name = "App\\Message\\" . $name; if (!class_exists($name)) { throw new Exception('Could not find message class `' . $name . '`'); } $messageObj = new $name($this); $message = $messageObj->renderMessage(); $this->addImportantFromArray($messageObj->getImportantValues()); return $message; } }