Guess yes/no values in php config manager

This commit is contained in:
jo 2021-08-06 12:23:30 +02:00
parent 5466cd8688
commit d0836b4313
2 changed files with 33 additions and 1 deletions

View File

@ -31,7 +31,7 @@ class Config {
$CC_CONFIG['basePort'] = $values['general']['base_port'];
$CC_CONFIG['stationId'] = $values['general']['station_id'];
$CC_CONFIG['phpDir'] = $values['general']['airtime_dir'];
$CC_CONFIG['forceSSL'] = isset($values['general']['force_ssl']) ? $values['general']['force_ssl'] : FALSE;
$CC_CONFIG['forceSSL'] = isset($values['general']['force_ssl']) ? Config::isYesValue($values['general']['force_ssl']) : FALSE;
$CC_CONFIG['protocol'] = isset($values['general']['protocol']) ? $values['general']['protocol'] : '';
if (isset($values['general']['dev_env'])) {
$CC_CONFIG['dev_env'] = $values['general']['dev_env'];
@ -108,4 +108,15 @@ class Config {
}
return self::$CC_CONFIG;
}
/**
* Check if the string is one of 'yes' or 'true' (case insensitive).
*/
public static function isYesValue($value)
{
if (is_bool($value)) return $value;
if (!is_string($value)) return false;
return in_array(strtolower($value), ['yes', 'true']);
}
}

View File

@ -0,0 +1,21 @@
<?php
require_once "../application/configs/conf.php";
class ConfigTest extends PHPUnit_Framework_TestCase
{
public function testIsYesValue()
{
foreach (["yes", "Yes", "True", "true", true] as $value) {
$this->assertEquals(Config::isYesValue($value), true);
}
foreach (["no", "No", "False", "false", false] as $value) {
$this->assertEquals(Config::isYesValue($value), false);
}
foreach (["", "anything", "0", 0, "1", 1, null] as $value) {
$this->assertEquals(Config::isYesValue($value), false);
}
}
}