diff options
Diffstat (limited to 'platform/www/lib/plugins/config/core/Setting/SettingNumeric.php')
-rw-r--r-- | platform/www/lib/plugins/config/core/Setting/SettingNumeric.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/platform/www/lib/plugins/config/core/Setting/SettingNumeric.php b/platform/www/lib/plugins/config/core/Setting/SettingNumeric.php new file mode 100644 index 0000000..8a6b179 --- /dev/null +++ b/platform/www/lib/plugins/config/core/Setting/SettingNumeric.php @@ -0,0 +1,42 @@ +<?php + +namespace dokuwiki\plugin\config\core\Setting; + +/** + * Class setting_numeric + */ +class SettingNumeric extends SettingString { + // This allows for many PHP syntax errors... + // var $_pattern = '/^[-+\/*0-9 ]*$/'; + // much more restrictive, but should eliminate syntax errors. + protected $pattern = '/^[-+]? *[0-9]+ *(?:[-+*] *[0-9]+ *)*$/'; + protected $min = null; + protected $max = null; + + /** @inheritdoc */ + public function update($input) { + $local = $this->local; + $valid = parent::update($input); + if($valid && !(is_null($this->min) && is_null($this->max))) { + $numeric_local = (int) eval('return ' . $this->local . ';'); + if((!is_null($this->min) && $numeric_local < $this->min) || + (!is_null($this->max) && $numeric_local > $this->max)) { + $this->error = true; + $this->input = $input; + $this->local = $local; + $valid = false; + } + } + return $valid; + } + + /** @inheritdoc */ + public function out($var, $fmt = 'php') { + if($fmt != 'php') return ''; + + $local = $this->local === '' ? "''" : $this->local; + $out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n"; + + return $out; + } +} |