|
|
|
@ -5,9 +5,9 @@ namespace jrosset\EnvReader;
|
|
|
|
|
use DateTime;
|
|
|
|
|
use DateTimeInterface;
|
|
|
|
|
use Exception;
|
|
|
|
|
use jrosset\ArrayClasses\IArrayCast;
|
|
|
|
|
use jrosset\ArrayClasses\ImmutableInsensitiveCaseArrayClass;
|
|
|
|
|
use jrosset\ArrayClasses\InsensitiveCaseArrayClass;
|
|
|
|
|
use jrosset\Collections\IArrayCast;
|
|
|
|
|
use jrosset\Collections\InsensitiveCaseKeyCollection;
|
|
|
|
|
use jrosset\Collections\InsensitiveCaseKeyImmutableCollection;
|
|
|
|
|
use jrosset\Singleton\TSingleton;
|
|
|
|
|
use RangeException;
|
|
|
|
|
use UnexpectedValueException;
|
|
|
|
@ -21,9 +21,9 @@ abstract class GenericConfig {
|
|
|
|
|
use TSingleton;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var InsensitiveCaseArrayClass Current properties
|
|
|
|
|
* @var InsensitiveCaseKeyCollection Current properties
|
|
|
|
|
*/
|
|
|
|
|
protected InsensitiveCaseArrayClass $properties;
|
|
|
|
|
protected InsensitiveCaseKeyCollection $properties;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize initial properties then read ENV file
|
|
|
|
@ -31,7 +31,7 @@ abstract class GenericConfig {
|
|
|
|
|
* @throws Exception If ENV can't be read
|
|
|
|
|
*/
|
|
|
|
|
protected function __construct () {
|
|
|
|
|
$this->properties = new InsensitiveCaseArrayClass($this->initialProperties());
|
|
|
|
|
$this->properties = new InsensitiveCaseKeyCollection($this->initialProperties());
|
|
|
|
|
$this->readConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -57,10 +57,10 @@ abstract class GenericConfig {
|
|
|
|
|
/**
|
|
|
|
|
* Get all properties
|
|
|
|
|
*
|
|
|
|
|
* @return ImmutableInsensitiveCaseArrayClass Get all properties (properties' name are in upper case)
|
|
|
|
|
* @return InsensitiveCaseKeyImmutableCollection Get all properties (properties' name are in upper case)
|
|
|
|
|
*/
|
|
|
|
|
public function getProperties (): ImmutableInsensitiveCaseArrayClass {
|
|
|
|
|
return $this->properties;
|
|
|
|
|
public function getProperties (): InsensitiveCaseKeyImmutableCollection {
|
|
|
|
|
return new InsensitiveCaseKeyImmutableCollection($this->properties);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Check if a property id defined
|
|
|
|
@ -70,7 +70,7 @@ abstract class GenericConfig {
|
|
|
|
|
* @return bool Is the property defined ?
|
|
|
|
|
*/
|
|
|
|
|
public function hasProperty (string $name): bool {
|
|
|
|
|
return $this->properties->has($name);
|
|
|
|
|
return $this->properties->exists($name);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Get a property value
|
|
|
|
@ -160,7 +160,7 @@ abstract class GenericConfig {
|
|
|
|
|
*/
|
|
|
|
|
public function getPropertyAsInt (string $name, ?int $default = null): int {
|
|
|
|
|
$value = $this->getProperty($name, $default === null ? null : (string)$default);
|
|
|
|
|
if (preg_match('#^\s*(?<value>[0-9]+)\s*$#', $value, $match) !== 1) {
|
|
|
|
|
if (preg_match('#^\s*(?<value>\d+)\s*$#', $value, $match) !== 1) {
|
|
|
|
|
throw new RangeException('The "' . $name . '" property is not a valid integer : ' . $value);
|
|
|
|
|
}
|
|
|
|
|
return (int)$match['value'];
|
|
|
|
@ -181,7 +181,7 @@ abstract class GenericConfig {
|
|
|
|
|
*/
|
|
|
|
|
public function getPropertyAsReal (string $name, ?float $default = null): float {
|
|
|
|
|
$value = $this->getProperty($name, $default === null ? null : (string)$default);
|
|
|
|
|
if (preg_match('#^\s*(?<value>[0-9]+(?:\.[0-9]+)?)\s*$#', $value, $match) !== 1) {
|
|
|
|
|
if (preg_match('#^\s*(?<value>\d+(?:\.\d+)?)\s*$#', $value, $match) !== 1) {
|
|
|
|
|
throw new RangeException('The "' . $name . '" property is not a valid float value : ' . $value);
|
|
|
|
|
}
|
|
|
|
|
return (float)$match['value'];
|
|
|
|
@ -228,7 +228,7 @@ abstract class GenericConfig {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($value === false) {
|
|
|
|
|
throw new RangeException('The "' . $name . '" property is not a valid date and time : ' . $value);
|
|
|
|
|
throw new RangeException('The "' . $name . '" property is not a valid date and time');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|