xref: /dokuwiki/lib/plugins/config/core/Setting/SettingNumeric.php (revision dccd6b2bba7367e4d1d2d7aa84c9f9d15584b593)
1<?php
2
3namespace dokuwiki\plugin\config\core\Setting;
4
5/**
6 * Class setting_numeric
7 */
8class SettingNumeric extends SettingString
9{
10    // This allows for many PHP syntax errors...
11    // var $_pattern = '/^[-+\/*0-9 ]*$/';
12    // much more restrictive, but should eliminate syntax errors.
13    protected $pattern = '/^[-+]? *\d+ *(?:[-+*] *\d+ *)*$/';
14    protected $min;
15    protected $max;
16
17    /** @inheritdoc */
18    public function update($input)
19    {
20        $local = $this->local;
21        $valid = parent::update($input);
22        if($valid && !(is_null($this->min) && is_null($this->max))) {
23            $numeric_local = (int) eval('return ' . $this->local . ';');
24            if((!is_null($this->min) && $numeric_local < $this->min) ||
25                (!is_null($this->max) && $numeric_local > $this->max)) {
26                $this->error = true;
27                $this->input = $input;
28                $this->local = $local;
29                $valid = false;
30            }
31        }
32        return $valid;
33    }
34
35    /** @inheritdoc */
36    public function out($var, $fmt = 'php')
37    {
38        if($fmt != 'php') return '';
39
40        $local = $this->local === '' ? "''" : $this->local;
41        $out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n";
42
43        return $out;
44    }
45}
46