News:

Looking for documentation? Take a look on our wiki

Main Menu

VmConfig::get () the default is ignored

Started by balai, January 23, 2018, 12:30:57 PM

Previous topic - Next topic

balai

Hi

I am checking the VmConfig::get('value', 'default');
Though even when the config value is empty the default is ignored.

Looking at the code, it justifies that. The default is taken into account only when the setting does not exist, ignoring the empty inputs.

static function get($key, $default='',$allow_load=FALSE)
{

$value = '';
if ($key) {

if (empty(self::$_jpConfig->_params) && $allow_load) {
self::loadConfig();
}

if (!empty(self::$_jpConfig->_params)) {
if(array_key_exists($key,self::$_jpConfig->_params) && isset(self::$_jpConfig->_params[$key])){
$value = self::$_jpConfig->_params[$key];
} else {
$value = $default;
}

} else {
$value = $default;
}

} else {
$app = JFactory::getApplication();
$app -> enqueueMessage('VmConfig get, empty key given');
}

return $value;
}

Studio 42

Have you read correctly?
if(array_key_exists($key,self::$_jpConfig->_params) && isset(self::$_jpConfig->_params[$key])){
$value = self::$_jpConfig->_params[$key];
} else {
$value = $default;
}

isset(self::$_jpConfig->_params[$key]) mean if not exist value and not if empty $value
So $default is loaded if $key do not exist.
Have you do a test ?

balai

Have you read my post?

This is exactly the bug i reported.

Studio 42

It's same as Joomla input.
If the value is set, default is not used.
so if(array_key_exists($key,self::$_jpConfig->_params) && isset(self::$_jpConfig->_params[$key])){ return the $value in all case if the $key exist
Else it return $default
It's why i have ask
QuoteHave you do a test ?

Milbo

Balai, a lot configs use empty String or 0. It is completly correct that way.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

balai

#5
I think 0 is acceptable, but an empty  field is not a value.
Also the ideal would be to have also some data validation like the inputs/requests.
VmConfig::get ('value', 'default', 'type');
*type > optional (defaults to string)

Accepting empty or invalid values, renders the default param almost useless.
This could generate problems to scripts using these values, since it will pass non expected values to them.

For example yesterday i confronted an issue, where a i could not get the proper image's filename, because the thumbnail's length setting was empty or a space.
As you know the thumbnails have this format: name_lengthxwidth.png
Hence i was getting an invalid filename (missing the length part). Instead i was expecting to read the default value that was there exactly for that.

Studio 42

VmConfig::get ('value', 'default', 'type');
Can be done using : (type)VmConfig::get ('value', 'default');
Where type is a valid PHP type eg. Int, String, Bool, Array ...

Milbo

Balai, the empty value is the common value for "use global"! That is the reason you have "is set" and "is empty"
http://php.net/manual/en/function.isset.php and http://php.net/manual/en/function.empty.php
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/