78 lines
1.6 KiB
PHP
78 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace core\config;
|
|
|
|
/**
|
|
* Configuration
|
|
*/
|
|
class Configuration
|
|
{
|
|
/**
|
|
* Configuration constructor
|
|
*
|
|
* @param \array[\string] $configuration Array containing configuration value string
|
|
*/
|
|
public function __construct(configuration: \array[\string] $configuration)
|
|
{
|
|
$this->configuration = $configuration;
|
|
}
|
|
|
|
/**
|
|
* Get a subpart of this configuration or a value in this configuration
|
|
*
|
|
* @param \string $keys,... Ordered tuple of key, which give the address of the wanted part
|
|
*
|
|
* @return \core\config\Configuration | \string
|
|
*/
|
|
public function get(keys: \string ...$keys) : \core\config\Configuration | \string
|
|
{
|
|
$part = $this->configuration;
|
|
|
|
foreach ($keys as $key)
|
|
{
|
|
if (\key_exists($key, $part))
|
|
{
|
|
\throw new \core\config\ConfigException(
|
|
\core\substitute(_('the key {key} does not exist in this part of the configuration'), array('key' => $key))
|
|
);
|
|
}
|
|
$part = $part[$key];
|
|
}
|
|
if (\is_array($part))
|
|
{
|
|
return new \core\config\Configuration($part);
|
|
}
|
|
if (\is_string($part))
|
|
{
|
|
return $part
|
|
}
|
|
\throw new \core\config\ConfigException(
|
|
\core\substitute(_('the type {type} should not be in a configuration'), array('type' => \gettype($part)))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get a subpart of this configuration or a value in this configuration
|
|
*
|
|
* @param \string $keys,... Ordered tuple of key, which give the address of the wanted part
|
|
*
|
|
* @return \bool
|
|
*/
|
|
public function exist(keys: \string ...$keys) : \bool
|
|
{
|
|
$part = $this->configuration;
|
|
|
|
foreach ($keys as $key)
|
|
{
|
|
if (\key_exists($key, $part))
|
|
{
|
|
return False;
|
|
}
|
|
$part = $part[$key];
|
|
}
|
|
|
|
return True;
|
|
}
|
|
}
|
|
|
|
?>
|