<?php
+
// Own namespace
+
namespace CoreFramework\Configuration;
// Import framework stuff
use CoreFramework\Generic\UnsupportedOperationException;
use CoreFramework\Registry\Registerable;
+// Import SPL stuff
+use \InvalidArgumentException;
+
/**
* A class for the configuration stuff implemented in a singleton design paddern
*
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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
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;
/**
*/
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
*
* @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);
*
* @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', '>=')) {
/*
* 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]);
*
* @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];
* @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;
*
* @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);
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;