diff --git a/src/Config.php b/src/Config.php index 101c80b..d74376a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -71,6 +71,27 @@ public function get(string $key, $default = null) return $default; } + /** + * Get a given key, throwing an exception if it is not found + * + * @param string $key + * @param string $message The exception message to throw + * @return mixed + * @throws RuntimeException If the key is not found + * @author Ronan Chilvers + */ + public function getOrThrow(string $key, string $message = null) + { + $value = $this->offsetGet($key); + if (!is_null($value)) { + return $value; + } + if (is_null($message)) { + $message = "Key {$key} not found"; + } + throw new RuntimeException($message); + } + /** * Get all the keys for this config object *