hanyuu/src/Config/ScopedConfig.php

38 lines
1.0 KiB
PHP

<?php
namespace Hanyuu\Config;
use InvalidArgumentException;
class ScopedConfig implements IConfig {
private IConfig $config;
private string $prefix;
private const SCOPE_CHAR = ':';
public function __construct(IConfig $config, string $prefix) {
if($prefix === '')
throw new InvalidArgumentException('$prefix may not be empty.');
if(!str_ends_with($prefix, self::SCOPE_CHAR))
$prefix .= self::SCOPE_CHAR;
$this->config = $config;
$this->prefix = $prefix;
}
private function getName(string $name): string {
return $this->prefix . $name;
}
public function scopeTo(string $prefix): IConfig {
return $this->config->scopeTo($this->getName($prefix));
}
public function getValue(string $name, string $type = IConfig::T_ANY, $default = null): mixed {
return $this->config->getValue($this->getName($name), $type, $default);
}
public function hasValue(string $name): bool {
return $this->config->hasValue($this->getName($name));
}
}