Skip to content

Commit

Permalink
Add getOrThrow to standardise exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanchilvers committed Sep 11, 2019
1 parent 50988bb commit 106191d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ronan@d3r.com>
*/
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
*
Expand Down

0 comments on commit 106191d

Please sign in to comment.