X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=framework%2Fconfig%2Fclass_FrameworkConfiguration.php;h=aa29d572a79db062e1e1e06cc5d2bbd30bf3834c;hp=014c08cddf433fa4db50b1887a74031239bf5f2d;hb=e38af158f73b1364728601cc378bab92f0e23452;hpb=deb4c766b3c6af391dc3beb4a7a450ae2a6249c2 diff --git a/framework/config/class_FrameworkConfiguration.php b/framework/config/class_FrameworkConfiguration.php index 014c08cd..aa29d572 100644 --- a/framework/config/class_FrameworkConfiguration.php +++ b/framework/config/class_FrameworkConfiguration.php @@ -1,5 +1,7 @@ . */ class FrameworkConfiguration implements Registerable { + /** * The framework's main configuration array which will be initialized with * hard-coded configuration data and might be overwritten/extended by @@ -55,8 +61,8 @@ class FrameworkConfiguration implements Registerable { private $callbackInstance = NULL; // Some constants for the configuration system - const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130; - const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131; + const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130; + const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131; const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132; /** @@ -84,7 +90,7 @@ class FrameworkConfiguration implements Registerable { */ public static final function getSelfInstance () { // is the instance there? - if (is_null(self::$configInstance)) { + if (is_null(self::$configInstance)) { // Create a config instance self::$configInstance = new FrameworkConfiguration(); } // END - if @@ -98,8 +104,19 @@ class FrameworkConfiguration implements Registerable { * * @param $str The string with maybe dashes inside * @return $str The converted string with no dashed, but underscores + * @throws NullPointerException If $str is null + * @throws InvalidArgumentException If $str is empty */ private final function convertDashesToUnderscores ($str) { + // Is it null? + if (is_null($str)) { + // Throw NPE + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif (empty($str)) { + // Entry is empty + throw new InvalidArgumentException('str is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } + // Convert them all $str = str_replace('-', '_', $str); @@ -112,8 +129,19 @@ class FrameworkConfiguration implements Registerable { * * @param $zone The time-zone string (e.g. Europe/Berlin) * @return void + * @throws NullPointerException If $zone is NULL + * @throws InvalidArgumentException If $zone is empty */ public final function setDefaultTimezone ($zone) { + // Is it null? + if (is_null($zone)) { + // Throw NPE + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif (empty($zone)) { + // Entry is empty + throw new InvalidArgumentException('zone is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } + // Is PHP version 5.1.0 or higher? Older versions are being ignored if (version_compare(phpversion(), '5.1.0', '>=')) { /* @@ -128,9 +156,20 @@ class FrameworkConfiguration implements Registerable { * Checks whether the given configuration key is set * * @param $configKey The configuration key we shall check - * @return $isset Whether the given configuration key is set + * @return $isset Whether the given configuration key is set + * @throws NullPointerException If $configKey is NULL + * @throws InvalidArgumentException If $configKey is empty */ public function isConfigurationEntrySet ($configKey) { + // Is it null? + if (is_null($configKey)) { + // Throw NPE + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif (empty($configKey)) { + // Entry is empty + throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } + // Is it set? $isset = isset($this->config[$configKey]); @@ -143,21 +182,28 @@ class FrameworkConfiguration implements Registerable { * * @param $configKey The configuration element * @return $configValue The fetched configuration value - * @throws ConfigEntryIsEmptyException If $configKey is empty - * @throws NoConfigEntryException If a configuration element was not found + * @throws NullPointerException If $configKey is NULL + * @throws InvalidArgumentException If $configKey is empty + * @throws NoConfigEntryException If a configuration element was not found */ public function getConfigEntry ($configKey) { + // Is it null? + if (is_null($configKey)) { + // Throw NPE + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif (empty($configKey)) { + // Entry is empty + throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } + // Convert dashes to underscore $configKey = self::convertDashesToUnderscores($configKey); // Is a valid configuration key provided? - if (empty($configKey)) { - // Entry is empty - throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (!$this->isConfigurationEntrySet($configKey)) { + if (!$this->isConfigurationEntrySet($configKey)) { // Entry was not found! throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND); - } + } // END - if // Return the requested value return $this->config[$configKey]; @@ -169,25 +215,26 @@ class FrameworkConfiguration implements Registerable { * @param $configKey The configuration key we want to add/change * @param $configValue The configuration value we want to set * @return void - * @throws ConfigEntryIsEmptyException If $configKey is empty + * @throws NullPointerException If $configKey is NULL + * @throws InvalidArgumentException If $configKey is empty * @throws ConfigValueTypeUnsupportedException If $configValue has an unsupported variable type */ public final function setConfigEntry ($configKey, $configValue) { - // Cast to string - $configKey = self::convertDashesToUnderscores($configKey); - // Is a valid configuration key key provided? if (is_null($configKey)) { // Configuration key is null throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } elseif ((empty($configKey)) || (!is_string($configKey))) { // Entry is empty - throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); } elseif ((is_null($configValue)) || (is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) { // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead. throw new ConfigValueTypeUnsupportedException(array($this, $configKey, $configValue), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED); } + // Cast to string + $configKey = self::convertDashesToUnderscores($configKey); + // Set the configuration value //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL); $this->config[$configKey] = $configValue; @@ -212,9 +259,19 @@ class FrameworkConfiguration implements Registerable { * * @param $configKey Configuration key to unset * @return void + * @throws NullPointerException If $configKey is NULL + * @throws InvalidArgumentException If $configKey is empty * @throws NoConfigEntryException If a configuration element was not found */ public final function unsetConfigEntry ($configKey) { + if (is_null($configKey)) { + // Configuration key is null + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif ((empty($configKey)) || (!is_string($configKey))) { + // Entry is empty + throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } + // Convert dashes to underscore $configKey = self::convertDashesToUnderscores($configKey); @@ -421,10 +478,10 @@ class FrameworkConfiguration implements Registerable { public function equals (FrameworkInterface $objectInstance) { // Now test it $equals = (( - $this->__toString() === $objectInstance->__toString() - ) && ( - $this->hashCode() === $objectInstance->hashCode() - )); + $this->__toString() === $objectInstance->__toString() + ) && ( + $this->hashCode() === $objectInstance->hashCode() + )); // Return the result return $equals;