nur-sery/nur_src/config/ArrayConfig.php

85 lines
2.1 KiB
PHP
Raw Normal View History

2023-12-03 22:10:18 +04:00
<?php
namespace nur\config;
use nur\A;
use nur\b\ValueException;
/**
* Class ArrayConfig: une configuration stockée dans un tableau
*
* Lors de la création de l'objet, il faut préciser le cas échéant dans quels
* profils sont valides cette configuration.
*/
class ArrayConfig extends DynConfig {
protected function PROFILES(): array {
return static::PROFILES;
} const PROFILES = [];
protected function SCHEMA(): array {
return static::SCHEMA;
} const SCHEMA = [];
protected function DBS(): array {
return static::DBS;
} const DBS = [];
protected function MSGS(): array {
return static::MSGS;
} const MSGS = [];
protected function MAILS(): array {
return static::MAILS;
} const MAILS = [];
protected function APP(): array {
return static::APP;
} const APP = [];
protected function USER(): array {
return static::USER;
} const USER = [];
/**
* @var array liste des profils dans lesquels cette configuration est valide.
*
* un tableau vide signifie que la configuration est valide dans tous les
* profils
*/
protected $profiles;
/** @var array */
protected $config;
function __construct(?array $config=null, string ...$profiles) {
if (!$profiles) $profiles = $this->PROFILES();
$this->profiles = $profiles;
A::update_nx($config, [
"schema" => $this->SCHEMA(),
"dbs" => $this->DBS(),
"msgs" => $this->MSGS(),
"mails" => $this->MAILS(),
"app" => $this->APP(),
"user" => $this->USER(),
]);
$this->config = $config;
}
function has(string $pkey, string $profile): bool {
if ($this->profiles && !in_array($profile, $this->profiles)) return false;
return A::phas_s($this->config, $pkey);
}
function get(string $pkey, string $profile) {
if ($this->profiles && !in_array($profile, $this->profiles)) return null;
return A::pget_s($this->config, $pkey);
}
function set(string $pkey, $value, string $profile): void {
if ($this->profiles && !in_array($profile, $this->profiles)) {
throw new ValueException("$profile: unauthorized profile");
}
A::pset_s($this->config, $pkey, $value);
}
}