diff --git a/src/EnvReader/GenericConfig.php b/src/EnvReader/GenericConfig.php index 4e7453d..f900c54 100644 --- a/src/EnvReader/GenericConfig.php +++ b/src/EnvReader/GenericConfig.php @@ -94,6 +94,28 @@ abstract class GenericConfig { return $this->properties->get($name); } + /** + * Get a property value as a string + * + * @param string $name The property name + * @param string|null $default The default value if property is not set + * Raise an exception if property is not set AND $default is Null + * + * @return string The property value + * + * @throws UnexpectedValueException If property is not set AND $default is Null + * @throws RangeException If the property can't be cast to a boolean + * + * @noinspection PhpUnused + */ + public function getPropertyAsString (string $name, ?string $default = null): string { + $value = $this->getProperty($name, $default); + if (!is_string($value) || !is_object($value) || method_exists($value, '__ToString')) { + throw new RangeException('The "' . $name . '" property is not a valid string : ' . $value); + } + + return (string)$value; + } /** * Get a property value as a boolean *