Add method to get property as string

2.x 2.1.0
Julien Rosset 3 years ago
parent 5acd182215
commit 55e1bc70d7

@ -94,6 +94,28 @@ abstract class GenericConfig {
return $this->properties->get($name); 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 * Get a property value as a boolean
* *

Loading…
Cancel
Save