mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-11-22 01:49:08 +01:00
LSconfig: add escape_key, explode_keys and implode_keys helper methods
Allow escaping dot in config variable key
This commit is contained in:
parent
e77a96066d
commit
9384d2e312
1 changed files with 54 additions and 3 deletions
|
@ -59,6 +59,57 @@ class LSconfig {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Explode configuration variable keys
|
||||
* @param string $value The variable name to explode
|
||||
* @return array<string> The exploded variable keys
|
||||
*/
|
||||
public static function explode_keys($value) {
|
||||
$keys = [];
|
||||
$key = "";
|
||||
for($i=0; $i<strlen($value); $i++) {
|
||||
switch($value[$i]) {
|
||||
case '\\':
|
||||
$key .= $value[$i+1];
|
||||
$i++;
|
||||
break;
|
||||
case '.':
|
||||
$keys[] = $key;
|
||||
$key = "";
|
||||
break;
|
||||
default:
|
||||
$key .= $value[$i];
|
||||
}
|
||||
}
|
||||
$keys[] = $key;
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a variable name key
|
||||
* @param string $value The variable name key to explode
|
||||
* @return string The escaped variable name key
|
||||
*/
|
||||
public static function escape_key($value) {
|
||||
return str_replace('.', '\\.', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implode configuration variable keys
|
||||
* @param array<array<string>|string> $args Function arguments could be an array of key or a key
|
||||
* to implode
|
||||
* @return string The imploded configuration variable keys
|
||||
*/
|
||||
public static function implode_keys(...$args) {
|
||||
$keys = [];
|
||||
foreach ($args as $arg)
|
||||
if (is_array($arg))
|
||||
$keys = array_merge($keys, $arg);
|
||||
else
|
||||
$keys[] = $arg;
|
||||
return implode('.', array_map(['LSconfig', 'escape_key'], $keys));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific configuration variable value
|
||||
*
|
||||
|
@ -73,7 +124,7 @@ class LSconfig {
|
|||
* @return mixed The configuration variable value
|
||||
**/
|
||||
public static function get($var, $default=null, $cast=null, $data=null) {
|
||||
$vars = explode('.', $var);
|
||||
$vars = self :: explode_keys($var);
|
||||
$value = $default;
|
||||
if (is_array($vars)) {
|
||||
$value = (is_array($data)?$data:self :: $data);
|
||||
|
@ -142,7 +193,7 @@ class LSconfig {
|
|||
}
|
||||
else {
|
||||
foreach ($subkeys as $subkey) {
|
||||
$key = "$root_key.$subkey";
|
||||
$key = self :: implode_keys(self :: explode_keys($root_key), $subkey);
|
||||
$return[$key] = self :: get($key, $default, $cast, $data);
|
||||
}
|
||||
}
|
||||
|
@ -163,7 +214,7 @@ class LSconfig {
|
|||
* @return boolean True si tout s'est bien passé, False sinon
|
||||
**/
|
||||
public static function set($var,$val) {
|
||||
$vars=explode('.',$var);
|
||||
$vars = self :: explode_keys($var);
|
||||
if(is_array($vars)) {
|
||||
$code='self :: $data';
|
||||
foreach ($vars as $v) {
|
||||
|
|
Loading…
Reference in a new issue