Compare commits

..

No commits in common. 'master' and '2.x' have entirely different histories.
master ... 2.x

@ -9,15 +9,15 @@
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "3.x-dev" "dev-master": "2.x-dev"
} }
}, },
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": "^8.1", "php": "^7.4 || ^8.0",
"jrosset/singleton": "^2.0", "jrosset/singleton": "^1.5",
"jrosset/collections": "^3.5" "jrosset/collections": "^2.3 || ^3.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

@ -5,18 +5,13 @@ namespace jrosset\EnvReader;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Exception; use Exception;
use InvalidArgumentException;
use jrosset\Collections\IArrayCast; use jrosset\Collections\IArrayCast;
use jrosset\Collections\InsensitiveCaseKeyCollection; use jrosset\Collections\InsensitiveCaseKeyCollection;
use jrosset\Collections\InsensitiveCaseKeyImmutableCollection; use jrosset\Collections\InsensitiveCaseKeyImmutableCollection;
use jrosset\Singleton\ISingleton; use jrosset\Singleton\ISingleton;
use jrosset\Singleton\TSingleton; use jrosset\Singleton\TSingleton;
use RangeException; use RangeException;
use ReflectionEnum;
use ReflectionEnumBackedCase;
use ReflectionException;
use UnexpectedValueException; use UnexpectedValueException;
use UnitEnum;
/** /**
* A generic configuration class * A generic configuration class
@ -45,8 +40,10 @@ abstract class GenericConfig implements ISingleton {
* Initial properties * Initial properties
* *
* @return string[]|IArrayCast Initial properties * @return string[]|IArrayCast Initial properties
*
* @noinspection PhpReturnDocTypeMismatchInspection
*/ */
protected function initialProperties (): array|IArrayCast { protected function initialProperties () {
return []; return [];
} }
/** /**
@ -79,15 +76,15 @@ abstract class GenericConfig implements ISingleton {
/** /**
* Get a property value * Get a property value
* *
* @param string $name The property name * @param string $name The property name
* @param mixed|null $default The default value if property is not set * @param mixed $default The default value if property is not set
* <br>Raise an exception if property is not set AND $default is Null * Raise an exception if property is not set AND $default is Null
* *
* @return mixed The property value * @return mixed The property value
* *
* @throws UnexpectedValueException If property is not set AND $default is Null * @throws UnexpectedValueException If property is not set AND $default is Null
*/ */
public function getProperty (string $name, mixed $default = null): mixed { public function getProperty (string $name, $default = null) {
if (!$this->hasProperty($name)) { if (!$this->hasProperty($name)) {
if ($default === null) { if ($default === null) {
throw new UnexpectedValueException('The "' . $name . '" property is not set'); throw new UnexpectedValueException('The "' . $name . '" property is not set');
@ -103,7 +100,7 @@ abstract class GenericConfig implements ISingleton {
* *
* @param string $name The property name * @param string $name The property name
* @param string|null $default The default value if property is not set * @param string|null $default The default value if property is not set
* <br>Raise an exception if property is not set AND $default is Null * Raise an exception if property is not set AND $default is Null
* *
* @return string The property value * @return string The property value
* *
@ -125,7 +122,7 @@ abstract class GenericConfig implements ISingleton {
* *
* @param string $name The property name * @param string $name The property name
* @param bool|null $default The default value if property is not set * @param bool|null $default The default value if property is not set
* <br>Raise an exception if property is not set AND $default is Null * Raise an exception if property is not set AND $default is Null
* *
* @return bool The property value * @return bool The property value
* *
@ -135,18 +132,25 @@ abstract class GenericConfig implements ISingleton {
* @noinspection PhpUnused * @noinspection PhpUnused
*/ */
public function getPropertyAsBool (string $name, ?bool $default = null): bool { public function getPropertyAsBool (string $name, ?bool $default = null): bool {
return match ($value = $this->getProperty($name, $default === null ? null : ($default === true ? '1' : '0'))) { switch ($value = $this->getProperty($name, $default === null ? null : ($default === true ? '1' : '0'))) {
'1', 'true' => true, case '1':
'0', 'false' => false, case 'true':
default => throw new RangeException('The "' . $name . '" property is not a valid boolean : ' . $value), return true;
};
case '0':
case 'false':
return false;
default:
throw new RangeException('The "' . $name . '" property is not a valid boolean : ' . $value);
}
} }
/** /**
* Get a property value as an integer * Get a property value as an integer
* *
* @param string $name The property name * @param string $name The property name
* @param int|null $default The default value if property is not set * @param int|null $default The default value if property is not set
* <br>Raise an exception if property is not set AND $default is Null * Raise an exception if property is not set AND $default is Null
* *
* @return int The property value * @return int The property value
* *
@ -167,7 +171,7 @@ abstract class GenericConfig implements ISingleton {
* *
* @param string $name The property name * @param string $name The property name
* @param float|null $default The default value if property is not set * @param float|null $default The default value if property is not set
* <br>Raise an exception if property is not set AND $default is Null * Raise an exception if property is not set AND $default is Null
* *
* @return float The property value * @return float The property value
* *
@ -190,7 +194,7 @@ abstract class GenericConfig implements ISingleton {
* *
* @param string $name The property name * @param string $name The property name
* @param DateTime|null $default The default value if property is not set * @param DateTime|null $default The default value if property is not set
* <br>Raise an exception if property is not set AND $default is Null * Raise an exception if property is not set AND $default is Null
* *
* @return DateTime The property value * @return DateTime The property value
* *
@ -202,7 +206,7 @@ abstract class GenericConfig implements ISingleton {
public function getPropertyAsDateTime (string $name, ?DateTime $default = null): DateTime { public function getPropertyAsDateTime (string $name, ?DateTime $default = null): DateTime {
$value = DateTime::createFromFormat( $value = DateTime::createFromFormat(
DateTimeInterface::RFC3339, DateTimeInterface::RFC3339,
$this->getProperty($name, $default?->format(DateTimeInterface::RFC3339)) $this->getProperty($name, $default === null ? null : $default->format(DateTimeInterface::RFC3339))
); );
$errors = DateTime::getLastErrors(); $errors = DateTime::getLastErrors();
@ -230,60 +234,4 @@ abstract class GenericConfig implements ISingleton {
return $value; return $value;
} }
/**
* Get a property value as an enum
*
* @param string $name The property name
* @param class-string<UnitEnum> $enumClass The enum class name
* @param UnitEnum|null $default The default value if property is not set
* <br>Raise an exception if not <b>$enumClass</b> enum
* <br>Raise an exception if property is not set AND $default is Null
* <br>Raise an exception if property is set but not valid (not an enum)
*
* @return UnitEnum The enum
*
* @throws ReflectionException If <b>$enumClass</b> is not a valid enum class
* @throws InvalidArgumentException If <b>$default</b> is not a <b>$enumClass</b> enum
*/
public function getPropertyAsEnum (string $name, string $enumClass, ?UnitEnum $default = null): UnitEnum {
$enumReflection = new ReflectionEnum($enumClass);
//region Check default value type
if ($default !== null && !$default instanceof $enumClass) {
throw new InvalidArgumentException('The default property type must be an ' . $enumClass);
}
//endregion
//region Return default value (or raise exception) if property is not set
if (!$this->hasProperty($name)) {
if ($default === null) {
throw new UnexpectedValueException('The "' . $name . '" property is not set');
}
return $default;
}
//endregion
//region Try to find the enum through a case name
$propertyValue = $this->getPropertyAsString($name);
if ($enumReflection->hasCase($propertyValue)) {
return $enumReflection->getCase($propertyValue)->getValue();
}
//endregion
//region Try to find enum through the value (if BackedEnum)
if ($enumReflection->isBacked()) {
if (((string)$enumReflection->getBackingType()) === 'int') {
$propertyValue = $this->getPropertyAsInt($name);
}
/** @var ReflectionEnumBackedCase $enumBackedCase */
foreach ($enumReflection->getCases() as $enumBackedCase) {
if ($enumBackedCase->getBackingValue() === $propertyValue) {
return $enumBackedCase->getValue();
}
}
}
//endregion
//region Unable to find valid enum → raise exception
throw new UnexpectedValueException('The "' . $name . '" property is not a valid enum ' . $enumClass);
//endregion
}
} }

@ -43,14 +43,14 @@ trait TMultiLevelProperties {
* Get a property value * Get a property value
* *
* @param string|string[]|IArrayCast $name The property name * @param string|string[]|IArrayCast $name The property name
* @param mixed|null $default The default value if property is not set * @param mixed $default The default value if property is not set
* Raise an exception if property is not set AND $default is Null * Raise an exception if property is not set AND $default is Null
* *
* @return mixed The property value * @return mixed The property value
* *
* @throws UnexpectedValueException If property is not set AND $default is Null * @throws UnexpectedValueException If property is not set AND $default is Null
*/ */
public function getProperty ($name, mixed $default = null): mixed { public function getProperty ($name, $default = null) {
if (!$this->hasProperty($name)) { if (!$this->hasProperty($name)) {
if ($default === null) { if ($default === null) {
throw new UnexpectedValueException('The "' . $name . '" property is not set'); throw new UnexpectedValueException('The "' . $name . '" property is not set');
@ -86,11 +86,11 @@ trait TMultiLevelProperties {
/** /**
* Get array of levels of a property name * Get array of levels of a property name
* *
* @param string|IArrayCast|string[] $propertyName The property name * @param string|string[]|IArrayCast $propertyName The property name
* *
* @return InsensitiveCaseKeyImmutableCollection The property levels * @return InsensitiveCaseKeyImmutableCollection The property levels
*/ */
private function getPropertyLevels (array|IArrayCast|string $propertyName): InsensitiveCaseKeyImmutableCollection { private function getPropertyLevels ($propertyName): InsensitiveCaseKeyImmutableCollection {
if ($propertyName instanceof IArrayCast) { if ($propertyName instanceof IArrayCast) {
$parts = $propertyName->toArray(); $parts = $propertyName->toArray();
} }

@ -15,5 +15,5 @@ interface IXmlStructure {
* *
* @return mixed The extracted value * @return mixed The extracted value
*/ */
public function parseXml (SimpleXMLElement $xmlNode): mixed; public function parseXml (SimpleXMLElement $xmlNode);
} }

@ -21,7 +21,7 @@ trait TAttributeListXmlStructure {
* *
* @return $this * @return $this
*/ */
public function setAttributesStructure (?AttributesXmlStructure $attributesStructure = null): static { public function setAttributesStructure (?AttributesXmlStructure $attributesStructure = null): self {
$this->attributesStructure = $attributesStructure; $this->attributesStructure = $attributesStructure;
return $this; return $this;
} }

@ -3,5 +3,3 @@ WEBSITE_NAME="Foo Bar"
DB_NAME=Bar DB_NAME=Bar
DB_LOGIN=Bar DB_LOGIN=Bar
DB_PASS=***** DB_PASS=*****
TEST=Test2

@ -1,7 +0,0 @@
<?php
enum TestEnum: int {
case Test1 = 1;
case Test2 = 2;
case Test3 = 3;
}

@ -2,7 +2,6 @@
/** @noinspection PhpIllegalPsrClassPathInspection */ /** @noinspection PhpIllegalPsrClassPathInspection */
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/TestEnum.php';
use jrosset\EnvReader\EnvConfig; use jrosset\EnvReader\EnvConfig;
@ -20,4 +19,3 @@ class Env extends EnvConfig {
var_dump(Env::getInstance()->getProperties()); var_dump(Env::getInstance()->getProperties());
var_dump(Env::getInstance()->getProperty('db_host', 'localhost')); var_dump(Env::getInstance()->getProperty('db_host', 'localhost'));
var_dump(Env::getInstance()->getPropertyAsEnum('test', TestEnum::class, TestEnum::Test1));
Loading…
Cancel
Save